Pagini recente » Cod sursa (job #1775527) | Cod sursa (job #2573249) | Cod sursa (job #1315039) | Cod sursa (job #2343143) | Cod sursa (job #2709251)
#include <fstream>
#include <set>
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
const int N = 16005, oo = 2e9;
vector <int> L[N];
bitset <N> vis;
int dp[N], cost[N], n;
void Read()
{
ifstream fin("asmax.in");
fin >> n;
for (int i = 1; i <= n; i++)
fin >> cost[i];
for (int i = 1, x, y; i < n; i++)
{
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
fin.close();
}
void DFS(int vertex)
{
vis[vertex] = 1;
dp[vertex] = cost[vertex];
for (auto next : L[vertex])
{
if (!vis[next])
{
DFS(next);
if (dp[next] > 0)
dp[vertex] += dp[next];
}
}
}
void Solve()
{
ofstream fout("asmax.out");
DFS(1);
int ans = dp[1];
for (int i = 2; i <= n; i++)
ans = max(ans, dp[i]);
fout << ans << "\n";
fout.close();
}
int main()
{
Read();
Solve();
return 0;
}