#include <iostream>
#include <memory.h>
using namespace std;
const int N = (int)200 + 1;
const int M = (int)500 + 1;
const int inf = (int)0;
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);
}
/*
8 3
0 13
1 1
1 1
2 1
2 1
3 4
3 4
3 4
*/
Compilation message
biochips.cpp:56:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1144 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |