# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
89842 | 2018-12-18T14:27:51 Z | Vardanyan | Biochips (IZhO12_biochips) | C++14 | 529 ms | 402792 KB |
#pragma GCC optimize "-O3" #include<bits/stdc++.h> using namespace std; const int N = 200*1000+5; const int M = 505; int dp[N][M]; int tin[N]; int tout[N]; int a[N]; int d[N]; vector<int> g[N]; int timer = 0; void dfs(int v){ tin[v] = ++timer; d[timer] = a[v]; dp[timer][1] = a[v]; for(int i = 0;i<g[v].size();i++){ int to = g[v][i]; dfs(to); } tout[tin[v]] = timer; } int main() { int n,m; scanf("%d%d",&n,&m); int root; for(int i = 1;i<=n;i++){ int x,y; scanf("%d%d",&x,&y); if(x == 0) root = i; if(x){ g[x].push_back(i); } a[i] = y; } dfs(root); for(int i = timer;i>=1;i--){ for(int j = 1;j<=m;j++){ //cout<<d[i]<<endl; dp[i][j] = max(dp[i][j],dp[i+1][j]); if(tout[i]+1<=timer) dp[i][j] = max(dp[i][j],dp[tout[i]+1][j-1]+d[i]); } } printf("%d\n",dp[1][m]); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 5112 KB | Output is correct |
2 | Correct | 6 ms | 5244 KB | Output is correct |
3 | Correct | 6 ms | 5380 KB | Output is correct |
4 | Correct | 22 ms | 22984 KB | Output is correct |
5 | Correct | 25 ms | 25220 KB | Output is correct |
6 | Correct | 30 ms | 25348 KB | Output is correct |
7 | Correct | 354 ms | 300132 KB | Output is correct |
8 | Correct | 374 ms | 300260 KB | Output is correct |
9 | Correct | 459 ms | 364948 KB | Output is correct |
10 | Correct | 529 ms | 402792 KB | Output is correct |