Pagini recente » Cod sursa (job #789286) | Cod sursa (job #2607460) | Cod sursa (job #490586) | Cod sursa (job #119173) | Cod sursa (job #1643435)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int Nmax = 16005;
vector <int> G[Nmax];
int n, Cost[Nmax], CostMax[Nmax];
int sol = INT_MIN;
int x,y;
void DFS(int nod, int tata)
{
for(int i = 0; i < (int)G[nod].size(); i++)
{
int v = G[nod][i];
if(v!=tata)
{
DFS(v,nod);
}
}
if(CostMax[nod] + Cost[nod] > Cost[nod]) CostMax[tata] += CostMax[nod] + Cost[nod];
}
int main()
{
in>>n;
for(int i = 1; i <= n; i++)
in>>Cost[i];
for(int i = 1; i < n; i++)
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
DFS(1,0);
for(int i = 1; i <= n; i++)
{
if(CostMax[i] + Cost[i] > sol) sol = CostMax[i] + Cost[i];
}
out<<sol;
return 0;
}