//! The Leader Of Retards Bemola
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define sz(x) (ll) x.size()
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
ll Pow(ll a, ll b, ll md, ll ans = 1) {
for (; b; b >>= 1, a = a * a % md)
if (b & 1)
ans = ans * a % md;
return ans % md;
}
const ll MAXN = 1e5 + 10;
const ll MAXK = 101;
const ll INF = 8e18;
const ll MOD = 1e9 + 7;
ll P[MAXN], M[MAXN], n, ans = 0, k;
vector<ll> adj[MAXN]; pll dp[MAXN][MAXK], pd[MAXN][MAXK], idp[MAXN][MAXK], ipd[MAXN][MAXK];
void merge(ll v, ll u, ll x) {
if (dp[v][x].S < max(dp[u][x].F, dp[u][x - 1].F + M[u] - P[v])) dp[v][x].S = max(dp[u][x].F, dp[u][x - 1].F + M[u] - P[v]), idp[v][x].S = u;
if (dp[v][x].F < dp[v][x].S) swap(dp[v][x].F, dp[v][x].S), swap(idp[v][x].F, idp[v][x].S);
if (pd[v][x].S < max(pd[u][x].F, pd[u][x - 1].F + M[v] - P[u])) pd[v][x].S = max(pd[u][x].F, pd[u][x - 1].F + M[v] - P[u]), ipd[v][x].S = u;
if (pd[v][x].F < pd[v][x].S) swap(pd[v][x].F, pd[v][x].S), swap(ipd[v][x].F, ipd[v][x].S);
}
void DFS(ll v, ll p = -1) {
pd[v][1].F = M[v]; ipd[v][1].F = v;
for (ll u : adj[v]) {
if (u != p) {
DFS(u, v);
for (ll i = 1; i < MAXK; i++) {
merge(v, u, i);
}
}
}
for (ll i = 0; i <= k; i++) {
if (ipd[v][i].F == idp[v][k - i].F) ans = max({ans, pd[v][i].F + dp[v][k - i].S, pd[v][i].S + dp[v][k - i].F});
else ans = max(ans, pd[v][i].F + dp[v][k - i].F);
}
}
int main() {
scanf("%lld%lld", &n, &k);
for (ll i = 1; i <= n; i++) scanf("%lld", &P[i]);
for (ll i = 1; i < n; i++) {
ll u, v; scanf("%lld%lld", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (ll i = 1; i <= n; i++) {
for (ll u : adj[i]) M[i] += P[u];
}
DFS(1);
printf("%lld\n", ans);
return 0;
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
51 | scanf("%lld%lld", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
chase.cpp:52:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
52 | for (ll i = 1; i <= n; i++) scanf("%lld", &P[i]);
| ~~~~~^~~~~~~~~~~~~~~
chase.cpp:54:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
54 | ll u, v; scanf("%lld%lld", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
7 |
Correct |
7 ms |
7660 KB |
Output is correct |
8 |
Correct |
7 ms |
7532 KB |
Output is correct |
9 |
Correct |
6 ms |
5996 KB |
Output is correct |
10 |
Correct |
8 ms |
7404 KB |
Output is correct |
11 |
Correct |
7 ms |
7532 KB |
Output is correct |
12 |
Correct |
7 ms |
7532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
551 ms |
488804 KB |
Output is correct |
2 |
Correct |
552 ms |
488800 KB |
Output is correct |
3 |
Correct |
437 ms |
327256 KB |
Output is correct |
4 |
Correct |
552 ms |
484440 KB |
Output is correct |
5 |
Correct |
620 ms |
484836 KB |
Output is correct |
6 |
Correct |
612 ms |
484040 KB |
Output is correct |
7 |
Correct |
597 ms |
484964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
7 |
Correct |
7 ms |
7660 KB |
Output is correct |
8 |
Correct |
7 ms |
7532 KB |
Output is correct |
9 |
Correct |
6 ms |
5996 KB |
Output is correct |
10 |
Correct |
8 ms |
7404 KB |
Output is correct |
11 |
Correct |
7 ms |
7532 KB |
Output is correct |
12 |
Correct |
7 ms |
7532 KB |
Output is correct |
13 |
Correct |
551 ms |
488804 KB |
Output is correct |
14 |
Correct |
552 ms |
488800 KB |
Output is correct |
15 |
Correct |
437 ms |
327256 KB |
Output is correct |
16 |
Correct |
552 ms |
484440 KB |
Output is correct |
17 |
Correct |
620 ms |
484836 KB |
Output is correct |
18 |
Correct |
612 ms |
484040 KB |
Output is correct |
19 |
Correct |
597 ms |
484964 KB |
Output is correct |
20 |
Correct |
614 ms |
485404 KB |
Output is correct |
21 |
Correct |
548 ms |
484196 KB |
Output is correct |
22 |
Correct |
590 ms |
485864 KB |
Output is correct |
23 |
Correct |
546 ms |
485220 KB |
Output is correct |
24 |
Correct |
605 ms |
484324 KB |
Output is correct |
25 |
Correct |
436 ms |
326740 KB |
Output is correct |
26 |
Correct |
558 ms |
488804 KB |
Output is correct |
27 |
Correct |
566 ms |
488804 KB |
Output is correct |