# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
992582 | tch1cherin | Biochips (IZhO12_biochips) | C++17 | 496 ms | 395600 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;
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 time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |