#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 25;
ll dp[MAXN][101], p[MAXN];
vector <int> adj[MAXN];
int n, v;
void fix (int pos, int par) {
if (par) {
adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), par));
}
for (auto j : adj[pos]) {
fix(j, pos);
}
}
ll ans (int pos, int cur) {
ll &ret = dp[pos][cur];
if (ret != -1) {
return ret;
}
ret = 0;
ll sum = 0;
for (auto j : adj[pos]) {
sum += p[j];
}
for (auto i : adj[pos]) {
ret = max(ret, ans(i, cur));
if (cur) ret = max(ret, sum + ans(i, cur - 1));
}
return ret;
}
void solve () {
cin >> n >> v;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(dp, -1, sizeof(dp));
fix(1, 0);
cout << ans(1, v) << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
82524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
82524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
90452 KB |
Output is correct |
2 |
Correct |
183 ms |
90456 KB |
Output is correct |
3 |
Correct |
32 ms |
85972 KB |
Output is correct |
4 |
Correct |
39 ms |
85680 KB |
Output is correct |
5 |
Correct |
112 ms |
85848 KB |
Output is correct |
6 |
Correct |
104 ms |
85840 KB |
Output is correct |
7 |
Correct |
111 ms |
85820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
82524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |