Pagini recente » Cod sursa (job #2205934) | Cod sursa (job #2696583) | Cod sursa (job #2658742) | Cod sursa (job #525765) | Cod sursa (job #3280535)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16000;
int v[N+1], s_max[N+1];
bool viz[N+1];
vector <int> a[N+1];
void dfs(int x){
viz[x] = true;
s_max[x] = v[x];
for (auto y:a[x]){
if (!viz[y]){
dfs(y);
if (s_max[y] > 0){
s_max[x] += s_max[y];
}
}
}
}
int main()
{
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
in >> n;
for (int i = 1; i <= n; i++){
in >> v[i];
}
for (int i = 1; i < n; i++){
int x, y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
int maxmax = s_max[1];
for (int i = 1; i <= n; i++){
if (s_max[i] > maxmax){
maxmax = s_max[i];
}
}
out << maxmax;
in.close();
out.close();
return 0;
}