#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int dp[maxn][510];
vector<int> g[maxn];
int c[maxn];
int sz[maxn];
int n, m;
void dfs(int x) {
sz[x] = 0;
for (int i = 1; i <= m; ++i) dp[x][i] = -1e9;
for (int v : g[x]) {
dfs(v);
for (int i = min(m, sz[x]+sz[v]); i >= 1; --i) {
for (int j = max(1, i-sz[x]); j <= min(i, sz[v]); ++j) {
dp[x][i] = max(dp[x][i], dp[x][i-j] + dp[v][j]);
}
}
sz[x] += sz[v];
}
sz[x] += 1;
dp[x][1] = max(dp[x][1], c[x]);
}
int main() {
cin >> n >> m;
int root = 0;
for (int i = 1; i <= n; ++i) {
int pi;
cin >> pi >> c[i];
if (pi == 0) root = i;
else g[pi].push_back(i);
}
dfs(root);
cout << dp[root][m] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
25 ms |
23160 KB |
Output is correct |
5 |
Correct |
24 ms |
25324 KB |
Output is correct |
6 |
Correct |
24 ms |
25324 KB |
Output is correct |
7 |
Correct |
420 ms |
303436 KB |
Output is correct |
8 |
Correct |
414 ms |
303340 KB |
Output is correct |
9 |
Correct |
561 ms |
368364 KB |
Output is correct |
10 |
Correct |
672 ms |
406508 KB |
Output is correct |