Pagini recente » Cod sursa (job #3041016) | Cod sursa (job #262141) | Cod sursa (job #2903255) | Cod sursa (job #1110788) | Cod sursa (job #1584346)
#include <cstdio>
#define nmax 16002
#include <queue>
#include <algorithm>
#include <stack>
using namespace std;
int N,d[nmax],v[nmax],rez=0,s[nmax],t[nmax];
bool a[nmax][nmax],viz[nmax];
queue <int> Q;
stack <int> S;
int main(){
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
scanf("%d ",&N);
int i,x,y;
for(i = 1;i <= N;++i){
scanf("%d ",&v[i]);
s[i] = v[i];
}
for(i=1;i < N;++i){
scanf("%d %d ",&x,&y);
a[x][y] = a[y][x] = 1;
d[x]++;d[y]++;
}
t[1] = 0;
S.push(1);
while(!S.empty()){
x = S.top();
S.pop();
if(viz[x] == 0){
viz[x] = 1;
if(d[x] == 1)Q.push(x);
else
for(i = 1;i <= N;++i)
if(a[i][x] == 1){
S.push(i);
t[i] = x;
}
}
}
while(!Q.empty()){
x = Q.front();
Q.pop();
s[t[x]] = max(s[t[x]],s[t[x]]+s[x]);
d[t[x]]--;
if(d[t[x]] == 1)Q.push(t[x]);
rez = max(s[x],rez);
}
printf("%d\n",rez);
return 0;
}