# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112221 | Kirill22 | Biochips (IZhO12_biochips) | C++17 | 447 ms | 405576 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.
#include "bits/stdc++.h"
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
vector<int> a(n);
int rt;
for (int i = 0; i < n; i++) {
int p;
cin >> p >> a[i];
p--;
if (p == -1) {
rt = i;
} else {
g[p].push_back(i);
}
}
vector<int> tin(n), tout(n);
vector<vector<pair<int, int>>> go(n);
int T = 0;
auto dfs = [&] (auto& dfs, int v) -> void {
tin[v] = T++;
for (auto u : g[v]) {
dfs(dfs, u);
}
tout[v] = T;
go[tin[v]].push_back({tout[v], a[v]});
};
dfs(dfs, rt);
vector<vector<int>> dp(n + 1, vector<int> (m + 1, -(int) 1e9));
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (auto [r, x] : go[i]) {
for (int j = 0; j < m; j++) {
dp[r][j + 1] = max(dp[r][j + 1], dp[i][j] + x);
}
}
for (int j = 0; j <= m; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
cout << dp.back().back();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |