Pagini recente » Cod sursa (job #33515) | Cod sursa (job #1902615) | Cod sursa (job #899450) | Cod sursa (job #830559) | Cod sursa (job #2830413)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const int NMAX = 16003;
int costMax = -32000;
int dfs(int node, vector<bool> visited, vector<vector<int>> edges, vector<int>costs) {
visited[node] = true;
int cost = costs[node];
for(auto neighbour: edges[node])
{
if(!visited[neighbour] ){
cost = max(cost, cost + dfs(neighbour, visited, edges, costs));
}
}
if(cost > costMax )
costMax = cost;
return cost;
}
int main()
{
vector<int> costs;
vector<bool> visited;
vector<vector<int>> edges;
ifstream f("asmax.in");
ofstream g("asmax.out");
costs.push_back(0);
int n, x, y;
f >> n;
for(int i = 0; i< n; i++) {
f >> x;
costs.push_back(x);
}
edges.resize(n+1);
visited.resize(n+1, false);
for(int i = 0; i< n-1; i++) {
f >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
dfs(1,visited, edges, costs);
g << costMax;
}