#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9, K = 100 + 2;
int n, V;
int p[N];
vector<int> g[N];
ll dp[N][K], ans;
void dfs(int u, int par) {
for (int j = 1; j <= V; ++j)
ans = max(ans, dp[u][j]);
ll s(0);
for (int v : g[u])
if (v != par) s += p[v];
for (int v : g[u]) if (v != par) {
dp[v][0] = 0;
for (int j = 1; j <= V; ++j) {
dp[v][j] = max(dp[u][j], dp[u][j - 1] + s);
}
dfs(v, u);
}
}
void calc(int u) {
fill(dp[u], dp[u] + V + 1, 0);
dfs(u, 0);
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> V;
for (int i = 1; i <= n; ++i) cin >> p[i];
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (n <= 1000) {
for (int i = 1; i <= n; ++i) calc(i);
}
else calc(1);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
/*
==================================================+
INPUT: |
--------------------------------------------------|
12 1
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
36
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
225 ms |
3560 KB |
Output is correct |
8 |
Correct |
27 ms |
3560 KB |
Output is correct |
9 |
Correct |
19 ms |
3468 KB |
Output is correct |
10 |
Correct |
215 ms |
3532 KB |
Output is correct |
11 |
Correct |
93 ms |
3516 KB |
Output is correct |
12 |
Correct |
39 ms |
3516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
90928 KB |
Output is correct |
2 |
Correct |
108 ms |
92816 KB |
Output is correct |
3 |
Correct |
75 ms |
88516 KB |
Output is correct |
4 |
Correct |
83 ms |
88192 KB |
Output is correct |
5 |
Correct |
123 ms |
88204 KB |
Output is correct |
6 |
Correct |
113 ms |
88184 KB |
Output is correct |
7 |
Correct |
112 ms |
88180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
225 ms |
3560 KB |
Output is correct |
8 |
Correct |
27 ms |
3560 KB |
Output is correct |
9 |
Correct |
19 ms |
3468 KB |
Output is correct |
10 |
Correct |
215 ms |
3532 KB |
Output is correct |
11 |
Correct |
93 ms |
3516 KB |
Output is correct |
12 |
Correct |
39 ms |
3516 KB |
Output is correct |
13 |
Correct |
102 ms |
90928 KB |
Output is correct |
14 |
Correct |
108 ms |
92816 KB |
Output is correct |
15 |
Correct |
75 ms |
88516 KB |
Output is correct |
16 |
Correct |
83 ms |
88192 KB |
Output is correct |
17 |
Correct |
123 ms |
88204 KB |
Output is correct |
18 |
Correct |
113 ms |
88184 KB |
Output is correct |
19 |
Correct |
112 ms |
88180 KB |
Output is correct |
20 |
Incorrect |
111 ms |
88204 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |