Pagini recente » Cod sursa (job #735192) | Cod sursa (job #2040985) | Cod sursa (job #2817769) | Cod sursa (job #603543) | Cod sursa (job #2194490)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
const int NMAX = 16e3 + 5;
long long sol;
vector <int> v[NMAX];
int viz[NMAX], cost[NMAX];
int get_max(int a, int b)
{
if(a > b)
return a;
return b;
}
void DFS(int nod)
{
viz[nod] = 1;
for(int i = 0; i < v[nod].size(); i++)
{
int fiu = v[nod][i];
if(viz[fiu] == 0)
{
DFS(fiu);
if(cost[nod] < cost[nod] + cost[fiu])
cost[nod] += cost[fiu];
sol = get_max(sol, cost[nod]);
}
}
}
int main()
{
int n;
f >> n;
for(int i = 1; i <= n; i++)
f >> cost[i];
for(int i = 1; i < n; i++)
{
int x, y;
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
DFS(1);
g << sol;
}