Cod sursa(job #2704872)

Utilizator gabriel_froneaFronea Gabriel gabriel_fronea Data 11 februarie 2021 15:37:30
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int N=16001;
bool viz[N];
int n, d[N], sum=-1001, sol[N];

vector <int> a[N];

void dfs(int x)
{
    viz[x]=true;
    sol[x]=d[x];
    for(auto y:a[x])
    {
        if(!viz[y])
        {
            dfs(y);
            if(sol[y]>0)
            {
                sol[x]+=sol[y];
            }
        }
        sum=max(sum,sol[x]);
    }
}

int main()
{
    in>>n;
    int x, y;
    for(int i=1; i<=n; i++)
    {
        in>>d[i];
    }
    while(in>>x>>y)
    {
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1);
    out<<sum;
    return 0;
}