Cod sursa(job #3229359)

Utilizator SmenPowerDragos Huiu SmenPower Data 15 mai 2024 16:45:37
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>
#include <climits>
#include <vector>

using namespace std;

const int N = 16e3;

vector <int> a[N + 1];
int val[N + 1];
bool viz[N + 1];
int n;

int dfs(int x, int &smax) 
{
    viz[x] = true;
    int s = val[x];
    
    for (int y : a[x])
        if (!viz[y])
            s += max(0, dfs(y, smax));
    
    smax = max(smax, s);
    return s;
}

int main() 
{
    ifstream in("asmax.in");
    ofstream out("asmax.out");
    
    in >> n;
    
    for (int i = 1; i <= n; ++i)
        in >> val[i];
    
    for (int i = 1, x, y; i < n && in >> x >> y; ++i)
        a[x].push_back(y), a[y].push_back(x);
    
    int smax = INT_MIN;
    dfs(1, smax);
    out << smax << endl;
}