Cod sursa(job #2952587)

Utilizator samyro14Samy Dragos samyro14 Data 9 decembrie 2022 15:59:55
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("asmax.in");
ofstream  fout("asmax.out");
#define ll long long
const int maxn = 16e3;
const int maxg = -1e7;
vector<int> a[maxn + 2];
bitset<maxn + 2> visited;
int cost[maxn + 2];
int n;
int ans = maxg;
int dp[maxn + 2];
void read(){
    fin >> n;
    for(int i = 1; i <= n; ++i) fin >> cost[i];
    for(int i = 1; i < n; ++i){
        int x, y; fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}
void dfs(int i){
    visited[i] = 1;
    int m1 = cost[i];
    for(auto x : a[i]){
        if(!visited[x]){
            dfs(x);
            m1 = max(m1, m1 + dp[x]);
        }

    }
    dp[i] = m1;
    ans = max(ans, m1);
}

int main(){
    read();
    ans = cost[1];
    dfs(1);
    fout << ans;

    return 0;
}
/*
 dp[i] suma maxima pe care o pot obtine dintr-un subgraf ce contine nodul i

*/