fork(3) download
  1. #include <vector>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. long long kn[1100][11000];
  8. long long weight[1100];
  9. long long value[1100];
  10. int n, b;
  11. int p[1100];
  12. vector<int> adj[1100];
  13.  
  14. void dfs(int v) {
  15. for (int i = 0; i <= b; i++) {
  16. kn[v][i] = kn[p[v]][i];
  17. }
  18.  
  19. for (int i = 0; i < adj[v].size(); i++) {
  20. dfs(adj[v][i]);
  21. }
  22.  
  23. for (int i = 0; i+weight[v] <= b; i++) {
  24. if (kn[v][i] != -1) {
  25. kn[p[v]][i + weight[v]] = max(kn[p[v]][i], kn[v][i] + value[v]);
  26. }
  27. }
  28. }
  29.  
  30. int main() {
  31. scanf("%d %d", &n, &b);
  32.  
  33. memset(kn[0], -1, sizeof(kn[0]));
  34. kn[0][0] = 0;
  35.  
  36. for (int i = 1; i <= n; i++) {
  37. scanf("%d %lld %lld", &p[i], &weight[i], &value[i]);
  38. adj[p[i]].push_back(i);
  39. }
  40.  
  41. for (int i = 1; i <= n; i++)
  42. if (p[i] == 0)
  43. dfs(i);
  44.  
  45. long long ans = 0;
  46. for (int i = 0; i <= b; i++) ans = max(ans, kn[0][i]);
  47. printf("%lld\n", ans);
  48. }
Success #stdin #stdout 0s 98048KB
stdin
14 450
0 193 1 
1 12 5
1 150 12
3 27 36
3 23 43
0 145 40
6 30 3
6 4 1
6 78 4
9 57 19
9 30 20
0 132 14
12 21 15
12 48 10
stdout
98