Cod sursa(job #3241354)

Utilizator luc3lexaAlexandrescu Luca luc3lexa Data 29 august 2024 14:48:43
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

const int nmax = 16e3+10;

vector<int> values(nmax,0),dp(nmax,0),visited(nmax,0);
vector<vector<int>> mat(nmax);
int n;

void read_input(){
	cin >> n;
	for(int i = 1; i <=n; i++){
		cin >> values[i];
	};
	int nod_1,nod_2;
	for(int i = 1; i <=n-1; i++){
		cin >> nod_1 >> nod_2;
		mat[nod_1].push_back(nod_2);
		mat[nod_2].push_back(nod_1);
	}
};

void dfs(int nod){
	visited[nod] = 1;
	dp[nod] = values[nod];
	
	for(auto nod_aux : mat[nod]){
		if(!visited[nod_aux]){
			dfs(nod_aux);
			dp[nod] = max(dp[nod],dp[nod] + dp[nod_aux]);
		}
	}
};


int main(){
	read_input();
	dfs(1);
	int ans = -1;
	for(int i = 1; i <=n; i++){
		ans = max(ans,dp[i]);
	};
	cout << ans;
}