Pagini recente » Cod sursa (job #2563068) | Cod sursa (job #2449503) | Cod sursa (job #2470258) | Cod sursa (job #182609) | Cod sursa (job #2677991)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 16000
#define inf 1001
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
int n, v[NMAX+10], dp[NMAX+10], ans;
vector <int> nod[NMAX+10];
void dfs(int x, int p)
{ for(int i=0; i<nod[x].size(); i++)
if(nod[x][i] != p)
{ dfs(nod[x][i], x);
dp[x] += max(0, dp[nod[x][i]]);
}
dp[x] = max(0, v[x] + dp[x]);
ans = max(ans, dp[x]);
}
int main()
{
f >> n;
int maxi = -inf, ok = 0;
for(int i=1; i<=n; i++)
{ f >> v[i];
if(v[i] >= 0) ok = 1;
maxi = max(maxi, v[i]);
}
if(!ok)
{ g << maxi << '\n';
return 0;
}
for(int i=1; i<n; i++)
{ int nod1, nod2;
f >> nod1 >> nod2;
nod[nod1].push_back(nod2);
nod[nod2].push_back(nod1);
}
dfs(1, 0);
g << ans << '\n';
return 0;
}