Cod sursa(job #2780217)

Utilizator seburebu111Mustata Dumtru Sebastian seburebu111 Data 6 octombrie 2021 15:00:40
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

const int N = 1e5;

int n;
int val[N+1];
int dp[N+1];
vector<int> a[N+1];
bitset <N+1> viz;

void dfs(int x)
{
    viz[x] = 1;
    dp[x]=val[x];
    for (auto y: a[x])
    {
        if(!viz[y])
        {
            dfs(y);
            dp[x]=max(dp[x], dp[x] + dp[y]);
        }
    }
}

int main()
{

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

    int maxx=-1000;
    dfs(1);
    for (int i = 1; i < n; i++)
    {
        maxx=max(maxx, dp[i]);
    }
    out << maxx;
    in.close();
    out.close();
    return 0;
}