Cod sursa(job #1585473)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 31 ianuarie 2016 08:47:06
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
// Template v2
#define pb push_back
#include<bits/stdc++.h>
   
using namespace std;
   
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<double, double> PKK;
   
const int CMAX = 350;
const int NMAX = 17000;
const short INF16 = 32000;
const int INF = int(1e9);
const LL INF64 = LL(1e18);
const LD EPS = 1e-9, PI = 3.1415926535897932384626433832795;

int a,b,n,rs=-INF;
vector<int> G[NMAX];
int A[NMAX], dp[NMAX];

void read()
{
	cin>>n;
	for(int i=1; i<=n; ++i)
		cin>>A[i];
	for(int i=1; i<=n-1; ++i)
	{
		cin>>a>>b;
		G[a].pb(b);
		G[b].pb(a);
	}
}

void dfs(int x, int p)
{
	dp[x]=A[x];
	for(int i=0; i<G[x].size(); ++i)
		if(G[x][i] != p){
			dfs(G[x][i], x);
			dp[x]=max(dp[x], dp[x] + dp[G[x][i]]);
		}
	rs=max(rs,dp[x]);
}

void solve()
{
	dfs(1,0);
	cout<<rs;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    assert(freopen("asmax.in", "rt", stdin));
    assert(freopen("asmax.out", "wt", stdout));
    
    read();
    solve();
   
    return 0;
}