Pagini recente » Cod sursa (job #1383674) | Cod sursa (job #367553) | Cod sursa (job #2990413) | Cod sursa (job #2102979) | Cod sursa (job #1777159)
#include <bits/stdc++.h>
#define NMAX 16005
using namespace std;
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
int C[NMAX];
vector < int > G[NMAX];
bitset < NMAX > T;
void Dfs(int nod,int &S,int s,int &st){
if(T[nod] == 1)
return;
T[nod] = 1;
for(int i = 0; i < G[nod].size(); i++){
st += C[G[nod][i]];
if(st > s)
s = st;
Dfs(G[nod][i],S,s,st);
if(st > s)
s = st;
if(S < s)
S = s;
}
}
int main()
{
ios :: sync_with_stdio(false);
fin.tie(NULL);
int n,S,s,st,x,y;
fin >> n;
for(int i = 1; i <= n; i ++)
fin >> C[i];
S = C[1];
for(int i = 1; i < n; i++){
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
T[j] = 0;
s = st = C[i];
Dfs(i,S,s,st);
}
fout << S;
return 0;
}