This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)1e5 + 5;
const ll INF = (ll)1e18;
vector<int> adj[MAXN];
ll dp[2][105][MAXN];
ll neigh[MAXN];
int arr[MAXN];
int n, k;
ll ans;
void dfs(int v, int pr = -1) {
for (int to : adj[v]) {
if (to != pr) {
dfs(to, v);
}
}
for (int step = 0; step < 2; ++step) {
for (int i = 0; i <= k; ++i) {
dp[0][i][v] = dp[1][i][v] = -INF;
}
dp[0][0][v] = 0;
dp[1][1][v] = neigh[v];
for (int to : adj[v]) {
if (to != pr) {
for (int a = 0; a <= k; ++a) {
for (int b = 0; a + b <= k; ++b) {
ans = max(ans, dp[0][a][v] + dp[0][b][to]);
ans = max(ans, dp[1][a][v] + dp[0][b][to]);
ans = max(ans, dp[0][a][v] + dp[1][b][to] - arr[v]);
ans = max(ans, dp[1][a][v] + dp[1][b][to] - arr[v] - arr[to]);
}
}
for (int i = 0; i <= k; ++i) {
dp[0][i][v] = max(dp[0][i][v], dp[0][i][to]);
dp[0][i][v] = max(dp[0][i][v], dp[1][i][to]);
dp[1][i + 1][v] = max(dp[1][i + 1][v], dp[0][i][to] + neigh[v] - arr[to]);
dp[1][i + 1][v] = max(dp[1][i + 1][v], dp[1][i][to] + neigh[v] - arr[to]);
}
}
}
reverse(all(adj[v]));
}
}
void solve() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
neigh[u] += arr[v];
neigh[v] += arr[u];
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
printf("%lld\n", ans);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message (stderr)
chase.cpp: In function 'void solve()':
chase.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
chase.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
chase.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &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... |