Submission #523698

#TimeUsernameProblemLanguageResultExecution timeMemory
523698knightron0Biochips (IZhO12_biochips)C++14
0 / 100
922 ms524292 KiB
#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 = 0, sz[MAXN], arr[MAXN]; int dfs(int s){ int curr = ++timer; arr[curr] = val[s]; sz[curr] = 1; for(auto u: adj[s]){ sz[curr] += dfs(u); } return sz[curr]; } 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 root = -1; 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); int dp[n+3][m+3]; for(int i =1;i<=n+1;i++){ dp[i][0] = 0; } for(int i =1;i<=m;i++){ dp[n+1][i] = -INF; for(int j= n;j>=1;j--){ dp[j][i] = max(dp[j+1][i], dp[j+sz[j]][i-1] + arr[j]); } } cout<<dp[1][m]<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...