#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
11 ms |
22876 KB |
Output is correct |
5 |
Correct |
12 ms |
24892 KB |
Output is correct |
6 |
Correct |
16 ms |
25000 KB |
Output is correct |
7 |
Correct |
169 ms |
298864 KB |
Output is correct |
8 |
Correct |
176 ms |
299076 KB |
Output is correct |
9 |
Correct |
205 ms |
363348 KB |
Output is correct |
10 |
Correct |
241 ms |
402256 KB |
Output is correct |