Cod sursa(job #3281005)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 28 februarie 2025 08:23:53
Problema Asmax Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;

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

/**
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
*/

int n, v[16003];
vector <int> g[16003];
bitset <16003> viz;
long long cnt, sum, maxx, summax;

void DFS(int x)
{
    viz[x] = 1;
    maxx = max(maxx, cnt + v[x]);
    cnt += v[x];
    for (int i : g[x])
        if (viz[i] == 0)
            DFS(i);
}

int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i, j, x, y;
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> v[i];
    for (i = 1; i < n; i++)
    {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (j = 1; j <= n; j++)
    {
        sum = 0;
        viz[j] = 1;
        for (int i : g[j])
        {
            cnt = maxx = 0;
            DFS(i);
            //fout << maxx << " ";
            sum += maxx;
        }
        summax = max(summax, v[j] + sum);
        for (i = 1; i <= n; i++)
            viz[i] = 0;
    }
    fout << summax << "\n";
    return 0;
}