Cod sursa(job #2952569)

Utilizator samyro14Samy Dragos samyro14 Data 9 decembrie 2022 15:47:54
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 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;
stack<int> s;
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;
    ans = max(ans, ans + cost[i]);
    for(auto x : a[i]){
        if(!visited[x])
            dfs(x);
    }

}

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

    return 0;
}
/*
 dp[i] suma maxima posibila folosindu-ma de primele i noduri

*/