Cod sursa(job #1708400)

Utilizator vladdy47Bucur Vlad Andrei vladdy47 Data 26 mai 2016 23:48:07
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
# include <bits/stdc++.h>

using namespace std;

const int Nmax = 16000 + 5;

int n, Max, cost[Nmax], sol[Nmax];
bool ap[Nmax];

vector <int> G[Nmax];


void DFS(int node) {

    ap[node] = true;

    sol[node] += cost[node];

    for (int i = 0; i < G[node].size(); ++i){
        if (!ap[G[node][i]]) {
            DFS(G[node][i]);
            if (sol[G[node][i]] > 0) sol[node] += sol[G[node][i]];
        }
    }

}

int main ()
{
    ifstream f("asmax.in");
    ofstream g("asmax.out");

    int x, y;

    f >> n;

    for (int i = 1; i <= n; ++i)
        f >> cost[i];


    while (f >> x >> y) {
        G[x].push_back(y);
        G[y].push_back(x);
    }

    DFS(1);

    Max = -INT_MAX;

    for (int i = 1; i <= n; ++i)
        Max = max(Max, sol[i]);

    g << Max << '\n';

    return 0;
}