#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void chmax(long long &x, long long v) { if (x < v) x = v; }
int N, K;
int A[100001];
long long T[100000];
vector<int> G[100000];
long long down[100000][101];
long long dp[100000][101][2];
int to[100000][101];
void f(int x, int p) {
for (int t : G[x]) {
if (t == p) continue;
f(t, x);
rep(k, K+1) chmax(down[x][k], down[t][k]);
if (p != -1) rep(k, K) chmax(down[x][k+1], down[t][k]+T[x]-A[p]);
}
}
inline void update(int x, int k, long long val, int t) {
if (val > dp[x][k][0]) {
if (t == to[x][k]) dp[x][k][0] = val;
else {
dp[x][k][1] = dp[x][k][0];
dp[x][k][0] = val;
to[x][k] = t;
}
}
else if (val > dp[x][k][1]) {
dp[x][k][1] = val;
}
}
void g(int x, int p) {
if (p != -1) {
rep(k, K+1) {
long long m = 0;
int s = 0;
if (to[p][k] == x) s++;
update(x, k, dp[p][k][s], p);
if (k < K) update(x, k+1, dp[p][k][s] + T[p] - A[x], p);
}
}
for (int t : G[x]) {
if (t == p) continue;
rep(k, K+1) update(x, k, down[t][k], t);
}
for (int t : G[x]) {
if (t == p) continue;
g(t, x);
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
if (K == 0) {
cout << 0 << "\n";
return 0;
}
rep(i, N) cin >> A[i];
rep(i, N-1) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].pb(v);
G[v].pb(u);
}
rep(i, N) for (int t : G[i]) T[i] += A[t];
rep(i, N) rep(j, K+1) to[i][j] = -1;
f(0, -1);
g(0, -1);
long long m = 0;
rep(i, N) {
chmax(m, dp[i][K][0]);
chmax(m, dp[i][K-1][0] + T[i]);
}
cout << m << "\n";
return 0;
}
Compilation message
chase.cpp: In function 'void g(int, int)':
chase.cpp:63:17: warning: unused variable 'm' [-Wunused-variable]
long long m = 0;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
281864 KB |
Output is correct |
2 |
Correct |
0 ms |
281864 KB |
Output is correct |
3 |
Correct |
0 ms |
281864 KB |
Output is correct |
4 |
Correct |
0 ms |
281864 KB |
Output is correct |
5 |
Correct |
0 ms |
281864 KB |
Output is correct |
6 |
Correct |
0 ms |
281864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
281864 KB |
Output is correct |
2 |
Correct |
0 ms |
281864 KB |
Output is correct |
3 |
Correct |
0 ms |
281864 KB |
Output is correct |
4 |
Correct |
0 ms |
281864 KB |
Output is correct |
5 |
Correct |
0 ms |
281864 KB |
Output is correct |
6 |
Correct |
0 ms |
281864 KB |
Output is correct |
7 |
Correct |
0 ms |
281864 KB |
Output is correct |
8 |
Correct |
3 ms |
281864 KB |
Output is correct |
9 |
Correct |
0 ms |
281864 KB |
Output is correct |
10 |
Correct |
3 ms |
281864 KB |
Output is correct |
11 |
Correct |
0 ms |
281864 KB |
Output is correct |
12 |
Correct |
0 ms |
281864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
319 ms |
290328 KB |
Output is correct |
2 |
Correct |
319 ms |
290300 KB |
Output is correct |
3 |
Correct |
199 ms |
285416 KB |
Output is correct |
4 |
Correct |
213 ms |
285032 KB |
Output is correct |
5 |
Correct |
476 ms |
285032 KB |
Output is correct |
6 |
Correct |
406 ms |
285032 KB |
Output is correct |
7 |
Correct |
446 ms |
285032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
281864 KB |
Output is correct |
2 |
Correct |
0 ms |
281864 KB |
Output is correct |
3 |
Correct |
0 ms |
281864 KB |
Output is correct |
4 |
Correct |
0 ms |
281864 KB |
Output is correct |
5 |
Correct |
0 ms |
281864 KB |
Output is correct |
6 |
Correct |
0 ms |
281864 KB |
Output is correct |
7 |
Correct |
0 ms |
281864 KB |
Output is correct |
8 |
Correct |
3 ms |
281864 KB |
Output is correct |
9 |
Correct |
0 ms |
281864 KB |
Output is correct |
10 |
Correct |
3 ms |
281864 KB |
Output is correct |
11 |
Correct |
0 ms |
281864 KB |
Output is correct |
12 |
Correct |
0 ms |
281864 KB |
Output is correct |
13 |
Correct |
319 ms |
290328 KB |
Output is correct |
14 |
Correct |
319 ms |
290300 KB |
Output is correct |
15 |
Correct |
199 ms |
285416 KB |
Output is correct |
16 |
Correct |
213 ms |
285032 KB |
Output is correct |
17 |
Correct |
476 ms |
285032 KB |
Output is correct |
18 |
Correct |
406 ms |
285032 KB |
Output is correct |
19 |
Correct |
446 ms |
285032 KB |
Output is correct |
20 |
Correct |
423 ms |
285032 KB |
Output is correct |
21 |
Correct |
0 ms |
281864 KB |
Output is correct |
22 |
Correct |
433 ms |
285032 KB |
Output is correct |
23 |
Correct |
176 ms |
285032 KB |
Output is correct |
24 |
Correct |
493 ms |
285032 KB |
Output is correct |
25 |
Correct |
149 ms |
285416 KB |
Output is correct |
26 |
Correct |
346 ms |
290376 KB |
Output is correct |
27 |
Correct |
296 ms |
290376 KB |
Output is correct |