# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896639 | 2024-01-01T19:58:55 Z | borisAngelov | Biochips (IZhO12_biochips) | C++17 | 2000 ms | 415876 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 subtreeSize[maxn]; int dp[maxn][maxm]; vector<int> childs[maxn]; void dfs(int node, int par) { subtreeSize[node] = 1; for (int i = 0; i < tree[node].size(); ++i) { if (tree[node][i] != par) { childs[node].push_back(tree[node][i]); dfs(tree[node][i], node); subtreeSize[node] += subtreeSize[tree[node][i]]; } } if (subtreeSize[node] != 1) { for (int i = 0; i < childs[node].size(); ++i) { for (int j = m; j >= 1; --j) { for (int k = m; k >= j; --k) { if (dp[node][k - j] == -inf || dp[childs[node][i]][j] == -inf) { continue; } //cout << node << " " << k << " " << j << " " << childs[node][i] << " " << dp[node][k - j] << " " << dp[childs[node][i]][j] << endl; dp[node][k] = max(dp[node][k], dp[node][k - j] + dp[childs[node][i]][j]); } } } } else { dp[node][1] = a[node]; } /*cout << node << endl; for (int i = 1; i <= m; ++i) { cout << i << " :: " << dp[node][i] << endl; } cout << "----------------------------------------------" << endl;*/ } 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; } } if (m == 1) { int ans = 0; for (int i = 1; i <= n; ++i) { ans = max(ans, a[i]); } cout << ans << endl; return 0; } dfs(root, -1); cout << dp[root][m] << endl; return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 3 ms | 13912 KB | Output isn't correct |
2 | Incorrect | 3 ms | 13916 KB | Output isn't correct |
3 | Incorrect | 4 ms | 13916 KB | Output isn't correct |
4 | Incorrect | 28 ms | 30888 KB | Output isn't correct |
5 | Incorrect | 46 ms | 32856 KB | Output isn't correct |
6 | Incorrect | 61 ms | 33032 KB | Output isn't correct |
7 | Execution timed out | 2023 ms | 313692 KB | Time limit exceeded |
8 | Execution timed out | 2033 ms | 313428 KB | Time limit exceeded |
9 | Execution timed out | 2062 ms | 378228 KB | Time limit exceeded |
10 | Execution timed out | 2094 ms | 415876 KB | Time limit exceeded |