Cod sursa(job #2492464)

Utilizator victorv88Veltan Victor victorv88 Data 14 noiembrie 2019 19:55:48
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

vector<int>graph[16005];

int n, valori[16005], from, to, dp[16006], maxi=-1005;
bool viz[16005];

void parcurgere(int ind)
{
    viz[ind]=1;
    for (int &v:graph[ind])
    {
        if (!viz[v])
        {
            parcurgere(v);
            dp[ind]=max(dp[ind],dp[ind]+dp[v]);
        }
    }
}

int main( )
{
    f >> n;
    for (int i=1; i<=n; ++i)
        f >> dp[i];
    for (int i=1; i<n; ++i)
    {
        f >> from >> to;
        graph[from].push_back(to);
        graph[to].push_back(from);
    }
    parcurgere(1);
    for (int i=1; i<=n; ++i)
    {
        if (dp[i]>maxi)
            maxi=dp[i];
    }
    g << maxi;
    return 0;
}