#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;
for(int k=0; k<m; k++) mx(D[d][p+k], D[d^1][q+k]);
n[d]=p+j;
while(--j>0) for(int k=min(m, j); k--;)
mx(D[d][p+j], D[d][p+j-k-1]+D[d^1][q+k]);
}
if(p==n[d]) D[d][n[d]++]=W[i];
else mx(D[d][n[d]], W[i]);
return n[d]-p;
};
cout<<D[0][dfs(r)-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;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |