Pagini recente » Cod sursa (job #2183487) | Cod sursa (job #1397106) | Cod sursa (job #2675668) | Cod sursa (job #1985480) | Cod sursa (job #1563210)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
typedef struct nod{
int key,val;
nod* next;
}* lnod;
lnod A[16010];
bool B[16010];
int maxi,N,V[16010],sef[16010],boss;
void add(lnod &a,int key){
lnod b = new nod;
b->key = key;
b->next = a;
a = b;
}
int rez(int key){
int rs = 0;
if(A[key]){
for(lnod l = A[key];l;l = l->next){
rs+=max(0,rez(l->key));
}
}
rs+=V[key];
if(maxi < rs) maxi = rs;
return rs;
}
int main(){
fin >> N;
for(int i = 1;i<=N;i++) fin >> V[i];
for(int i = 1;i<N;i++){
int x,y;
fin >> x >> y;
if(sef[x] == 0) sef[x] = x;
sef[y] = x;
add(A[x],y);
}
boss = 1;
while(sef[boss] != boss) boss = sef[boss];
rez(boss);
fout << maxi;
return 0;
}