# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
435933 | cgiosy | Biochips (IZhO12_biochips) | C++17 | 2 ms | 1868 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
while(n[d]<p+j) D[d][n[d]++]=-1e9;
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;
};
cout<<D[0][dfs(r)-1]<<'\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |