Cod sursa(job #1208118)

Utilizator PopescuMihai95Popescu Mihai PopescuMihai95 Data 14 iulie 2014 19:57:53
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int n,m,i,j,v[16005],s,t,nr;
vector < int > V[16005];
int w[16005],viz[16005],nr1,nr2;
void citire()
{
    int x,y;
    for (i=1;i<=n;i++)
    {
        scanf("%d %d",&x,&y);
        V[x].push_back(y);
        V[y].push_back(x);
    }
}
void df(int x)
{
    int i,j,t;
    viz[x]=1; t=v[x];
    for (j=0;j<V[x].size();j++) if (!viz[V[x][j]])
    {
        viz[V[x][j]]=1;
        df(V[x][j]);
        if (w[V[x][j]]>0) t=t+w[V[x][j]];
    }
    w[x]=t;
    if (s<t) s=t;
}
int main()
{
    freopen("asmax.in","r",stdin);
    freopen("asmax.out","w",stdout);
    scanf("%d",&n);
    for (i=1;i<=n;i++) scanf("%d",&v[i]),s=s+v[i];
    citire(); nr1=0;
    df(1); printf("%d",s);
    return 0;
}