1 /**************************************************************
2 Problem: 3163
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:816 ms
7 Memory:9252 kb
8 ****************************************************************/
9
10 //Huce #6 A
11 #include<vector>
12 #include<cstdio>
13 #include<cstdlib>
14 #include<cstring>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 using namespace std;
21
22 int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 typedef long long LL;
29 const int N=1010,INF=~0u>>2;
30 /*******************tamplate********************/
31 int n,m,a[N],b[N],c[N],g1[N][N],g2[N][N];
32 int main(){
33 #ifndef ONLINE_JUDGE
34 freopen("A.in","r",stdin);
35 // freopen("output.txt","w",stdout);
36 #endif
37 n=getint();
38 F(i,1,n){a[i]=getint(); b[i]=getint(); c[i]=getint();}
39 F(i,1,n) F(k,0,c[i]) F(j,k*a[i],1000)
40 g1[i][j]=max(g1[i][j],g1[i-1][j-k*a[i]]+k*b[i]);
41
42 D(i,n,1) F(k,0,c[i]) F(j,k*a[i],1000)
43 g2[i][j]=max(g2[i][j],g2[i+1][j-k*a[i]]+k*b[i]);
44 m=getint();
45 while(m--){
46 int d=getint()+1,e=getint(),ans=0;
47 F(j,0,e) ans=max(ans,g1[d-1][j]+g2[d+1][e-j]);
48 printf("%d\n",ans);
49 }
50 return 0;
51 }