Pagini recente » Cod sursa (job #2228217) | Cod sursa (job #672612) | Cod sursa (job #1336789) | Cod sursa (job #1760471) | Cod sursa (job #2829573)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
vector<int> cost;
vector< vector<int> > ad;
vector<int> maxCost;
int n;
vector<bool> vis;
void Read(){
int x, y;
fin >> n;
cost.resize(n+5);
vis.resize(n+5);
maxCost.resize(n+5, 0);
for(int i = 1; i <= n; ++i){
fin >> cost[i];
}
ad.resize(n+5);
for(int i = 1; i < n; ++i){
fin >> x >> y;
ad[x].push_back(y);
ad[y].push_back(x);
}
}
void DFS(int parent, int node){
vis[node] = 1;
maxCost[node] = cost[node];
for(int i = 0; i < ad[node].size(); ++i){
int w = ad[node][i];
if(vis[w] == 0)
DFS(node, w);
if(maxCost[w] > 0 && w != parent)
maxCost[node] += maxCost[w];
}
if(maxCost[node] < 0)
maxCost[node] = 0;
}
void Do(){
DFS(0, 1);
fout << maxCost[1] << " ";
/*for(int i = 1; i<=n; ++i)
cout << maxCost[i] << " ";*/
}
int main()
{
Read();
Do();
return 0;
}