Pagini recente » Cod sursa (job #420322) | Cod sursa (job #2474451) | Cod sursa (job #3211303) | Cod sursa (job #555873) | Cod sursa (job #3000907)
#include <fstream>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
const int NMAX = 16000;
int d[NMAX + 5], father[NMAX + 5];
vector <int> graph[NMAX + 5];
int dp[NMAX + 5];
void get_father(int node)
{
dp[node] = d[node];
for(auto neigh : graph[node])
if(neigh != father[node])
{
father[neigh] = node;
get_father(neigh);
if(dp[neigh] >= 0)
dp[node] += dp[neigh];
}
}
int main()
{
int n;
fin >> n;
for(int i = 1; i <= n; i++)
fin >> d[i];
for(int i = 1; i <= n - 1; i++)
{
int a, b;
fin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
get_father(1);
int maxim = -INT_MAX;
for(int i = 1; i <= n; i++)
maxim = max(maxim , dp[i]);
fout << maxim;
fin.close();
fout.close();
return 0;
}