Submission #90154

#TimeUsernameProblemLanguageResultExecution timeMemory
90154mirbek01Biochips (IZhO12_biochips)C++17
0 / 100
16 ms20728 KiB
# include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int n, m, rt, a[N], tin[N], tout[N], timer, cur; long long dp1[505][N], dp2[505][N], pref[N], suff[N], x[N]; vector <int> g[N]; void dfs1(int v){ tin[v] = ++ timer; for(int to : g[v]){ dfs1(to); } tout[v] = timer; } void dfs(int v){ for(int to : g[v]){ dfs(to); } if(cur >= 3){ dp1[cur][tout[v]] = max(dp1[cur][tout[v]], dp1[cur - 2][tin[v] - 1] + dp2[cur - 2][tout[v] + 1] + x[v]); dp2[cur][tin[v]] = max(dp2[cur][tin[v]], dp1[cur - 2][tin[v] - 1] + dp2[cur - 2][tout[v] + 1] + x[v]); } else { dp1[cur][tout[v]] = max(dp1[cur][tout[v]], max(dp1[cur - 1][tin[v] - 1], dp2[cur - 1][tout[v] + 1]) + x[v]); dp2[cur][tin[v]] = max(dp2[cur][tin[v]], max(dp1[cur - 1][tin[v] - 1], dp2[cur - 1][tout[v] + 1]) + x[v]); } } int main(){ cin >> n >> m; for(int i = 1; i <= n; i ++){ int p; cin >> p >> x[i]; if(!p) rt = i; else { g[p].push_back(i); } } dfs1(rt); for(int i = 0; i <= m; i ++){ for(int j = 0; j < N; j ++) dp1[i][j] = dp2[i][j] = -1e18; } for(int i = 1; i <= n; i ++){ dp1[1][tout[i]] = max(x[i], dp1[1][tout[i]]); dp2[1][tin[i]] = max(x[i], dp2[1][tin[i]]); } for(int i = 2; i <= m; i ++){ cur = i; for(int j = 1; j <= n; j ++) dp1[i - 1][j] = max(dp1[i - 1][j - 1], dp1[i - 1][j]); for(int j = n; j >= 1; j --) dp2[i - 1][j] = max(dp2[i - 1][j + 1], dp2[i - 1][j]); dfs(rt); } long long ans = 0; for(int i = 1; i <= n; i ++) ans = max(ans, dp1[m][i]); cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...