# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896649 | 2024-01-01T20:12:03 Z | borisAngelov | Biochips (IZhO12_biochips) | C++17 | 149 ms | 524292 KB |
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int maxm = 505; const int inf = 1e9; int n, m; int a[maxn]; int root = -1; vector<int> tree[maxn]; int dp[maxn][maxm]; int tmpDP[maxn][maxm]; int subtreeSize[maxn]; void dfs(int node, int par) { subtreeSize[node] = 1; int upTo = 1; for (int i = 0; i < tree[node].size(); ++i) { if (tree[node][i] != par) { dfs(tree[node][i], node); subtreeSize[node] += subtreeSize[tree[node][i]]; upTo = min(m, subtreeSize[node]); for (int j = 1; j <= m; ++j) { tmpDP[node][j] = dp[node][j]; } for (int j = 0; j <= upTo; ++j) { for (int k = 0; k <= min(subtreeSize[tree[node][i]], upTo - j); ++k) { tmpDP[node][j + k] = max(tmpDP[node][j + k], dp[node][j] + dp[tree[node][i]][k]); } } for (int j = 1; j <= m; ++j) { dp[node][j] = tmpDP[node][j]; } } } dp[node][1] = max(dp[node][1], a[node]); } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n >> m; for (int i = 1; i <= n; ++i) { int par; cin >> par >> a[i]; if (par == 0) { root = i; } else { tree[par].push_back(i); tree[i].push_back(par); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { dp[i][j] = -inf; tmpDP[i][j] = -inf; } } dfs(root, -1); cout << dp[root][m] << endl; return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 10584 KB | Output is correct |
2 | Correct | 2 ms | 10588 KB | Output is correct |
3 | Correct | 2 ms | 12636 KB | Output is correct |
4 | Correct | 10 ms | 45912 KB | Output is correct |
5 | Correct | 11 ms | 50084 KB | Output is correct |
6 | Correct | 12 ms | 50008 KB | Output is correct |
7 | Runtime error | 137 ms | 524288 KB | Execution killed with signal 9 |
8 | Runtime error | 137 ms | 524292 KB | Execution killed with signal 9 |
9 | Runtime error | 144 ms | 524288 KB | Execution killed with signal 9 |
10 | Runtime error | 149 ms | 524288 KB | Execution killed with signal 9 |