#include <iostream>
#include <bitset>
#include <algorithm>
#include <memory.h>
#include <utility>
#include <vector>
#include <cmath>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <map>
#include <queue>
#define int long long
using namespace std;
const int N = (int)2e5 + 1;
const int M = (int)5e2 + 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) {
if (m != 0) {
return -inf;
} else {
return 0;
}
}
int &res = dpg[v][m];
if (res != -1) return res;
res = 0;
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) {
int &res = dp[v][m];
if (res != -1)
return res;
res = 0;
if (m == 1) {
res = w[v];
return res;
} else if (m == 0) {
return res;
}
if (lst[v] == -1) {
return res = -inf;
}
//cout << "f -> " << v << ' ' << m << ' ' << res << endl;
return res = g(lst[v], m);
}
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
biochips.cpp:69:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
448 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |