Pagini recente » Cod sursa (job #1708620) | Cod sursa (job #2904641) | Cod sursa (job #2535342) | Cod sursa (job #494353) | Cod sursa (job #3312463)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("asmax.in");
ofstream g ("asmax.out");
const int NMAX = 16000;
int n, V[NMAX+1], D[2][NMAX+1];
vector<int> G[NMAX+1];
void citire() {
f >> n;
for (int i=1; i<=n; i++)
f >> V[i];
int x, y;
for (int i=1; i<n; i++) {
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int nod, int tata) {
D[1][nod] = V[nod];
D[0][nod] = 0;
//
for (const auto &x : G[nod])
if (x != tata) {
DFS(x, nod);
D[1][nod] += max(D[1][x], 0);
D[0][nod] = max(D[0][nod], max(D[0][x], D[1][x]));
}
}
int main(){
citire();
DFS(1, 0);
g << max(D[1][1], D[0][1]);
//
f.close();
g.close();
return 0;
}