Cod sursa(job #997730)

Utilizator Johny_Depp22Johnny Depp Johny_Depp22 Data 14 septembrie 2013 19:53:59
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
#include <vector>
using namespace std;

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

bool viz[16005];
vector < int > a[16005];
int x, y, n, v[16005], cost[16005], sol=-(1<<30);

void dfs(int nod)
{
    viz[nod]=1; cost[nod]=v[nod];

    for (int i=0; i<a[nod].size(); i++)
        if (viz[a[nod][i]]==0)
        {
            dfs(a[nod][i]);
            if (cost[a[nod][i]]>0)
                cost[nod]=cost[nod]+cost[a[nod][i]];
        }
    if (cost[nod]>sol) sol=cost[nod];
}

int main()
{
    f>>n;
    for (int i=1; i<=n; i++) f>>v[i];
    for (int i=1; i<n; i++)
    {
        f>>x>>y;
        a[x].push_back(y); a[y].push_back(x);
    }
    dfs(1);
    g<<sol;
    return 0;
}