Submission #1325790

#TimeUsernameProblemLanguageResultExecution timeMemory
1325790aaaaaaaaBiochips (IZhO12_biochips)C++20
60 / 100
2113 ms309192 KiB
#include <bits/stdc++.h>
using namespace std;
const long long inf = -1e18;
const int mxN = 2e5 + 10;
const int mxM = 505;
vector<int> adj[mxN];
long long dp1[mxN][mxM], dp2[mxM], val[mxN];
int n, m, root = 0;
void dfs(int u){
    for(int j = 1; j <= m + 1; ++j) dp1[u][j] = inf, dp2[j] = inf;
    for(auto it : adj[u]) {
        dfs(it);
        for(int x = 0; x <= m; ++x){
            for(int y = 0; y <= m; ++y){
                if((x + y) > m) break;
                dp2[x + y] = max(dp2[x + y], dp1[u][x] + dp1[it][y]);
            }
        }
        for(int j = 0; j <= m; ++j){
            dp1[u][j] = dp2[j];
        }
    }
    dp1[u][1] = max(dp1[u][1], val[u]);
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> m;
    for(int i = 1, u; i <= n; ++i){
        cin >> u >> val[i];
        adj[u].push_back(i);
        if(u == 0) root = i;
    }
    dfs(root);
    cout << dp1[root][m] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...