#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(rt);
cout << dp[timer][M];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
7 ms |
25436 KB |
Output is correct |
5 |
Correct |
7 ms |
27492 KB |
Output is correct |
6 |
Correct |
10 ms |
27484 KB |
Output is correct |
7 |
Correct |
129 ms |
299900 KB |
Output is correct |
8 |
Correct |
135 ms |
299908 KB |
Output is correct |
9 |
Correct |
162 ms |
364536 KB |
Output is correct |
10 |
Correct |
202 ms |
402044 KB |
Output is correct |