Pagini recente » Monitorul de evaluare | Cod sursa (job #1778082) | Cod sursa (job #452579) | Profil Ramona2007 | Cod sursa (job #364595)
Cod sursa(job #364595)
#include <cstdio>
#include <vector>
using namespace std;
struct node{
int seen;
int val,max;
vector<int> children;
};
node* mat[16001];
void dfs(int x){
mat[x]->seen=1;
int t,j;
for(j=0;j<mat[x]->children.size();j++){
t=mat[x]->children[j];
if(mat[t]->seen==0){
dfs(t);
if(mat[t]->max>0)
mat[x]->max+=mat[t]->max;
}
}
}
int main(){
freopen("asmax.in","rt",stdin);
freopen("asmax.out","wt",stdout);
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
mat[i]=new node;
scanf("%d",&(mat[i]->val));
mat[i]->max=mat[i]->val;
}
int a,b;
for(i=1;i<n;i++){
scanf("%d%d",&a,&b);
mat[a]->children.push_back(b);
mat[b]->children.push_back(a);
}
dfs(1);
int max=mat[1]->max;
for(i=2;i<=n;i++)
if(mat[i]->max>max)
max=mat[i]->max;
printf("%d",max);
return 0;
}