Submission #435951

# Submission time Handle Problem Language Result Execution time Memory
435951 2021-06-24T02:31:53 Z cgiosy Biochips (IZhO12_biochips) C++17
100 / 100
211 ms 9956 KB
#include <cstdio>
#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(){
    scanf("%d %d",&n,&m);
    for (int i=1; i<=n; i++) {
        int p, w;
        scanf("%d %d",&p,&w);inp[i]=w;
        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]);
        }
    }
    printf("%d",dp[m%2][1]);
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     scanf("%d %d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
biochips.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         scanf("%d %d",&p,&w);inp[i]=w;
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 8 ms 5324 KB Output is correct
5 Correct 9 ms 5196 KB Output is correct
6 Correct 9 ms 5316 KB Output is correct
7 Correct 117 ms 8968 KB Output is correct
8 Correct 116 ms 9012 KB Output is correct
9 Correct 205 ms 9584 KB Output is correct
10 Correct 211 ms 9956 KB Output is correct