Pagini recente » Cod sursa (job #248306) | Cod sursa (job #2416747) | Cod sursa (job #612084) | Cod sursa (job #266057) | Cod sursa (job #2201695)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#define MAXN 16005
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
vector<int>graf[MAXN];
bool viz[MAXN];
int n,cost_nod[MAXN],bestsum = -2e9;
void dfs(int nod){
viz[nod] = true;
int curent;
for(int i = 0; i < graf[nod].size(); i++){
curent = graf[nod][i];
if(!viz[curent]){
dfs(curent);
if(cost_nod[curent] > 0)
cost_nod[nod] += cost_nod[curent];
}
}
bestsum = max(bestsum,cost_nod[nod]);
}
int main()
{
in>>n;
for(int i = 1; i <= n; i++)
in>>cost_nod[i];
for(int i = 1; i <= n-1; i++){
int x,y;
in>>x>>y;
graf[x].push_back(y);
graf[y].push_back(x);
}
dfs(1);
out<<bestsum;
return 0;
}