Pagini recente » Cod sursa (job #2222862) | Cod sursa (job #2017279) | Cod sursa (job #698458) | Cod sursa (job #3344634) | Cod sursa (job #3351900)
#include <fstream>
#include <vector>
using namespace std;
const int INF = 1e9;
void dfs(int x, vector <vector <int>> &a, vector <bool> &viz, vector <int> &val) {
viz[x] = true;
for (auto y: a[x]) {
if (!viz[y]) {
dfs(y, a, viz, val);
if (val[y] > 0) {
val[x] += val[y];
}
}
}
}
int main() {
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
in >> n;
vector <int> val(n + 1, -INF);
for (int i = 1; i <= n; i++) {
in >> val[i];
}
vector <vector <int>> a(n + 1);
vector <bool> viz(n + 1, false);
for (int i = 0; i < n - 1; i++) {
int x, y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1, a, viz, val);
in.close();
for (int i = 1; i <= n; i++) {
if (!viz[i]) {
dfs(i, a, viz, val);
}
}
int val_max = -INF;
for (auto x: val) {
val_max = max(val_max, x);
}
out << val_max << "\n";
out.close();
return 0;
}