#include <bits/stdc++.h>
using namespace std;
#define max2(x, y) (x > y ? x : y)
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] = max2(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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
8 ms |
25436 KB |
Output is correct |
5 |
Correct |
7 ms |
27484 KB |
Output is correct |
6 |
Correct |
8 ms |
27456 KB |
Output is correct |
7 |
Correct |
128 ms |
300024 KB |
Output is correct |
8 |
Correct |
125 ms |
299860 KB |
Output is correct |
9 |
Correct |
161 ms |
364504 KB |
Output is correct |
10 |
Correct |
207 ms |
402256 KB |
Output is correct |