Pagini recente » Cod sursa (job #363621) | Cod sursa (job #467745) | Cod sursa (job #448867) | Cod sursa (job #2658078) | Cod sursa (job #1847193)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
const int maxn=16002;
const int inf=1<<30;
int N,c[maxn],dp[maxn],maxi=-inf;
bool x[maxn];
vector <int>G[maxn];
void dfs(int last,int nod)
{
int sz,i;
x[nod]=1;
sz=G[nod].size();
for (i=0;i<sz;i++)
if (x[G[nod][i]]==0)
{
dfs(nod,G[nod][i]);
if (dp[G[nod][i]]>0)
dp[nod]+=dp[G[nod][i]];
}
maxi=max(maxi,dp[nod]);
}
int main()
{
int i,x,y;
f>>N;
for (i=1;i<=N;i++)
{f>>c[i];dp[i]=c[i];}
for (i=1;i<N;i++)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(0,1);
g<<maxi;
}