Cod sursa(job #1848661)

Utilizator GoogalAbabei Daniel Googal Data 16 ianuarie 2017 13:39:11
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <vector>
#define nmax 16001

using namespace std;

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

int n,v[nmax],cost[nmax],maxx=-1*nmax;
bool viz[nmax];

vector < int > leg[nmax];

inline void read()
{
    int i,a,b;
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>v[i];
        cost[i]=v[i];
    }
    for(i=1; i<n; i++)
    {
        fin>>a>>b;
        leg[a].push_back(b);
        leg[b].push_back(a);
    }

    fin.close();
}

void DFS(int x)
{
    int i;
    viz[x]=true;
    for(i=0; i<leg[x].size(); i++)
    {
        if(viz[leg[x][i]]==false)
        {
            DFS(leg[x][i]);
            if(cost[leg[x][i]]>0)
                cost[x]+=cost[leg[x][i]];
        }
        if(cost[x]>maxx)
            maxx=cost[x];
    }
}

int main()
{
    read();
    DFS(1);
    fout<<maxx;
    fout.close();
    return 0;
}