Pagini recente » Cod sursa (job #185225) | Cod sursa (job #1319015) | Cod sursa (job #1995635) | Cod sursa (job #2416827) | Cod sursa (job #2676203)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <limits.h>
#include <algorithm>
using namespace std;
const int N = 16005;
int n, mx = INT_MIN;
vector<int> graf[N];
int cost[N];
bool amfost[N];
bool on[N];
void dfs(int nod) {
amfost[nod] = true;
for (int i = 0; i < graf[nod].size(); ++i) {
int to = graf[nod][i];
if(amfost[to] == false) {
dfs(to);
cost[nod] = max(cost[nod] + cost[to], cost[nod]);
}
}
amfost[nod] = false;
}
int main() {
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n;
fin >> n;
for (int i = 1; i <= n; ++i)
fin >> cost[i];
for (int i = 1; i < n; ++i) {
int a, b;
fin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
dfs(1);
for (int i = 1; i <= n; ++i) {
// cout << cost[i] << '\n';
mx = max(cost[i], mx);
}
//cout << '\n';
fout << mx << '\n';
return 0;
}