// gs14004's code test
#include <bits/stdc++.h>
using namespace std;
int n,m,inp[200005];
int piv,sz[200005],a[200005];
vector<int> g[200005];
int root;
int dfs(int x){
int num = ++piv;
a[num] = inp[x];
sz[num] = 1;
for(int &i : g[x]){
sz[num] += dfs(i);
}
return sz[num];
}
int dp[2][200005];
void mx(int&a, int b) { a=max(a, b); }
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int N, K, r;
cin>>N>>K;n=N,m=K;
for(int i=1; i<=N; i++) {
int p;
cin>>p>>inp[i];
if(p) g[p].push_back(i);
else root=i;
}
dfs(root);
for (int i=1; i<=m; i++) {
dp[i%2][n+1] = -1e9;
for (int j=n; j; j--) {
dp[i%2][j] = max(dp[i%2][j+1],dp[(i-1)%2][j+sz[j]] + a[j]);
}
}
cout<<dp[m%2][1]<<'\n';
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:26:12: warning: unused variable 'r' [-Wunused-variable]
26 | int N, K, r;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
10 ms |
5288 KB |
Output is correct |
5 |
Correct |
10 ms |
5364 KB |
Output is correct |
6 |
Correct |
11 ms |
5324 KB |
Output is correct |
7 |
Correct |
137 ms |
9040 KB |
Output is correct |
8 |
Correct |
129 ms |
9032 KB |
Output is correct |
9 |
Correct |
209 ms |
9628 KB |
Output is correct |
10 |
Correct |
263 ms |
9996 KB |
Output is correct |