Pagini recente » Cod sursa (job #2351960) | Borderou de evaluare (job #1289655) | Borderou de evaluare (job #1648164) | Borderou de evaluare (job #2859991) | Cod sursa (job #2329230)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
static const int NMAX = 16005;
int n;
vector<int> v[NMAX];
vector<int> stack;
int val[NMAX];
int valMax;
bool visited[NMAX];
void Solve(int node)
{
visited[node] = true;
stack.push_back(node);
if(v[node].empty())
valMax = max(valMax, val[node]);
else{
for(auto child : v[node])
{
if(!visited[child]){
Solve(child);
val[node] = max(val[node], val[node] + val[stack.back()]);
valMax = max(valMax,val[node]);
stack.pop_back();
}
}
}
}
int main()
{
fin >> n;
for(int i =1; i<= n; ++i)
{
fin >> val[i];
}
int x, y;
for(int i = 1; i< n; ++i)
{
fin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
valMax= val[1];
Solve(1);
fout << valMax;
return 0;
}