Pagini recente » Cod sursa (job #2520302) | Cod sursa (job #1879632) | Cod sursa (job #1781710) | Cod sursa (job #1087241) | Cod sursa (job #2655138)
#include <fstream>
#include <vector>
#define N 16005
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
int cost[N];
bool vizitat[N];
vector<vector<int> >graf(N);
int dfs(int nod){
vizitat[nod] = true;
for(size_t i = 0; i < graf[nod].size(); ++i)
if(!vizitat[graf[nod][i]])
cost[nod] += dfs(graf[nod][i]);
if(cost[nod] < 0) return 0;
else return cost[nod];
}
int main()
{
in>>n;
for(int i = 1; i <= n; ++i)
in>>cost[i];
for(int i = 1; i <= n - 1; ++i){
int x, y;
in>>x>>y;
graf[x].push_back(y);
graf[y].push_back(x);
}
dfs(1);
int costMax = cost[1];
for(int i = 2; i <= n; ++i)
costMax = max(costMax, cost[i]);
out<<costMax<<" ";
in.close();
out.close();
return 0;
}