#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 5;
int n, m, root, par[maxn], val[maxn], in[maxn], out[maxn], timer = 0;
vector<int> graph[maxn], euler;
ll dp[maxn][505];
void dfs(int u) {
in[u] = timer++; euler.push_back(u);
for(int &v : graph[u]) dfs(v);
out[u] = timer - 1;
}
int main() {
cin >> n >> m;
for(int i=1; i<=n; i++) {
cin >> par[i] >> val[i];
if(par[i]) graph[par[i]].push_back(i);
else root = i;
}
dfs(root);
for(int i=n-1; i>=0; i--) {
for(int j=0; j<=m; j++) {
dp[i][j] = dp[i+1][j];
if(j) dp[i][j] = max(dp[i][j], dp[out[euler[i]]+1][j-1] + val[euler[i]]);
}
}
cout << dp[1][m] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Correct |
1 ms |
9052 KB |
Output is correct |
4 |
Correct |
8 ms |
43860 KB |
Output is correct |
5 |
Correct |
9 ms |
48476 KB |
Output is correct |
6 |
Correct |
11 ms |
48476 KB |
Output is correct |
7 |
Runtime error |
264 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Runtime error |
268 ms |
524288 KB |
Execution killed with signal 9 |
9 |
Runtime error |
271 ms |
524288 KB |
Execution killed with signal 9 |
10 |
Runtime error |
325 ms |
524288 KB |
Execution killed with signal 9 |