Cod sursa(job #694213)

Utilizator warchildmdMihail Burduja warchildmd Data 27 februarie 2012 19:20:32
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <cstdio>
#include <vector>
using namespace std;

int N, M;
int RESULT;
vector<int> ls[16001];
int cost[16001];

int sum(int x, int from)
{
    int s = cost[x];
    for(int i = 0; i < ls[x].size(); i++)
    {
        if(ls[x][i] == from)
        {
            continue;
        }
        int temp = sum(ls[x][i], x);
        if(temp > 0)
        {
            s += temp;
        }
    }
    if(s > RESULT)
    {
        RESULT = s;
    }
    return s;
}

int main()
{
    freopen("asmax.in", "r", stdin);
    freopen("asmax.out", "w", stdout);
    RESULT = -16777216;

    scanf("%d", &N);
    M = N - 1;
    for(int i = 1; i <= N; i++)
    {
        scanf("%d", &cost[i]);
    }
    for(int i = 0; i < M; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        ls[x].push_back(y);
        ls[y].push_back(x);
    }

    sum(1, 0);
    printf("%d", RESULT);
    return 0;
}