Pagini recente » Cod sursa (job #2246445) | Cod sursa (job #1273941) | Cod sursa (job #1763246) | Cod sursa (job #2826101) | Cod sursa (job #512897)
Cod sursa(job #512897)
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
FILE *f=fopen("asmax.in","r");
FILE *g=fopen("asmax.out","w");
vector <int> a[16005];
int cost[16005],N,x,y,sol,viz[16005];
void df(int v){
viz[v]=1;
for(unsigned int i=0;i<a[v].size();i++)
if(!viz[a[v][i]]){
df(a[v][i]);
if(cost[a[v][i]] > 0)
cost[v]+=cost[a[v][i]];
if(cost[v]>sol)
sol=cost[v];
}
}
int main(){
sol=-2000000000;
fscanf(f,"%d",&N);
for(int i=1;i<=N;++i)
fscanf(f,"%d",&cost[i]);
for(int i=1;i<N;++i)
fscanf(f,"%d%d",&x,&y),a[x].push_back(y),a[y].push_back(x);
for(int i=1;i<=N;++i)
if(viz[i]==0)
df(i);
fprintf(g,"%d",sol);
return 0;
}