Bills

来源:互联网 发布:高一数学优化方案答案 编辑:程序博客网 时间:2024/06/06 03:54
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;}



0 0