Cod sursa(job #2425904)

Utilizator florin_salamFlorin Salam florin_salam Data 25 mai 2019 13:15:48
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <vector>
#include <fstream>

using namespace std;

const int NMAX = 16010;
int n, cost[NMAX], dp[NMAX], ans = -(1 << 30);
vector <int> graph[NMAX];

void DFS(int father, int node)
{
    for (auto i : graph[node])
    {
        if (i != father)
        {
            DFS(node, i);
            if (dp[i] > 0)
                dp[node] += dp[i];
        }
    }
    dp[node] += cost[node];
}

int main()
{
    ifstream fin("asmax.in");
    ofstream fout("asmax.out");
    fin >> n;
    for (int i = 1;i <= n;++i)
        fin >> cost[i];
    for (int i = 1;i < n;++i)
    {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    DFS(0, 1);
    for (int i = 1;i <= n;++i)
        ans = max(ans, dp[i]);
    fout << ans << "\n";
    fin.close();
    fout.close();
    return 0;
}