# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37894 | alenam0161 | Biochips (IZhO12_biochips) | C++14 | 359 ms | 407076 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>
#define ad push_back
using namespace std;
const int N = 200007;
int cost[N];
vector<int> esim;
vector<int> g[N];
int ham[N];
void dfs(int v){
esim.ad(v);
for(int to:g[v])dfs(to);
ham[v]=esim.size()-1;
}
int dp[N][507];
int main(){
int root;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
int P,c;
scanf("%d%d",&P,&c);
if(P==0){
cost[i]=c;
root=i;
}
else{
cost[i]=c;
g[P].ad(i);
}
}
for(int i=1;i<=m;++i)dp[n][i]=-1e9;
dfs(root);
for(int i=esim.size()-1;i>=0;i--){
int to=esim[i];
for(int j=0;j<=min(m,(int)esim.size()-i);++j){
dp[i][j]=dp[i+1][j];
if(j)dp[i][j]=max(dp[i][j],cost[to]+dp[ham[to]+1][j-1]);
}
}
cout<<dp[0][m]<<endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |