You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
890 B
40 lines
890 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
const int N = 110, M = N << 1;
|
|
int v[N], w[N];
|
|
int n, m, root;
|
|
int f[N][N];
|
|
|
|
// 链式前向星
|
|
int e[M], h[N], idx, ne[M];
|
|
void add(int a, int b) {
|
|
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
|
|
void dfs(int u) {
|
|
for (int j = w[u]; j <= m; j++) f[u][j] = v[u];
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int son = e[i];
|
|
dfs(son);
|
|
for (int j = m; j >= w[u]; j--)
|
|
for (int k = 0; k <= j - w[u]; k++)
|
|
f[u][j] = max(f[u][j], f[u][j - k] + f[son][k]);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= n; i++) {
|
|
int p;
|
|
cin >> w[i] >> v[i] >> p;
|
|
if (p == -1)
|
|
root = i;
|
|
else
|
|
add(p, i);
|
|
}
|
|
dfs(root);
|
|
printf("%d\n", f[root][m]);
|
|
return 0;
|
|
} |