# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
435954 | cgiosy | Biochips (IZhO12_biochips) | C++17 | 204 ms | 10776 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.
// gs14004's code test
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
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];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
for (int i=1, w[200005]; i<=n; i++) {
int p;
cin>>p>>w[i];inp[i]=w[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];
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |