Pagini recente » Cod sursa (job #1020653) | Cod sursa (job #2062802) | Cod sursa (job #1238301) | Rating Mihai Barbu (mihaibarbu) | Cod sursa (job #2025998)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int oo = 1e9;
int n;
vector<int> d, cost;
vector<vector<int> > v;
vector<bool> viz;
int mx = -oo;
inline void read() {
fin >> n;
cost = vector<int>(n + 1);
d = vector<int>(n + 1, -oo);
v = vector<vector<int> >(n + 1);
viz = vector<bool>(n + 1);
for (int i = 1; i <= n; ++i)
fin >> cost[i];
int x, y;
for (int i = 1; i < n; ++i) {
fin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
}
void dfs(int node) {
viz[node] = 1;
d[node] = cost[node];
for (const int& newNode: v[node]) {
if (!viz[newNode]) {
dfs(newNode);
d[node] = max(d[node], d[newNode] + d[node]);
}
}
mx = max(mx, d[node]);
}
int main()
{
read();
dfs(1);
fout << mx;
fin.close();
fout.close();
return 0;
}