Pagini recente » Cod sursa (job #2955804) | Cod sursa (job #666772) | Cod sursa (job #1639221) | Cod sursa (job #2393096) | Cod sursa (job #1892684)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int Inf = 0x3f3f3f3f;
int n, vmax;
vector<int> A, D;
vector<bool> P;
vector<vector<int>> G;
void Dfs(int x) {
P[x] = true;
D[x] = A[x];
for(auto y : G[x])
if(!P[y]) {
Dfs(y);
D[x] = max(D[x], D[y] + D[x]);
}
vmax = max(vmax, D[x]);
}
int main()
{
fin >> n;
A = vector<int> (n + 1);
D = vector<int> (n + 1, -Inf);
P = vector<bool> (n + 1);
G = vector<vector<int>> (n + 1);
for(int i = 1; i <= n; ++i)
fin >> A[i];
int x, y;
for(int i = 1; i < n; ++i) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
Dfs(1);
fout << vmax;
return 0;
}