Pagini recente » Cod sursa (job #2534673) | Cod sursa (job #2110756) | Cod sursa (job #1067397) | Cod sursa (job #942919) | Cod sursa (job #2928995)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;
#if 1
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define cin fin
#define cout fout
#endif
const int nmx = 16e3+1;
vi a[nmx];
int v[nmx],dp[nmx];
void Dfs(int k,int t)
{
for (int to : a[k])
{
if (to != t)
{
Dfs(to,k);
dp[k] += max(0,dp[to]);
}
}
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> v[i], dp[i] = v[i];
for (int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
a[x].emplace_back(y);
a[y].emplace_back(x);
}
int mx = INT_MIN;
Dfs(1, 0);
for (int i = 1; i <= n; i++)
mx = max(dp[i], mx);
cout << mx;
}