Pagini recente » Cod sursa (job #1250440) | Cod sursa (job #2420235) | Cod sursa (job #1675858) | Cod sursa (job #428922) | Cod sursa (job #1920374)
#include <fstream>
#include <iostream>
#include <vector>
#define maxn 100005
#define INF 0x3f3f3f3f
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
vector <int> v[maxn];
int n, m, maxim = -INF;
int visited[maxn], cost[maxn];
void DFS(int current){
visited[current] = 1;
for(int i = 0; i < v[current].size(); ++i){
int nod = v[current][i];
if(!visited[nod]){
DFS(nod);
cost[current] = max(cost[current], cost[current] + cost[nod]);
}
}
}
int main(){
f >> n;
for(int i = 1; i <= n; ++i) f >> cost[i];
for(int i = 1; i <= n-1; ++i){
int x, y;
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
DFS(1);
for(int i = 1; i <= n; ++i){
maxim = max(maxim, cost[i]);
}
g << maxim << '\n';
}