Pagini recente » Cod sursa (job #2855211) | Cod sursa (job #1981148) | Cod sursa (job #3248829) | Cod sursa (job #387757) | Cod sursa (job #1048206)
#include <fstream>
#include <vector>
using namespace std;
ifstream in ("asmax.in");
ofstream out ("asmax.out");
const int N = 16005;
vector <int> a[N];
int n;
int cost[N];
int sum[N];
bool viz[N];
inline int maxim (int a, int b)
{
if (a > b)
return a;
return b;
}
void read()
{
in >> n;
for (int i = 1; i <= n; i++) {
in >> cost[i];
}
for (int i = 1; i < n; i++) {
int x, y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int nod)
{
viz[nod] = true;
sum[nod] = cost[nod];
for (int i = 0; i < a[nod].size(); i++) {
int y = a[nod][i];
if (!viz[y]) {
dfs(y);
if(sum[y] > 0) {
sum[nod] += sum[y];
}
}
}
}
int main()
{
read();
dfs(1);
int sumaMaxima = -1001;
for (int i = 1; i <= n; i++) {
sumaMaxima = maxim(sumaMaxima, sum[i]);
}
out << sumaMaxima << "\n";
return 0;
}