Cod sursa(job #3281029)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 28 februarie 2025 08:47:14
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 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], cnt, sum, maxx, summax, a[16003];
vector <int> g[16003];
bitset <16003> viz;

void DFS(int x)
{
    viz[x] = 1;
    for (int i : g[x])
        if (viz[i] == 0)
        {
            a[i] = v[i];
            DFS(i);
            if (a[x] + a[i] > a[x]) a[x] += a[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);
    }
    maxx = INT_MIN;
    a[1] = v[1];
    DFS(1);
    for (i = 1; i <= n; i++)
        maxx = max(maxx, a[i]);
    fout << maxx << "\n";
    return 0;
}