#include <bits/stdc++.h>
using namespace std;
int N, M, p[200005], x[200005], rt;
vector <int> son[200005];
int timer = 0, dp[200005][505];
void dfs(int u){
int pre = timer;
for (int v : son[u]) dfs(v);
timer++;
for (int i = 1; i <= M; i++)
dp[timer][i] = max(dp[pre][i-1]+x[u], dp[timer-1][i]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++){
cin >> p[i] >> x[i];
if (!p[i]) rt = i;
son[p[i]].push_back(i);
}
for (int i = 1; i <= M; i++) dp[0][i] = -1e9;
dfs(0);
cout << dp[timer][M];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
7 ms |
25436 KB |
Output is correct |
5 |
Correct |
9 ms |
27468 KB |
Output is correct |
6 |
Correct |
8 ms |
27484 KB |
Output is correct |
7 |
Correct |
129 ms |
299864 KB |
Output is correct |
8 |
Correct |
130 ms |
300088 KB |
Output is correct |
9 |
Correct |
162 ms |
364372 KB |
Output is correct |
10 |
Correct |
201 ms |
402260 KB |
Output is correct |