Pagini recente » Cod sursa (job #570629) | Cod sursa (job #79262) | Cod sursa (job #108706) | Cod sursa (job #1971409) | Cod sursa (job #3157998)
#include <fstream>
#include <vector>
using namespace std;
ifstream in ("asmax.in");
ofstream out ("asmax.out");
const int max_size = 16e3 + 1;
int viz[max_size], dp[max_size], ans;
vector <int> edges[max_size];
void dfs (int nod)
{
viz[nod] = 1;
for (auto f : edges[nod])
{
if (viz[f] == 0)
{
dfs(f);
if (dp[f] > 0)
{
dp[nod] += dp[f];
}
}
}
ans = max(dp[nod], ans);
}
int main ()
{
int n;
in >> n;
for (int i = 1; i <= n; i++)
{
in >> dp[i];
}
for (int i = 1; i < n; i++)
{
int x, y;
in >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
ans = dp[1];
dfs(1);
out << ans;
in.close();
out.close();
return 0;
}