Cod sursa(job #2709251)

Utilizator AndreiJJIordan Andrei AndreiJJ Data 20 februarie 2021 08:51:34
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <set>
#include <iostream>
#include <vector>
#include <bitset>

using namespace std;

const int N = 16005, oo = 2e9;

vector <int> L[N];
bitset <N> vis;
int dp[N], cost[N], n;

void Read()
{
    ifstream fin("asmax.in");
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> cost[i];
    for (int i = 1, x, y; i < n; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    fin.close();
}

void DFS(int vertex)
{
    vis[vertex] = 1;
    dp[vertex] = cost[vertex];
    for (auto next : L[vertex])
    {
        if (!vis[next])
        {
            DFS(next);
            if (dp[next] > 0)
                dp[vertex] += dp[next];
        }
    }
}

void Solve()
{
    
    ofstream fout("asmax.out");
    DFS(1);
    int ans = dp[1];
    for (int i = 2; i <= n; i++)
        ans = max(ans, dp[i]);
    fout << ans << "\n";
    fout.close();
}

int main()
{
    Read();
    Solve();
    return 0;
}