Cod sursa(job #3280998)

Utilizator mateistefan11matei stefan mateistefan11 Data 28 februarie 2025 08:15:03
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
#define oo 2000000001
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
/**
2 4 6
1 3
2 10
4 6
5 8

3
*/
int n;
int val[16005];
int root[16005];
int dp[16005];
vector<int> G[16005];
void DFS(int x)
{
    if(G[x].empty())
        dp[x] = val[x];

    for(auto e : G[x])
        DFS(e);
    for(auto e : G[x])
    {
        dp[x] = max(dp[x], dp[e] + val[x]);
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i,j,x,y;
    fin >> n;
    for(i = 1; i <= n; i++)
        fin >> val[i];
    for(i = 1; i <= n; i++)
        root[i] = 1;
    for(i = 1; i < n; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
        root[y] = 0;
    }
    for(i = 1; i <= n && root[i] == 0; i++)
        ;
    int rad = i;
    DFS(rad);
    fout << dp[rad] + val[rad];
    return 0;
}