#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 sum_p[N];
ll dp1[N][K]; // dp1[u][j]: bắt đầu tại u, sẽ dùng j lượt
void dfs_1(int u, int par) {
for (int j = 0; j <= V; ++j)
dp1[u][j] = 0;
for (int v : g[u]) if (v != par) {
dfs_1(v, u);
for (int j = 1; j <= V; ++j) {
dp1[u][j] = max({ dp1[u][j], dp1[v][j], dp1[v][j - 1] + sum_p[v] - p[u] });
}
}
}
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);
}
for (int u = 1; u <= n; ++u)
for (int v : g[u]) sum_p[u] += p[v];
ll ans(0);
if (n <= 1000) {
for (int i = 1; i <= n; ++i) {
dfs_1(i, 0);
for (int j = 1; j <= V; ++j) {
ans = max(ans, dp1[i][j]);
if (j < V) ans = max(ans, dp1[i][j] + sum_p[i]);
}
}
}
else {
dfs_1(1, 0);
for (int j = 1; j <= V; ++j) {
ans = max(ans, dp1[1][j]);
if (j < V) ans = max(ans, dp1[1][j] + sum_p[1]);
}
}
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
/*
==================================================+
INPUT: |
--------------------------------------------------|
12 2
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 |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
91476 KB |
Output is correct |
2 |
Correct |
113 ms |
91452 KB |
Output is correct |
3 |
Correct |
77 ms |
87220 KB |
Output is correct |
4 |
Incorrect |
77 ms |
86852 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |