Pagini recente » Cod sursa (job #1353458) | Cod sursa (job #3256341) | Cod sursa (job #2030289) | Cod sursa (job #2334386) | Cod sursa (job #1892691)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int Inf = 0x3f3f3f3f;
int n, vmax = -Inf;
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;
fin.close();
fout.close();
return 0;
}