1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 #define For(i, s, t) for(int i = (s); i <= (t); i++)
16 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
17 #define MIT (2147483647)
18 #define INF (1000000001)
19 #define MLL (1000000000000000001LL)
20 #define sz(x) ((bnt) (x).size())
21 #define clr(x, y) memset(x, y, sizeof(x))
22 #define puf push_front
23 #define pub push_back
24 #define pof pop_front
25 #define pob pop_back
26 #define ft first
27 #define sd second
28 #define mk make_pair
29 inline void SetIO(string Name) {
30 string Input = Name+".in",
31 Output = Name+".out";
32 freopen(Input.c_str(), "r", stdin),
33 freopen(Output.c_str(), "w", stdout);
34 }
35
36 const int N = 60, M = 2010, Max = 110;
37 int n, m;
38 int Root, Dp[N][Max][M], F[N][N][M];
39 int Fa[N], Need[N][N], Child[N][N], LC[N];
40 int Power[N], Money[N], Limit[N];
41 bool Visit[N], Basic[N];
42
43 inline void Input() {
44 scanf("%d%d", &n, &m);
45 For(i, 1, n) {
46 scanf("%d", &Power[i]);
47 char ch = ‘ ‘;
48 while(ch != ‘A‘ && ch != ‘B‘) ch = getchar();
49 if(ch == ‘A‘) {
50 scanf("%d", &LC[i]);
51 For(j, 1, LC[i]) {
52 scanf("%d%d", &Child[i][j], &Need[i][j]);
53 Visit[Child[i][j]] = 1;
54 }
55 } else {
56 scanf("%d%d", &Money[i], &Limit[i]);
57 Limit[i] = min(Limit[i], m/Money[i]), Basic[i] = 1;
58 }
59 }
60 }
61
62 inline void CalnLimit(int x) {
63 if(Basic[x]) return;
64 Limit[x] = INF, Money[x] = 0;
65 For(i, 1, LC[x]) {
66 int y = Child[x][i];
67 CalnLimit(y);
68 Limit[x] = min(Limit[x], Limit[y]/Need[x][i]);
69 Money[x] += Money[y]*Need[x][i];
70 }
71 Limit[x] = min(Limit[x], m/Money[x]);
72 }
73
74 inline void Search(int x) {
75 if(Basic[x]) {
76 For(i, 0, Limit[x])
77 For(j, i, Limit[x])
78 Dp[x][i][j*Money[x]] = (j-i)*Power[x];
79 } else {
80 For(i, 1, LC[x]) Search(Child[x][i]);
81
82 For(k, 0, Limit[x]) {
83 For(i, 0, LC[x])
84 For(j, 0, m) F[x][i][j] = -INF;
85 F[x][0][0] = 0;
86
87 For(i, 1, LC[x])
88 For(j, 0, m)
89 For(g, 0, j)
90 F[x][i][j] = max(F[x][i][j],
91 F[x][i-1][j-g]+Dp[Child[x][i]][k*Need[x][i]][g]);
92
93 For(i, 0, k)
94 For(g, 0, m)
95 Dp[x][i][g] = max(Dp[x][i][g], F[x][LC[x]][g]+Power[x]*(k-i));
96 }
97 }
98 }
99
100 inline void Solve() {
101 For(i, 1, n)
102 if(!Visit[i]) {
103 Root = i;
104 break;
105 }
106
107 CalnLimit(Root);
108
109 For(i, 1, n)
110 For(j, 0, Limit[i])
111 For(k, 0, m) Dp[i][j][k] = -INF;
112 Search(Root);
113
114 int Ans = 0;
115 For(i, 0, m) Ans = max(Ans, Dp[Root][0][i]);
116 printf("%d\n", Ans);
117 }
118
119 int main() {
120 SetIO("1017");
121 Input();
122 Solve();
123 return 0;
124 }