Submission #717444

# Submission time Handle Problem Language Result Execution time Memory
717444 2023-04-01T23:34:20 Z Ahmed57 Biochips (IZhO12_biochips) C++14
60 / 100
277 ms 197432 KB
#include <bits/stdc++.h>

using namespace std;
#define int long long
int en[200001];
vector<int> adj[200001];
long long cost[200001];
int n,m;int timer = 0;
vector<int> euler;
void dfs(int i,int pr){
    ++timer;
    euler.push_back(i);
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
    }
    en[i] = timer;
}
long long dp[20001][501];
long long solve(int i,int rem){
    if(i==n)return (rem==0?0:-1e18);
    if(dp[i][rem]!=-1)return dp[i][rem];
    long long c1 = solve(i+1,rem);
    if(rem)c1 = max(c1,solve(en[euler[i]],rem-1)+cost[euler[i]]);
    return dp[i][rem] = c1;
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    int ro = 0;
    for(int i = 0;i<n;i++){
        int a,b;
        cin>>a>>b;
        cost[i+1] = b;
        if(a==0)ro = i+1;
        else{
        adj[a].push_back(i+1);
        adj[i+1].push_back(a);
        }
    }
    dfs(ro,0);
    memset(dp,-1,sizeof dp);
    cout<<solve(0,m)<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 83412 KB Output is correct
2 Correct 33 ms 83420 KB Output is correct
3 Correct 32 ms 83460 KB Output is correct
4 Correct 49 ms 84496 KB Output is correct
5 Correct 56 ms 84556 KB Output is correct
6 Correct 58 ms 84600 KB Output is correct
7 Incorrect 198 ms 94508 KB Output isn't correct
8 Incorrect 183 ms 94560 KB Output isn't correct
9 Runtime error 244 ms 195328 KB Execution killed with signal 11
10 Runtime error 277 ms 197432 KB Execution killed with signal 11