# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30847 | Kerim | Biochips (IZhO12_biochips) | C++14 | 853 ms | 405436 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 MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN],go[MAXN],cost[MAXN],dp[505][MAXN],TIM,id[MAXN];
vector<int>adj[MAXN];
void dfs(int nd){
int tm=++TIM;
id[tm]=nd;
tr(it,adj[nd])
dfs(*it);
go[tm]=TIM;
}
int main(){
//~ #ifndef ONLINE_JUDGE
//~ freopen("file.in", "r", stdin);
//~ #endif
int n,m,root;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int p;
scanf("%d%d",&p,&arr[i]);
if(p)
adj[p].pb(i);
else
root=i;
}dfs(root);
for(int i=1;i<=n;i++)
dp[1][i]=arr[id[i]];
for(int i=2;i<=m;i++){cost[n+1]=-INF;
for(int j=n;j>=1;j--)
cost[j]=max(cost[j+1],dp[i-1][j]);
for(int j=n;j>=1;j--)
umax(dp[i][j],cost[go[j]+1]+arr[id[j]]);
}
int ans=0;
for(int i=1;i<=n;i++)
umax(ans,dp[m][i]);
printf("%d\n",ans) ;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |