Cod sursa(job #2892100)

Utilizator 100pCiornei Stefan 100p Data 20 aprilie 2022 19:15:59
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>
#define ull unsigned long long
#define FILES freopen("asmax.in","r",stdin);\
              freopen("asmax.out","w",stdout);
#define CMAX 15485863
#define fastio std::ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define mp make_pair
#define INF 1e18
#define mod 666013
#define ll long long
#define SMAX 300
#define MAX 16000
#define pb push_back
#define add emplace_back
#define void inline void
#define int ll
using namespace std;
int n,dp[MAX+5],best = -1e9;
vector<int>v[MAX+5];
bool check[MAX+5];
void dfs(int x)
{
    check[x] = 1;
    for(auto j : v[x])
    {
        if(!check[j])
        {
            dfs(j);
            dp[x] += dp[j];
        }
    }
    dp[x] = max(dp[x],0LL);
}
signed main()
{
    fastio
    FILES
    cin >> n;
    for(int i = 1;i <= n; ++i)
        cin >> dp[i],best = max(best,dp[i]);
    for(int i = 1;i < n; ++i)
    {
        int a,b;
        cin >> a >> b;
        v[a].add(b),v[b].add(a);
    }
    dfs(1);
    for(int i = 1;i <= n; ++i)
        best = max(best,dp[i]);
    cout << best;
}