Pagini recente » Cod sursa (job #1211784) | Cod sursa (job #1491197) | Cod sursa (job #2073627) | Cod sursa (job #1189412) | Cod sursa (job #2841845)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, m, maxim = -1;
vector<vector<int>> adjList;
vector<bool> vis;
vector<int> cost;
void read() {
fin >> n;
adjList.resize(n + 1);
vis.resize(n + 1);
cost.resize(n + 1);
for (int i = 1; i <= n; i++)
fin >> cost[i];
for (int i = 0; i < n; i++) {
int x, y;
fin >> x >> y;
adjList[x].push_back(y);
adjList[y].push_back(x);
}
}
void dfs(int node) {
vis[node] = true;
for (int i = 0; i < adjList[node].size(); i++) { // Eroare la auto??
int x = adjList[node][i];
if (!vis[x]) {
dfs(x);
if (cost[node] < cost[node] + cost[x])
cost[node] = cost[node] + cost[x];
}
}
maxim = max(maxim, cost[node]);
}
void printSol() {
dfs(1);
fout << maxim;
}
int main()
{
read();
printSol();
return 0;
}