Cod sursa(job #2832856)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 14 ianuarie 2022 13:58:01
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

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

int dp[16006], n, v[16006], mx = -1005, viz[16006];
vector <int> h[16005];

void Dfs(int x)
{
    viz[x] = 1;
    dp[x] = v[x];
    for(auto i : h[x])
        if(viz[i] == 0)
    {
        Dfs(i);
        int s = dp[x] + dp[i];
        if(s > dp[x])
            dp[x] = s;
//        Dfs(i);
    }
}

int main()
{
    int x, y;
    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> v[i];
        dp[i] = v[i];
    }
    for(int i = 1; i < n; i++)
    {
        fin >> x >> y;
        h[x].push_back(y);
        h[y].push_back(x);
    }
    Dfs(1);
    for(int i = 1; i <= n; i++)
        mx = max(mx, dp[i]);
    fout << mx;
    return 0;
}