Pagini recente » Cod sursa (job #2426086) | Cod sursa (job #1399349) | Cod sursa (job #18413) | Cod sursa (job #128606) | Cod sursa (job #2425904)
#include <vector>
#include <fstream>
using namespace std;
const int NMAX = 16010;
int n, cost[NMAX], dp[NMAX], ans = -(1 << 30);
vector <int> graph[NMAX];
void DFS(int father, int node)
{
for (auto i : graph[node])
{
if (i != father)
{
DFS(node, i);
if (dp[i] > 0)
dp[node] += dp[i];
}
}
dp[node] += cost[node];
}
int main()
{
ifstream fin("asmax.in");
ofstream fout("asmax.out");
fin >> n;
for (int i = 1;i <= n;++i)
fin >> cost[i];
for (int i = 1;i < n;++i)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
DFS(0, 1);
for (int i = 1;i <= n;++i)
ans = max(ans, dp[i]);
fout << ans << "\n";
fin.close();
fout.close();
return 0;
}