Pagini recente » Cod sursa (job #854945) | Cod sursa (job #593608) | Cod sursa (job #96549) | Cod sursa (job #2222869) | Cod sursa (job #1668656)
#include <iostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <numeric>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <map>
#define pb push_back
#define mp make_pair
#define INF numeric_limits<int>::max()
#define lsb(x) (-x)&x
#define int64 long long
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
int n,val[16002],dp[16002],sol=-INF;
vector< vector<int> > tree;
void dfs(int x,int p)
{
dp[x]=val[x];
for(vector<int>::iterator it=tree[x].begin();it!=tree[x].end();it++)
if(*it!=p)
{
dfs(*it,x);
dp[x]+=max(dp[*it],0);
}
sol=max(sol,dp[x]);
}
int main()
{
in>>n;
tree=vector< vector<int> > (n+1);
for(int i=1;i<=n;i++)
in>>val[i];
for(int i=1;i<n;i++)
{
int x,y;
in>>x>>y;
tree[x].pb(y);
tree[y].pb(x);
}
dfs(1,0);
out<<sol<<'\n';
return 0;
}