Pagini recente » Cod sursa (job #1093887) | Cod sursa (job #309845) | Cod sursa (job #2351828) | Rating Serban Andrei (kronos719) | Cod sursa (job #1563168)
#include <fstream>
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){
B[key] = 1;
int rs = 0;
if(A[key]){
for(lnod l = A[key];l;l = l->next){
if(!B[l->key]){
int pas = V[l->key] + rez(l->key);
if(pas > 0) rs+=pas;
}
}
}
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;
}