#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] - pigeons[to]);
}
}
}
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++) {
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 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
96 ms |
53432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |