Cod sursa(job #3241166)

Utilizator PetrudpPetru Paun Petrudp Data 27 august 2024 12:12:37
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

const int N = 16001;

vector<int> vec[N];
int val[N], dst[N], n;
bitset<N> viz;

void Init()
{
    in >> n;
    for(int i = 1; i <= n; i++)
    {
        in >> val[i];
        dst[i] = val[i];
    }
    for(int i = 1; i < n; i++)
    {
        int x, y;
        in >> x >> y;
        vec[x].push_back(y);
        vec[y].push_back(x);
    }
}

void Dfs(int k)
{
    viz[k] = 1;
    for(auto y : vec[k])
    {
        if(viz[y] == 0)
        {
            Dfs(y);
            if(dst[y] > 0)
            {
                dst[k] += dst[y];
            }
        }
    }
}

int main()
{
    Init();
    Dfs(1);
    int maxi = -10000;
    for(int i = 1; i <= n; i++)
    {
        maxi = max(maxi, dst[i]);
    }
    out << maxi;

    out.close();
    in.close();
}