Bills

There are N number kinds of bills with different worth. At this moment, you are allowed to use the same kinds of bills several times, but the total number of uses of all bills is restricted to less than K number.
Within this limitation, you want to know the maximum worth by making the worth one by one starting from 1 using the bills.
For example, N=2 is given, the worth of each bill is 1, 3 and K=5 is decided. Then you can make the worth up to 5 by using the bills whose worth is 1, and you can find it out from 6 to 13 as the below:

6 = 3+3
7 = 3+3+1
8 = 3+3+1+1
9 = 3+3+3
10 = 3+3+3+1
11 = 3+3+3+1+1
12 = 3+3+3+3
13 = 3+3+3+3+1

However, you cannot make 14 using 5 bills. Therefore, the maximum worth you can make is 13.

Time limit: 1 second (java: 2 seconds)

Input format


Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
The maximum use number, K and the number of kinds of bills, N are given by being separated with a blank on the first row per each test case. (1 ≤ N ≤ 50, 1 ≤ K ≤ 200)
Each bill worth is given by being separated with a blank on the second row. Each worth is a natural number below 10,000.

Output format

Output the maximum worth you can when making worth using bills in order on the first row per each test case.

Example of Input

2
5 2
1 3
3 10
29 50 36 43 1 2 4 8 15 22

Example of Output

13

56


C

#include <stdio.h>
int a[64],b[2000005];
int main()
{
        int i,j,k,t,n;
        scanf("%d",&t);
        while(t--)
        {
                scanf("%d%d",&k,&n);
                for(i=0;i<n;i++)
                scanf("%d",&a[i]);
                for(i=1; ;i++)
                {
                        b[i]=1000;
                        for(j=0;j<n;j++)
                        if(i>=a[j]&&b[i]>b[i-a[j]]+1)
                                b[i]=b[i-a[j]]+1;
                        if(b[i]>k) break;
                }
                printf("%d\n",i-1);
        }
        return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值