Cod sursa(job #2155850)

Utilizator Juve45UAIC Alexandru Ionita Juve45 Data 8 martie 2018 10:45:30
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>
using namespace std;
#define dmax 16001
vector <int > v[dmax];
int n, use[dmax], val[dmax];
int smax=-9999999;
int maxv=-99999;
 
ifstream fin("asmax.in");
ofstream fout("asmax.out");
 
void read()
{
    int a, b;
    fin>>n;
 
    for(int i=1; i<=n; i++)
    {
        fin>>val[i];
        if(val[i]>maxv)
            maxv=val[i];
 
    }
    for(int i=1; i<n; i++)
    {
        fin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
}
 
int cut_tree(int vf)
{
    int k=0, vfg=0;
    use[vf]=1;
 
    for(int i=0; i<v[vf].size(); i++)
    {
        int y=v[vf][i];
        if(use[y]==0)
        {
            vfg=cut_tree(y);
            k=k+vfg;
            if(vfg>smax)
                smax=k;
        }
 
    }
    if(k+val[vf]>0)
        return k+val[vf];
    else return 0;
 
}
 
int main()
{
    read();
int asd=cut_tree(1);
    if(smax<asd)
        smax=asd;
    if(smax)
        fout<<smax<<'\n';
    else fout<<maxv<<'\n';
 
    return 0;
}