Cod sursa(job #2828201)

Utilizator davidbejenariu2David Bejenariu davidbejenariu2 Data 6 ianuarie 2022 22:59:57
Problema Asmax Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

const int nMax = 16001;

int n;
int cost[nMax];
int maxCost[nMax];
vector<int> ad[nMax];

void DFS(int current, int parent) {
    maxCost[current] = cost[current];

    for (auto &w : ad[current]) {
        if (w != parent) {
            DFS(w, current);

            if (maxCost[w] > 0) {
                maxCost[current] += maxCost[w];
            }
        }
    }
}

int main() {
    int x, y;

    fin >> n;

    for (int i = 1; i <= n; ++i) {
        fin >> cost[i];
    }

    for (int i = 1; i <= n; ++i) {
        fin >> x >> y;

        ad[x].push_back(y);
        ad[y].push_back(x);
    }

    fin.close();

    DFS(1, 0);

    int ans = - (1 << 30);

    for (int i = 1; i <= n; ++i) {
        ans = max(ans, maxCost[i]);
    }

    fout << ans << '\n';
    fout.close();

    return 0;
}