Cod sursa(job #2286626)

Utilizator Seba951Sebastian Boerescu Seba951 Data 20 noiembrie 2018 16:28:13
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

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

void citire()
{
    int x,y;
    for(int i=1; i<=n-1; ++i)
    {
        in>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    in.close();
}

void defeseu(int x)
{
    viz[x]=true;
    scor[x]=v[x];
    for(int i=0; i < a[x].size(); i++)
    {
        int y = a[x][i];
        if(!viz[y])
        {
            defeseu(y);
            if(scor[y]>0) scor[x]+=scor[y];
        }
    }
}

int main()
{
    int scr=0;
    in>>n;
    for(int i=1; i<=n; i++) in>>v[i];
    citire();
    defeseu(1);
    sort(scor+1,scor+n+1);
    out<<scor[n];
    return 0;
}