Pagini recente » Cod sursa (job #2001906) | Cod sursa (job #4218) | Cod sursa (job #2967089) | Cod sursa (job #1749555) | Cod sursa (job #2420768)
#include <fstream>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
#define nmax 16001
#define max(a,b) a > b ? a : b
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, maxim = -1e9;
vector <int> a[nmax];
bool vizitat[nmax];
int s[nmax];
void citire()
{ int i, x, y;
fin >> n;
for(i = 1; i <= n; ++i)
fin >> s[i];
for(i = 1; i , n; ++i)
{
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
vizitat[x] = 1;
for(auto y : a[x])
{
if(!vizitat[y])
dfs(y);
if(s[y] > 0)
s[x] += s[y];
}
}
int main()
{ citire();
dfs(1);
int i;
for(i = 1; i <= n; ++i)
maxim = max(maxim, s[i]);
fout << maxim;
return 0;
}