Pagini recente » Cod sursa (job #2733597) | Cod sursa (job #2123929) | Cod sursa (job #1017709) | Cod sursa (job #2734347) | Cod sursa (job #2136167)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int N = 16001;
int n, sum[N];
bool viz[N];
vector <int> a[N];
void citire() {
int x, y;
in >> n;
for (int i = 1; i <= n; i++) in >> sum[i];
for (int i = 0; i < n - 1; i++) {
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
in.close();
}
void dfs(int x) {
viz[x] = true;
for (int i = 0; i < a[x].size(); i++) {
int y = a[x][i];
if (!viz[y]) {
dfs(y);
if (sum[y] > 0) sum[x] += sum[y];
}
}
}
int main()
{
int m = INT_MIN;
citire();
for (int i = 1; i <= n; i++) {
if (!viz[i]) dfs(i);
}
for (int i = 1; i <= n; i++) m = max(m, sum[i]);
//for (int i = 1; i <= n; i++) out << sum[i] << ' ';
//out << '\n';
out << m;
out.close();
}