Pagini recente » Cod sursa (job #2777163) | Cod sursa (job #2755812) | Cod sursa (job #2753213) | Cod sursa (job #1921693) | Cod sursa (job #1584366)
#include <cstdio>
#define nmax 16002
#include <queue>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
int N,d[nmax],v[nmax],rez=0,s[nmax],t[nmax];
bool viz[nmax];
queue <int> Q;
stack <int> S;
vector <int> V[nmax];
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];
rez = min(rez,v[i]);
}
for(i=1;i < N;++i){
scanf("%d %d ",&x,&y);
V[x].push_back(y);
V[y].push_back(x);
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(vector <int> :: iterator it = V[x].begin();it != V[x].end();++it)
{
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]]--;d[x]--;
if(d[t[x]] == 1)Q.push(t[x]);
rez = max(s[x],rez);
}
printf("%d\n",rez);
return 0;
}