Cod sursa(job #3280535)

Utilizator CiorpionanRoman Matei-Ciprian Ciorpionan Data 26 februarie 2025 17:15:25
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 16000;

int v[N+1], s_max[N+1];
bool viz[N+1];
vector <int> a[N+1];

void dfs(int x){
    viz[x] = true;
    s_max[x] = v[x];
    for (auto y:a[x]){
        if (!viz[y]){
            dfs(y);
            if (s_max[y] > 0){
                s_max[x] += s_max[y];
            }
        }
    }
}

int main()
{
    ifstream in("asmax.in");
    ofstream out("asmax.out");
    int n;
    in >> n;
    for (int i = 1; i <= n; i++){
        in >> v[i];
    }
    for (int i = 1; i < n; i++){
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1);
    int maxmax = s_max[1];
    for (int i = 1; i <= n; i++){
        if (s_max[i] > maxmax){
            maxmax = s_max[i];
        }
    }
    out << maxmax;
    in.close();
    out.close();
    return 0;
}