Pagini recente » Cod sursa (job #2675942) | Cod sursa (job #2971894)
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
void DFS(int nod, vector< vector<int> > &Best, vector< vector<int> > &List, vector<int> &Cost, int p)
{
Best[nod][1] = Cost[nod];
for (int i = 0; i < List[nod].size(); i++)
{
if (List[nod][i] == p)
continue;
DFS(List[nod][i], Best, List, Cost, nod);
Best[nod][1] += max(Best[ List[nod][i] ][1], 0);
Best[nod][0] = max(Best[ List[nod][i] ][0], Best[nod][0]);
}
Best[nod][0] = max(Best[nod][0], 0);
Best[nod][0] = max(Best[nod][0], Best[nod][1]);
}
int main()
{
int n;
in>> n;
vector<int> Cost(n);
vector< vector<int> > v(n);
for (int i = 0; i < n; i++)
in>> Cost[i];
for (int i = 0; i < n - 1; i++)
{
int a, b;
in>> a >> b; a--; b--;
v[a].push_back(b);
v[b].push_back(a);
}
vector< vector<int> > Best(n, vector<int> (2, 0));
DFS(0, Best, v, Cost, -1);
out<< Best[0][0];
return 0;
}