Submission #523499

#TimeUsernameProblemLanguageResultExecution timeMemory
523499knightron0Biochips (IZhO12_biochips)C++14
0 / 100
6 ms11212 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 = 4e5 + 5; vector<int> adj[MAXN]; int val[MAXN], timer = 1; int tin[MAXN], tout[MAXN], et[4*MAXN]; bool iftin[4*MAXN]; void dfs(int s){ tin[s] = timer; iftin[timer] = true; et[timer] = s; timer++; for(auto u: adj[s]){ dfs(u); } tout[s] = timer; et[timer] = s; iftin[timer] = false; 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 dp[(2*n)+3][2]; int root = -1; clr(iftin, 0); 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); dp[(2*n)+1][0] = 0; dp[(2*n)+1][1] = -INF; int ans = -INF; for(int i = 2*n;i>=1;i--){ dp[i][0] = 0; for(int j = 1;j<=m;j++){ if(iftin[i]){ if(tout[et[i]] == 2*n && j != 1){ dp[i][j%2] = dp[i+1][j%2]; continue; } dp[i][j%2] = max(dp[i+1][j%2], dp[tout[et[i]]+1][(j-1)%2]+val[et[i]]); } else { dp[i][j%2] = dp[i+1][j%2]; } } ans = max(ans, dp[i][m%2]); } cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...