This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//! 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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |