Pagini recente » Cod sursa (job #1121324) | Cod sursa (job #757399) | Istoria paginii runda/dij/clasament | Cod sursa (job #170352) | Cod sursa (job #3197985)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int MAX_LENGTH = 16000;
const int MAX_COST = 16000000;
int noNodes;
int cost[MAX_LENGTH + 1], maxNodeCost[MAX_LENGTH + 1];
vector<int> graph[MAX_LENGTH + 1];
void gen(int startNode, int &maxCost, bool isVisited[MAX_LENGTH + 1], int ¤tCost) {
for (vector<int>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
if (isVisited[*it] == 0) {
isVisited[*it] = 1;
currentCost += cost[*it];
maxCost = max(currentCost, maxCost);
gen(*it, maxCost, isVisited, currentCost);
}
}
}
int main() {
fin >> noNodes;
for (int i = 1; i <= noNodes; ++i) {
fin >> cost[i];
}
for (int i = 1; i < noNodes; ++i) {
int startNode, endNode;
fin >> startNode >> endNode;
graph[startNode].push_back(endNode);
graph[endNode].push_back(startNode);
}
int maxCost = -MAX_COST - 1;
for (int i = 1; i <= noNodes; ++i) {
bool isVisited[MAX_LENGTH + 1] = {0};
isVisited[i] = 1;
int currentCost = cost[i];
gen(i, maxCost, isVisited, currentCost);
}
fout << maxCost;
return 0;
}