제출 #523694

#제출 시각아이디문제언어결과실행 시간메모리
523694knightron0바이오칩 (IZhO12_biochips)C++14
0 / 100
4 ms4940 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, tin[MAXN], tout[MAXN], sz[MAXN], arr[MAXN], idx[MAXN]; void dfs(int s){ tin[s] = ++timer; arr[timer] = val[s]; idx[s] = timer; for(auto u: adj[s]){ dfs(u); } tout[s] = 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 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); for(int i= 1;i<=n;i++){ sz[i] = tout[idx[i]]-tin[idx[i]]+1; } 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...