Cod sursa(job #1584358)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 29 ianuarie 2016 23:05:02
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 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;
vector<int> G[NMAX];
int A[NMAX];
bool viz[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);
	}
}

int dp(int x)
{
	viz[x]=true;
	int rs=-INF;
	rs=max(rs,A[x]);
	int sub=0;
	for(int i=0; i<G[x].size(); ++i)
		if(!viz[G[x][i]])
			sub=max(sub, sub + dp(G[x][i]));
	rs=max(rs,sub+A[x]);
	return rs;
}

void solve()
{
	cout<<dp(1);
}

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;
}