#include <bits/stdc++.h> // chase_ceoi17
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 1e5+5;
const ll LLINF = 2e18;
int m, a[N];
ll sum[N], ans, dp[N][2][105], dp2[N][2][105];
vector<int> g[N];
inline bool chk(ll n) { return n > ans; }
inline void dfs(int x, int fa)
{
for (int v : g[x]) sum[x] += a[v];
dp[x][1][0] = dp2[x][1][0] = -LLINF;
//dp[x][1][i] = dp2[x][1][i] = sum[x];
for (int i=1; i <= m; ++i) dp[x][1][i] = dp2[x][1][i] = sum[x];
for (int v : g[x])
{
if (v == fa) continue;
dfs(v, x);
for (int i=1; i < m; ++i)
{
ans = max(ans,
max(dp[x][0][m-i], dp[x][1][m-i]) + max(dp2[v][0][i], dp2[v][1][i]-a[x]));
ans = max(ans,
max(dp2[x][0][m-i], dp2[x][1][m-i]-a[v]) + max(dp[v][0][i], dp[v][1][i]));
//ans = max(ans, max(dp[x][0][m-i], dp[x][1][m-i]) + dp2[v][t][i] - a[x]);
//ans = max(ans, max(dp2[x][0][m-i], dp2[x][1][m-i]) + dp[v][t][i]);
}
//debug(x), debug(v), debug(ans), endl;
for (int i=1; i <= m; ++i)
{
dp[x][0][i] = max({dp[x][0][i], dp[v][0][i], dp[v][1][i]});
dp2[x][0][i] = max({dp2[x][0][i], dp2[v][0][i], dp2[v][1][i] - a[x]});
dp[x][1][i] = max(dp[x][1][i], max(dp[v][0][i-1], dp[v][1][i-1]) + sum[x] - a[v]);
dp2[x][1][i] = max(dp2[x][1][i], max(dp2[v][0][i-1], dp2[v][1][i-1] - a[x]) + sum[x]);
}
ans = max({ans, dp[x][1][m], dp2[x][1][m]});
}
#ifdef TEST
debug(x), debug(sum[x]), debug(ans), endl;
for (int t=0; t < 2; ++t)
{
for (int i=0; i <= m; ++i) bug(dp[x][t][i]); endl;
for (int i=0; i <= m; ++i) bug(dp2[x][t][i]); endl;
} endl;
#endif
}
int main(void)
{ IO
int n, i;
cin >> n >> m;
for (i=1; i <= n; ++i) cin >> a[i];
for (i=0; i < n-1; ++i)
{
int x, y; cin >> x >> y;
g[x].pb(y), g[y].pb(x);
}
dfs(1, 0);
cout << ans << '\n';
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6800 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6800 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
345664 KB |
Output is correct |
2 |
Correct |
251 ms |
345624 KB |
Output is correct |
3 |
Correct |
143 ms |
338352 KB |
Output is correct |
4 |
Correct |
100 ms |
338012 KB |
Output is correct |
5 |
Correct |
236 ms |
337892 KB |
Output is correct |
6 |
Correct |
235 ms |
338032 KB |
Output is correct |
7 |
Correct |
248 ms |
338200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6800 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
287 ms |
345664 KB |
Output is correct |
14 |
Correct |
251 ms |
345624 KB |
Output is correct |
15 |
Correct |
143 ms |
338352 KB |
Output is correct |
16 |
Correct |
100 ms |
338012 KB |
Output is correct |
17 |
Correct |
236 ms |
337892 KB |
Output is correct |
18 |
Correct |
235 ms |
338032 KB |
Output is correct |
19 |
Correct |
248 ms |
338200 KB |
Output is correct |
20 |
Correct |
242 ms |
338064 KB |
Output is correct |
21 |
Correct |
97 ms |
338004 KB |
Output is correct |
22 |
Correct |
234 ms |
338328 KB |
Output is correct |
23 |
Correct |
93 ms |
338004 KB |
Output is correct |
24 |
Correct |
233 ms |
338060 KB |
Output is correct |
25 |
Correct |
142 ms |
337960 KB |
Output is correct |
26 |
Correct |
239 ms |
345724 KB |
Output is correct |
27 |
Correct |
232 ms |
345668 KB |
Output is correct |