#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100 , M = 500 + 100;
int n , m , root , T = 0;
vector<int> e(N) , in(N) , out(N) , ord = {0};
vector<vector<int>> g(N) , dp(N , vector<int>(M));
void dfs(int u){
in[u] = ++T;
ord.push_back(u);
for(int v : g[u]){
dfs(v);
}
out[u] = T;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i ++){
int p;
cin >> p >> e[i];
if(p > 0){
g[p].push_back(i);
}else{
root = i;
}
}
dfs(root);
for(int i = 0;i < N;i ++){
for(int j = 0;j < M;j ++){
dp[i][j] = -1e9;
}
}
dp[n + 1][0] = 0;
for(int i = n;i >= 1;i --){
for(int x = 1;x <= m;x ++){
dp[i][x] = max(dp[i + 1][x] , dp[out[ord[i]] + 1][x - 1] + e[ord[i]]);
}
}
cout << dp[1][m] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
485204 KB |
Output is correct |
2 |
Correct |
276 ms |
485028 KB |
Output is correct |
3 |
Incorrect |
277 ms |
485096 KB |
Output isn't correct |
4 |
Incorrect |
293 ms |
485456 KB |
Output isn't correct |
5 |
Incorrect |
290 ms |
485200 KB |
Output isn't correct |
6 |
Incorrect |
317 ms |
485200 KB |
Output isn't correct |
7 |
Incorrect |
399 ms |
487444 KB |
Output isn't correct |
8 |
Incorrect |
406 ms |
487376 KB |
Output isn't correct |
9 |
Incorrect |
441 ms |
487348 KB |
Output isn't correct |
10 |
Incorrect |
478 ms |
487372 KB |
Output isn't correct |