# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
435929 | cgiosy | Biochips (IZhO12_biochips) | C++17 | 1 ms | 204 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[N+1][500], 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;
}
function<int(int)> dfs=[&](int i) {
int n=0;
for(int e=G[i]; e; e=F[e]) {
int m=dfs(e), j=min(n+m, K);
while(n<j) D[i][n++]=-1e9;
while(--j) for(int k=min(m, j); k--;)
mx(D[i][j], D[i][j-k-1]+D[e][k]);
for(int k=0; k<m; k++) mx(D[i][k], D[e][k]);
}
if(n) mx(D[i][0], W[i]);
else D[i][n++]=W[i];
return n;
};
cout<<D[r][dfs(r)-1]<<'\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |