Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.
Zuosige likes elephants very much. Today he finds many types of elephant dolls. Each type has dolls in different colors or different sizes. Zuosige assigned a preference value to each of these dolls, and is willing to buy some. However, Zuosige has limited
money, so he can only choose at most one doll for each type. Please help Zuosige calculate the maximum total preference value of the dolls he can buy without exceeding his money limit.
The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines.
In the first line, there are two integers N and M (1<=N<=20, 1<=M<=1000), indicating the number of types and the money he has.
In the following N lines, each line begin with an integer ci (1<=ci<=50), indicating the number of different dolls in the i-th type. Following in the same line there are 2*ci integers, every two of them describe a doll, in order
of wi and vi (1<=wi, vi<=1000), indicating the price and the preference value of the doll.
For each test case, output one integer in one line indicating the answer. It is guaranteed that Zuosige can always buy one doll from each type.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define N 1000005
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-8
struct node
{
int v,w;
}a[25][55];
int len[25];
int cmp(node a,node b)
{
return a.v*b.w>b.v*b.w;
}
int dp[1005];
int main()
{
int t,n,m,i,j,k;
cin>>t;
w(t--)
{
scanf("%d%d",&n,&m);
up(i,1,n)
{
scanf("%d",&len[i]);
up(j,1,len[i])
{
scanf("%d%d",&a[i][j].w,&a[i][j].v);
}
}
mem(dp,0);
for(k = 1; k<=n; k++)
{
for(i = m; i>=0; i--)
{
for(j = 1;j<=len[k];j++)
{
if(a[k][j].w<=i)
dp[i] = max(dp[i],dp[i-a[k][j].w]+a[k][j].v);
}
}
}
printf("%d\n",dp[m]);
}
return 0;
}