#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;
#define llong long long
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
// #define rand __rand
// mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); // or mt19937_64
// template<class T = int> T rand(T range = numeric_limits<T>::max()) {
// return (T)(rng() % range);
// }
#define maxn 200010
#define maxm 555
int n, m;
int a[maxn];
vector<int> gr[maxn];
vector<int> topo_a, prev_pos;
void dfs(int u) {
int t = len(topo_a);
for (auto v: gr[u]) {
dfs(v);
}
topo_a.push_back(a[u]);
prev_pos.push_back(t);
}
llong dp[maxn][maxm];
int main(void) {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
rep1(i, n) {
int p; cin >> p >> a[i];
gr[p].push_back(i);
}
dfs(0);
rep(i, m + 1) dp[0][i] = INT_MIN;
dp[0][0] = 0;
rep(i, n) {
memcpy(dp[i + 1], dp[i], sizeof(dp[i]));
rep1(f, m) {
dp[i + 1][f] = max(dp[i + 1][f], dp[prev_pos[i]][f - 1] + topo_a[i]);
}
}
cout << dp[n][m];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5500 KB |
Output is correct |
4 |
Correct |
40 ms |
44068 KB |
Output is correct |
5 |
Correct |
45 ms |
48888 KB |
Output is correct |
6 |
Correct |
45 ms |
48888 KB |
Output is correct |
7 |
Runtime error |
485 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |