#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, m;
int a[N], L[N], R[N], pos[N], dp[N][501];
vector<int> g[N];
int timer;
void dfs(int u) {
pos[L[u] = ++timer] = u;
for (int v : g[u]) {
dfs(v);
}
R[u] = timer;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
int rt = -1;
for (int i = 1; i <= n; ++i) {
int pr; cin >> pr >> a[i];
if (!pr) {
rt = i;
} else {
g[pr].push_back(i);
}
}
dfs(rt);
for (int i = 1; i <= n; ++i) {
int u = pos[i];
for (int j = 0; j <= m; ++j) {
dp[R[u] + 1][j + 1] = max(dp[R[u] + 1][j + 1], dp[i][j] + a[u]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[n][m];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
10 ms |
22876 KB |
Output is correct |
5 |
Correct |
12 ms |
25104 KB |
Output is correct |
6 |
Correct |
13 ms |
25152 KB |
Output is correct |
7 |
Correct |
200 ms |
299164 KB |
Output is correct |
8 |
Correct |
192 ms |
299092 KB |
Output is correct |
9 |
Correct |
223 ms |
363348 KB |
Output is correct |
10 |
Correct |
301 ms |
400980 KB |
Output is correct |