Cod sursa(job #1342272)

Utilizator serban_ioan97Ciofu Serban serban_ioan97 Data 13 februarie 2015 18:46:00
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#define pb push_back
#define nmax 16016

using namespace std;

vector <int> a[nmax];
int n, i, max_tree=0, x, y;
bool used[nmax];
int value[nmax];

void dfs(int node)
{
    used[node]=true;
    vector<int>::iterator it;

    for(it=a[node].begin(); it!=a[node].end(); ++it)
    if(!used[*it])
    {
        dfs(*it);
        value[node]=max(value[node], value[node]+value[*it]);
    }

    max_tree=max(max_tree, value[node]);
}
int main()
{
    freopen("asmax.in", "rt", stdin);
    freopen("asmax.out", "wt", stdout);

    scanf("%d", &n);

    for(i=1; i<=n; ++i)
        scanf("%d", &value[i]);

    for(i=1; i<n; ++i)
    {
        scanf("%d%d", &x, &y);
        a[x].pb(y);
        a[y].pb(x);
    }

    dfs(1);

    printf("%d\n", max_tree);

    return 0;
}