# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
32008 | szawinis | Biochips (IZhO12_biochips) | C++14 | 333 ms | 402488 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 = 2e5+1;
int n, m, x[MAX], ed[MAX], dp[MAX][501];
vector<int> g[MAX], ord;
void dfs(int u) {
ord.push_back(u);
for(int v: g[u]) dfs(v);
ed[u] = ord.size();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int root = -1;
for(int i = 0, p; i < n; i++) {
cin >> p >> x[i]; --p;
if(p >= 0) g[p].push_back(i);
else root = i;
}
dfs(root);
for(int j = 1; j <= m; j++) dp[n][j] = INT_MIN;
for(int i = n-1; i >= 0; i--) {
for(int j = 0; j <= m; j++) {
dp[i][j] = dp[i+1][j];
if(j) dp[i][j] = max(dp[ed[ord[i]]][j-1] + x[ord[i]], dp[i][j]);
}
}
cout << dp[0][m];
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |