# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
86872 | Just_Solve_The_Problem | Biochips (IZhO12_biochips) | C++17 | 3 ms | 1148 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 <iostream>
#include <memory.h>
using namespace std;
const int N = (int)200 + 1;
const int M = (int)500 + 1;
const int inf = (int)1e9 + 7;
int n;
int pr[N], w[N];
int root;
int dp[N][M];
int dpg[N][M];
int prv[N], lst[N];
int f(int, int);
int g(int, int);
int g(int v, int m) {
if (m == 0) return 0;
if (v < 0) {
return -inf;
}
int &res = dpg[v][m];
if (res != -1) return res;
res = -inf;
for (int j = 0; j <= m; j++) {
res = max(res, f(v, j) + g(prv[v], m - j));
}
//cout << "g -> " << v << ' ' << m << ' ' << res << endl;
return res;
}
int f(int v, int m) {
if (m == 0) {
return 0;
}
if (lst[v] == -1) {
if (m == 1) return w[v];
return -inf;
}
int &res = dp[v][m];
if (res != -1)
return res;
res = -inf;
if (m == 1) {
res = max(w[v], g(lst[v], m));
} else {
res = g(lst[v], m);
}
//cout << "f -> " << v << ' ' << m << ' ' << res << endl;
return res;
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m;
memset(dp, -1, sizeof dp);
memset(dpg, -1, sizeof dpg);
memset(lst, -1, sizeof lst);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> pr[i] >> w[i];
if (!pr[i]) {
root = i;
} else {
prv[i] = lst[pr[i]];
lst[pr[i]] = i;
}
}
prv[root] = -1;
cout << f(root, m);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |