Submission #1069344

# Submission time Handle Problem Language Result Execution time Memory
1069344 2024-08-21T19:57:26 Z VMaksimoski008 Biochips (IZhO12_biochips) C++17
90 / 100
442 ms 403400 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxn = 2e5 + 5;

int n, m, root, par[maxn], val[maxn], in[maxn], out[maxn], dp[maxn][505], timer = 0;
vector<int> graph[maxn], euler;

void dfs(int u) {
    in[u] = timer++; euler.push_back(u);
    for(int &v : graph[u]) dfs(v);
    out[u] = timer - 1;
}

int main() {
    cin >> n >> m;
    for(int i=1; i<=n; i++) {
        cin >> par[i] >> val[i];
        if(par[i]) graph[par[i]].push_back(i);
        else root = i;
    }

    dfs(root);
    for(int i=n-1; i>=0; i--) {
        for(int j=0; j<=m; j++) {
            dp[i][j] = max(dp[i+1][j], dp[out[euler[i]]+1][j]);
            if(j) dp[i][j] = max(dp[i][j], dp[out[euler[i]]+1][j-1] + val[euler[i]]);
        }
    }

    int ans = 0;
    for(int i=1; i<=n; i++) ans = max(ans, dp[i][m]);
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Incorrect 2 ms 4956 KB Output isn't correct
3 Correct 3 ms 5212 KB Output is correct
4 Correct 14 ms 22876 KB Output is correct
5 Correct 13 ms 25176 KB Output is correct
6 Correct 14 ms 25180 KB Output is correct
7 Correct 263 ms 300660 KB Output is correct
8 Correct 261 ms 300572 KB Output is correct
9 Correct 364 ms 365512 KB Output is correct
10 Correct 442 ms 403400 KB Output is correct