Cod sursa(job #2823105)

Utilizator oporanu.alexAlex Oporanu oporanu.alex Data 26 decembrie 2021 23:08:56
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int nmax = 20000;
vector<int> adj[nmax];
int maxSum[nmax];
bool vis[nmax];

void dfs(int crt) {
    vis[crt] = true;
    for(auto ngb: adj[crt]) {
        if(vis[ngb] == false) {
            dfs(ngb);
            if(maxSum[ngb] > 0) {
                maxSum[crt] += maxSum[ngb];
            }
        }
    }
}

int main() {
    int N;
    fin >> N;
    for(int i = 1; i <= N; ++i) {
        int x; fin >> x;
        maxSum[i] = x;
    }

    for(int i = 1; i <= N - 1; ++i) {
        int x, y;
        fin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs(1);
    int maxx = INT_MIN;
    for(int i = 1; i <= N; ++i) {
        maxx = max(maxx, maxSum[i]);
    }

    fout << maxx << '\n';
    return 0;
}