# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
875621 | The_Samurai | Biochips (IZhO12_biochips) | C++17 | 2094 ms | 387344 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("Ofast,O3")
// #pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 2e9;
int n, k, root;
vector<vector<int>> dp, g;
void dfs(int u) {
int val = dp[1][u]; dp[1][u] = -inf;
dp[0][u] = 0;
for (int v: g[u]) {
dfs(v);
vector<int> copy(k + 1);
for (int i = 1; i <= k; i++) copy[i] = dp[i][u];
for (int i = 1; i <= k; i++) {
if (dp[i][v] == -inf) continue;
for (int j = i; j <= k; j++) {
if (copy[j - i] == -inf) continue;
dp[j][u] = max(dp[j][u], dp[i][v] + copy[j - i]);
}
}
}
dp[1][u] = max(dp[1][u], val);
// if (u != root) return;
}
void solve() {
cin >> n >> k;
g.assign(n + 1, {});
dp.assign(k + 1, vector<int>(n + 1, -inf));
for (int i = 1; i <= n; i++) {
int p;
cin >> p >> dp[1][i];
if (!p) root = i;
else g[p].emplace_back(i);
}
dfs(root);
cout << dp[k][root];
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |