Submission #523495

# Submission time Handle Problem Language Result Execution time Memory
523495 2022-02-07T17:43:07 Z knightron0 Biochips (IZhO12_biochips) C++14
0 / 100
296 ms 524292 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double

const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 2e5 + 5;

vector<int> adj[MAXN];
int val[MAXN], timer = 1;
int tin[MAXN], tout[MAXN], et[4*MAXN];
bool iftin[4*MAXN];

void dfs(int s){
    tin[s] = timer;
    iftin[timer] = true;
    et[timer] = s;
    timer++;
    for(auto u: adj[s]){
        dfs(u);
    }
    tout[s] = timer;
    et[timer] = s;
    iftin[timer] = false;
    timer++;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif
    int n, m;
    cin>>n>>m;
    int dp[(2*n)+3][m+4];
    int root = -1;
    clr(iftin, 0);
    for(int i= 1;i<=n;i++){
        int t1, t2;
        cin>>t1>>t2;
        val[i] = t2;
        if(t1 == 0){
            root = i;
            continue;
        } else {
            adj[t1].pb(i);
        }
    }
    dfs(root);
    dp[(2*n)+1][0] = 0;
    for(int j = 1;j<=m;j++){
        dp[(2*n)+1][j] = -INF;
    }
    int ans = -INF;
    for(int i = 2*n;i>=1;i--){
        dp[i][0] = 0;
        for(int j = 1;j<=m;j++){
            if(iftin[i]){
                dp[i][j] = max(dp[i+1][j], dp[tout[et[i]]+1][j-1]+val[et[i]]);
            } else {
                dp[i][j] = dp[i+1][j];
            }
        }
        ans = max(ans, dp[i][m]);
    }
    cout<<ans<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5768 KB Output is correct
2 Correct 4 ms 5780 KB Output is correct
3 Correct 4 ms 5788 KB Output is correct
4 Correct 17 ms 15228 KB Output is correct
5 Correct 17 ms 18784 KB Output is correct
6 Correct 22 ms 22636 KB Output is correct
7 Runtime error 296 ms 524292 KB Execution killed with signal 9
8 Halted 0 ms 0 KB -