Problem: garlic Jun 1 shopping bag

Garlic king went shopping, he has a capacity of  VV  shopping bag, and he bought a nn  items, each item known volume v_ivi. Garlic king wanted to know what the selection of articles into the shopping bag, the remaining space can be minimized bags.

Input Format

A first line of input integer  VV (1 \ leq V \ leq 20,000. 1 V 2 0 , 0 0 0 ), represents the capacity of the bag.

A second line of the input integer  nn (1 \ leq n \ leq 30. 1 n . 3 0 ), indicates garlic Jun purchased nn  items.

Next input  nn  lines, each line of input integer v_ivi( 1 \ leq v_i \ leq 10,0001vi. 1 0 , 0 0 0 ), represents the ii  volume of items.

Output Format

Output line, the output of an integer, represents the minimum space remaining bags.

Sample input

20
5
7
5
7
3
7

Sample Output

1

#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum ,n ;
int i,j;
int v[35]={0},dp[35][20010];
memset(dp,0,sizeof(dp));


scanf("%d%d",&sum,&n);
for(i=1;i<=n;i++)
{
    scanf("%d",&v[i]);


}
for(i=1;i<=n;i++)
{
    for(j=1;j<=sum;j++)
    {
        if(j>v[i])
            dp[i][j]=dp[i-1][j]>(dp[i-1][j-v[i]]+v[i])?dp[i-1][j]:(dp[i-1][j-v[i]]+v[i]);
        else
            dp[i][j]=dp[i-1][j];
    }
}
printf("%d",sum-dp[n][sum]);


    return 0;
}

发布了19 篇原创文章 · 获赞 8 · 访问量 4146

Guess you like

Origin blog.csdn.net/paohui001lqp/article/details/79647254
Bag