1 #include<cstdio>
2 #include<iostream>
3 #include<cstdlib>
4 using namespace std;
5 int f[10010][1010]={0},f2[10010][1010]={0},a[1111]={0},b[1111]={0},c[1111]={0},d[300005]={0},e[300005]={0},v[10010]={0},w[10010]={0};
6 int s[1111]={0};
7 int main()
8 {
9 int n=0,q=0,tot;
10 scanf("%d",&n);
11 for (int i=1;i<=n;i++)
12 scanf("%d%d%d",&a[i],&b[i],&c[i]);
13 tot=0;
14 int now=1,x=0,t=0;
15 while (now<=n)//二进制优化
16 {
17 x=1;
18 t=c[now];
19 while (t-x>0)
20 {
21 ++tot;
22 w[tot]=x*a[now];
23 v[tot]=x*b[now];
24 t-=x;
25 x=x*2;
26 }
27 if (t>0)
28 {
29 ++tot;
30 v[tot]=t*b[now];
31 w[tot]=t*a[now];
32 }
33 s[now]=tot;
34 ++now;
35 }
36 cin>>q;
37 int me=0;
38 for (int i=1;i<=q;++i)//寻找询问中最大的容量
39 {
40 scanf("%d%d",&d[i],&e[i]);
41 d[i]+=1;
42 if (e[i]>me)
43 me=e[i];
44 }
45 for (int i=1;i<=tot;++i)//正着
46 for (int j=0;j<=me;++j)
47 if (j<w[i])
48 f[i][j]=f[i-1][j];
49 else f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+v[i]);
50 for (int i=tot;i>=1;i--)//倒着
51 for (int j=0;j<=me;++j)
52 if (j<w[i])
53 f2[i][j]=f2[i+1][j];
54 else f2[i][j]=max(f2[i+1][j],f2[i+1][j-w[i]]+v[i]);
55 int ans=0;
56 for (int i=1;i<=q;++i)
57 {
58 ans=0;
59 for (int j=0;j<=e[i];++j)//枚举两边的容量如何分配
60 if (f[s[d[i]-1]][j]+f2[s[d[i]]+1][e[i]-j]>ans)
61 ans=f[s[d[i]-1]][j]+f2[s[d[i]]+1][e[i]-j];
62 printf("%d\n",ans);
63 }
64 return 0;
65 }