#include <bits/stdc++.h>
using namespace std;
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;
int D[2][200004], F[N+1], G[N+1]{}, W[N+1];
for(int i=1; i<=N; i++) {
int p;
cin>>p>>W[i];
if(p) F[i]=G[p], G[p]=i;
else r=i;
}
int n[2]{}, d=0;
function<int(int)> dfs=[&](int i) {
int p=n[d];
for(int e=G[i]; e; e=F[e]) {
d^=1;
int m=dfs(e);
d^=1;
int q=n[d^1]-=m, j=min(n[d]-p+m, K);
for(int k=n[d]; k<p+j; k++) D[d][k]=-1e9;
n[d]=p+j;
while(--j) for(int k=min(m, j); k--;)
mx(D[d][p+j], D[d][p+j-k-1]+D[d^1][q+k]);
for(int k=0; k<m; k++) mx(D[d][p+k], D[d^1][q+k]);
}
if(p==n[d]) D[d][n[d]++]=W[i];
else mx(D[d][p], W[i]);
return n[d]-p;
};dfs(r);
cout<<D[0][K-1]<<'\n';
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:7:12: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
7 | int N, K, r;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1868 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |