Cod sursa(job #2347132)

Utilizator DanSDan Teodor Savastre DanS Data 18 februarie 2019 15:27:43
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 16001;
int n, x, y, v[N];
bool viz[N];
vector <int> a[N];

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

int main()
{
    in>>n;
    for(int i=1; i<=n; i++)
        in>>v[i];
    for(int i=1; i<n; i++)
    {
        in>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1);
    int ans = -1001;
    for(int i=1; i<=n; i++)
        if(v[i] > ans) ans = v[i];
    out<<ans;
    return 0;
}