Pagini recente » Cod sursa (job #1599092) | Cod sursa (job #3237188) | Cod sursa (job #922051) | Cod sursa (job #863694) | Cod sursa (job #3205281)
/*#include <bits/stdc++.h>
using namespace std;
//ifstream fin("asmax.in");
//ofstream fout("asmax.out");
const int MAX_SIZE = 16000;
const int MAX_COST = 1000;
vector<int> tree[MAX_SIZE + 1];
int cost[MAX_SIZE + 1];
int maxCost[MAX_SIZE + 1];
int maxSum = -MAX_COST;
void gen(int startNode, bool isVisited[MAX_SIZE + 1]) {
if (tree[startNode].size() == 1) {
maxCost[startNode] = cost[startNode];
return;
}
for (vector<int>::iterator it = tree[startNode].begin(); it < tree[startNode].end(); ++it) {
if (isVisited[*it] == 0) {
isVisited[*it] = 1;
gen(*it, isVisited);
if (maxCost[*it] + cost[startNode] > cost[startNode]) {
if (maxCost[startNode] != -MAX_COST * MAX_SIZE - 1) {
maxCost[startNode] = max(maxCost[*it] + maxCost[startNode], maxCost[startNode]);
} else {
maxCost[startNode] = max(maxCost[*it] + cost[startNode], maxCost[startNode]);
}
} else {
maxCost[startNode] = max(cost[startNode], maxCost[startNode]);
}
cout << startNode << ' ' << maxSum << '\n';
maxSum = max(maxCost[startNode], maxSum);*/
/* if (maxCost[*it] >= 0 && maxCost[startNode] >= 0 && costAdded[startNode] == 0) {
maxCost[startNode] += cost[startNode] + maxCost[*it];
costAdded[startNode] = 1;
} else if (maxCost[*it] >= 0 && maxCost[startNode] <= 0 && costAdded[startNode] == 0) {
maxCost[startNode] = cost[startNode] + maxCost[*it];
costAdded[startNode] = 1;
} else if (maxCost[*it] >= 0 && maxCost[startNode] >= 0 && costAdded[startNode] == 1) {
maxCost[startNode] += maxCost[*it];
} else if (maxCost[*it] >= 0 && maxCost[startNode] <= 0 && costAdded[startNode] == 1) {
maxCost[startNode] = cost[startNode] + maxCost[*it];
}
cout << startNode << ' ' << maxCost[startNode] << '\n';
maxSum = max(maxCost[startNode], maxSum);*/
/* }
}
}*/
/*int main() {
int noNodes;
cin >> noNodes;
for (int i = 1; i <= noNodes; ++i) {
cin >> cost[i];
maxSum = max(cost[i], maxSum);
maxCost[i] = -MAX_COST * MAX_SIZE - 1;
}
for (int i = 1; i < noNodes; ++i) {
int startNode, endNode;
cin >> startNode >> endNode;
tree[startNode].push_back(endNode);
tree[endNode].push_back(startNode);
}
bool isVisited[MAX_SIZE + 1] = {0};
gen(1, isVisited);
cout << maxSum;
return 0;
}*/
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int MAX_SIZE = 16000;
vector<int> graph[MAX_SIZE + 1];
int noNodes;
int cost[MAX_SIZE + 1];
int sum;
int maxSum;
int visitedNodes;
bool isSelected[MAX_SIZE + 1];
bool dfs(int startNode, int treeSize, bool isVisited[MAX_SIZE + 1]) {
for (vector<int>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
if (isVisited[*it] == 0 && isSelected[*it] == 1) {
isVisited[*it] = 1;
sum += cost[*it];
++visitedNodes;
dfs(*it, treeSize, isVisited);
}
}
}
void gen(int treeSize) {
for (int i = 1; i <= noNodes; ++i) {
if (isSelected[i] == 0) {
isSelected[i] = 1;
gen(treeSize + 1);
bool isVisited[MAX_SIZE + 1] = {0};
isVisited[i] = 1;
sum = cost[i];
visitedNodes = 1;
dfs(i, treeSize, isVisited);
if (visitedNodes == treeSize) {
maxSum = max(sum, maxSum);
}
isSelected[i] = 0;
}
}
}
void solve() {
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);
}
gen(1);
cout << maxSum;
}
int main() {
solve();
return 0;
}
/*
9
4 -1 1 -1 -2 10 -2 1 7
1 5
5 4
4 3
1 2
2 6
6 9
2 7
1 8
=>
21
*/