#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1e5 + 7;
ll pigeons[N], subtree[N];
int n, bread;
vector <int> graph[N];
ll dp[N][103];
void dfs(int v, int p) {
subtree[v] = 0;
for (auto to : graph[v]) {
if (to == p) continue;
subtree[v] += pigeons[to];
dfs(to, v);
for (int j = 0; j <= bread; j++) {
dp[v][j] = max(dp[v][j], dp[to][j]);
}
}
for (auto to : graph[v]) {
if (to == p) continue;
for (int j = 1; j <= bread; j++) {
dp[v][j] = max(dp[v][j], dp[to][j - 1] + subtree[v]);
}
}
}
void solve() {
cin >> n >> bread;
for (int i = 1; i <= n; i++) {
cin >> pigeons[i];
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= bread; k++) {
dp[j][k] = -2e18;
}
dp[j][0] = 0;
}
dfs(i, i);
for (int j = 0; j <= bread; j++) {
ans = max(ans, dp[i][j]);
}
if (n > 1000) break;
}
cout << ans << endl;
}
/*
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
*/
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
296 ms |
3596 KB |
Output is correct |
8 |
Correct |
36 ms |
3588 KB |
Output is correct |
9 |
Correct |
37 ms |
3540 KB |
Output is correct |
10 |
Correct |
300 ms |
3544 KB |
Output is correct |
11 |
Correct |
145 ms |
3540 KB |
Output is correct |
12 |
Correct |
56 ms |
3544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
93856 KB |
Output is correct |
2 |
Correct |
115 ms |
95528 KB |
Output is correct |
3 |
Correct |
87 ms |
90436 KB |
Output is correct |
4 |
Correct |
86 ms |
90176 KB |
Output is correct |
5 |
Correct |
151 ms |
90052 KB |
Output is correct |
6 |
Correct |
158 ms |
90044 KB |
Output is correct |
7 |
Correct |
144 ms |
90248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
296 ms |
3596 KB |
Output is correct |
8 |
Correct |
36 ms |
3588 KB |
Output is correct |
9 |
Correct |
37 ms |
3540 KB |
Output is correct |
10 |
Correct |
300 ms |
3544 KB |
Output is correct |
11 |
Correct |
145 ms |
3540 KB |
Output is correct |
12 |
Correct |
56 ms |
3544 KB |
Output is correct |
13 |
Correct |
128 ms |
93856 KB |
Output is correct |
14 |
Correct |
115 ms |
95528 KB |
Output is correct |
15 |
Correct |
87 ms |
90436 KB |
Output is correct |
16 |
Correct |
86 ms |
90176 KB |
Output is correct |
17 |
Correct |
151 ms |
90052 KB |
Output is correct |
18 |
Correct |
158 ms |
90044 KB |
Output is correct |
19 |
Correct |
144 ms |
90248 KB |
Output is correct |
20 |
Incorrect |
131 ms |
90084 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |