#include <bits/stdc++.h>
#define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0)
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)1e5 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 7;
const int SQ = 107;
int n, m, t, k, tmp, tmp2, tmp3, tmp4, flag, u, v, w, ans;
int arr[MAXN], dp[MAXN][SQ], dp2[MAXN][SQ], sum[MAXN];
int ps[SQ][2][MAXN], pd[SQ][MAXN], ps2[SQ][2][MAXN], pd2[SQ][MAXN];
vector<int> adj[MAXN], vec[MAXN];
void DFSsz(int v, int p=-1) {
for (int u:adj[v]) {
if (u == p) continue;
DFSsz(u, v);
sum[v] += arr[u]; vec[v].pb(u);
}
for (int j=1; j<=k; j++) {
for (int u:vec[v]) {
dp[v][j] = max({dp[v][j], max(dp[u][j], dp2[u][j])});
dp2[v][j] = max({dp2[v][j], max(dp[u][j-1], dp2[u][j-1])+sum[v]});
}
}
for (int j=1; j<=k; j++) {
ps[j][0][0] = ps[j][1][vec[v].size()+1] = ps2[j][0][0] = ps2[j][1][vec[v].size()+1] = 0;
for (int i=1; i<=vec[v].size(); i++) {
int u = vec[v][i-1];
ps[j][0][i] = max(ps[j][0][i-1], max(dp[u][j], dp2[u][j]));
ps2[j][0][i] = max(ps2[j][0][i-1], max(dp[u][j-1], dp2[u][j-1]));
}
for (int i=vec[v].size(); i>=1; i--) {
int u = vec[v][i-1];
ps[j][1][i] = max(ps[j][1][i+1], max(dp[u][j], dp2[u][j]));
ps2[j][1][i] = max(ps2[j][1][i+1], max(dp[u][j-1], dp2[u][j-1]));
}
for (int i=1; i<=vec[v].size(); i++) {
int u = vec[v][i-1];
pd[j][u] = max({ps[j][0][i-1], ps[j][1][i+1]});
pd2[j][u] = max(ps2[j][0][i-1], ps2[j][1][i+1])+sum[v]-arr[u];
}
}
}
void Walk(int v, int p, vector<int> mul) {
for (int j=1; j<=k; j++) ans = max({ans, max(dp[v][j], dp2[v][j]+arr[p]), mul[j], mul[j-1]+sum[v]+arr[p]});
// cout << "! " << v << " " << merge(dp[v], mul).F << " " << merge(dp[v], mul).S << endl;
for (int u:adj[v]) {
if (u == p) continue;
vector<int> mul2(k+1);
for (int j=0; j<=k; j++) mul2[j] = max({mul[j], pd[j][u], pd2[j][u]+arr[p]});
Walk(u, v, mul2);
}
}
int32_t main() {
#ifdef LOCAL
freopen("inp.in", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> n >> k;
for (int i=1; i<=n; i++) cin >> arr[i];
for (int i=1; i<n; i++) {
cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
DFSsz(1);
Walk(1, 0, vector<int>(k+1, 0));
cout << ans << endl;
return 0;
}
Compilation message
chase.cpp: In function 'void DFSsz(long long int, long long int)':
chase.cpp:60:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i=1; i<=vec[v].size(); i++) {
| ~^~~~~~~~~~~~~~~
chase.cpp:74:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i=1; i<=vec[v].size(); i++) {
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
7 ms |
11868 KB |
Output is correct |
8 |
Incorrect |
3 ms |
6492 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
610 ms |
343632 KB |
Output is correct |
2 |
Correct |
620 ms |
343020 KB |
Output is correct |
3 |
Correct |
474 ms |
482368 KB |
Output is correct |
4 |
Incorrect |
124 ms |
153684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
7 ms |
11868 KB |
Output is correct |
8 |
Incorrect |
3 ms |
6492 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |