Cod sursa(job #2543891)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 11 februarie 2020 16:59:17
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 16005;

vector <int> l[DIM];

int dp[DIM];
int c[DIM];

bool viz[DIM];

void dfs(int x)
{
    viz[x] = true;

    dp[x] = c[x];
    for(int i = 0; i < l[x].size(); i++)
    {
        if(viz[l[x][i]] == false)
        {
        dfs(l[x][i]);
        dp[x] = max(dp[x], dp[x] + dp[l[x][i]]);
        }

    }
}
int main()
{

    int n;

    in >> n;

    for(int  i = 1; i <= n; i++)
        {in >> dp[i];
           c[i] = dp[i];
        }
    for(int i = 1; i < n; i++)
    {
        int x, y;

        in >> x >> y;

        l[x].push_back(y);
        l[y].push_back(x);

    }

    int rez = -1e9;
    dfs(1);

    for(int i = 1; i <= n ;i++)
        rez = max(rez,dp[i]);

    out << rez;

    return 0;
}