1 #include<iostream>
2 #include<cstring>
3 #include<vector>
4 #include<cstdio>
5 #include<algorithm>
6 #include<cstdlib>
7 using namespace std;
8
9 #define inf (1<<29)
10 #define maxn 60
11 #define maxm 2010
12 #define maxl 110
13 int n,m,con[maxn],pri[maxn],lim[maxn];
14 int f[maxn][maxl][maxm],g[maxm],root,ans;
15 bool bas[maxn];
16 vector < pair <int,int> > need[maxn];
17
18 inline int Max(int a,int b) { if (a > b) return a; return b; }
19
20 inline int Min(int a,int b) { if (a < b) return a; return b; }
21
22 inline int getint()
23 {
24 int x=0,f=1;char ch=getchar();
25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
26 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
27 return x*f;
28 }
29
30 inline void read()
31 {
32 for (int i = 1;i <= n;++i)
33 {
34 con[i] = getint();
35 char opt = getchar();
36 if (opt == ‘A‘)
37 {
38 int nn = getint();
39 for (int j = 1;j <= nn;++j)
40 {
41 int a = getint(),b = getint();
42 need[i].push_back(make_pair(a,b));
43 bas[a] = true;
44 }
45 lim[i] = inf;
46 }
47 else pri[i] = getint(),lim[i] = getint();
48 }
49 for (int i = 1;i <= n;++i)
50 if (!bas[i]) { root = i; break; }
51 }
52
53 inline void dp(int now)
54 {
55 if (need[now].size() == 0)
56 {
57 lim[now] = Min(lim[now],m/pri[now]);
58 for (int i = 0;i <= lim[now];++i)
59 for (int j = i;j <= lim[now];++j)
60 f[now][i][j*pri[now]] = (j-i)*con[now];
61 return;
62 }
63 lim[now] = inf; int nn = need[now].size();
64 for (int i = 0;i < nn;++i)
65 dp(need[now][i].first),lim[now] = Min(lim[now],lim[need[now][i].first]/need[now][i].second);
66 for (int i = 0;i <= lim[now];++i) f[now][i][0] = 0;
67 for (int i = 0;i < nn;++i)
68 for (int j = 0;j <= lim[now];++j)
69 {
70 memcpy(g,f[now][j],sizeof(g));
71 memset(f[now][j],-1,sizeof(g));
72 for (int k = m;k >= 0;--k)
73 {
74 for (int r = k;r >= 0; --r)
75 if (g[k - r] != -1&&f[need[now][i].first][j*need[now][i].second][r] != -1)
76 {
77 f[now][j][k] = Max(f[now][j][k],g[k-r]+f[need[now][i].first][j*need[now][i].second][r]);
78 ans = Max(ans,f[now][j][k]);
79 }
80 }
81 }
82 for (int i = 0;i <= lim[now];++i)
83 for (int j = i;j <= lim[now];++j)
84 for (int k = 0;k <= m;++k)
85 {
86 if (f[now][j][k] == -1) continue;
87 f[now][i][k] = Max(f[now][i][k],f[now][j][k] + (j-i)*con[now]),ans = Max(ans,f[now][i][k]);
88 }
89 }
90
91 int main()
92 {
93 freopen("1017.in","r",stdin);
94 freopen("1017.out","w",stdout);
95 memset(f,-1,sizeof(f));
96 scanf("%d %d",&n,&m);
97 read();
98 dp(root);
99 printf("%d",ans);
100 fclose(stdin); fclose(stdout);
101 return 0;
102 }