| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 339462 | Vladikus004 | Biochips (IZhO12_biochips) | C++14 | 553 ms | 402280 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define inf 2e9
#define ff first
#define ss second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int N = 200000 + 1, M = 500 + 1;
int n, m, sz[N], w[N], dp[N][M];
vector <int> v[N], ord;
void dfs(int x){
    ord.pb(x);
    for (auto u: v[x]){
        dfs(u);
        sz[x] += sz[u];
    }
    sz[x]++;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    freopen("input.txt", "r", stdin);
    cin >> n >> m;
    int st;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x >> w[i];
        if (x)
            v[x-1].pb(i);
        else st = i;
    }
    dfs(st);
    memset(dp, -1, sizeof dp);
    for (int i = 0; i <= ord.size(); i++)
        dp[i][0] = 0;
    int ans = 0;
    for (int i = ord.size()-1; i >= 0; i--){
        for (int j = 1; j <= m; j++){
            if (dp[i+sz[ord[i]]][j-1] != -1)
                dp[i][j] = dp[i+sz[ord[i]]][j-1] + w[ord[i]];
            dp[i][j] = max(dp[i][j], dp[i + 1][j]);
        }
        ans = max(ans, dp[i][m]);
    }
    cout << ans;
    return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
