Submission #992582

#TimeUsernameProblemLanguageResultExecution timeMemory
992582tch1cherinBiochips (IZhO12_biochips)C++17
100 / 100
496 ms395600 KiB
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200000; vector<int> graph[MAX_N]; int W[MAX_N]; int tin[MAX_N], tout[MAX_N], pos[MAX_N]; int cur = 0; void dfs(int u) { tin[u] = cur; pos[cur++] = u; for (int v : graph[u]) { dfs(v); } tout[u] = cur; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, M; cin >> N >> M; int root = -1; for (int i = 0; i < N; i++) { int p; cin >> p >> W[i]; p--; if (p != -1) { graph[p].push_back(i); } else { root = i; } } dfs(root); vector<vector<int>> dp(N + 1, vector<int>(M + 1, -1e9)); for (int i = 0; i <= N; i++) { dp[i][0] = 0; } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { dp[tout[pos[i]]][j + 1] = max(dp[tout[pos[i]]][j + 1], dp[i][j] + W[pos[i]]); } for (int j = 0; j <= M; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } cout << dp[N][M] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...