A. 装箱问题(boxes) [ Discussion ]
Description
有一个箱子容量为v(正整数,0≤v≤20000),同时有n个物品(0<n≤30),每个物品有一个体积(正整数)。
要求从n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小。
Input
箱子的容量v
物品数n
接下来n行,分别表示这n个物品的体积
Output
箱子剩余空间
Samples
Input Copy
24
6
8
3
12
7
9
7
Output
0
#include<bits/stdc++.h>
using namespace std;
#define IN -1e6
#define INT 1e6
const int maxn=1e8;
int res=1;
typedef long long ll;
set <int> s;
typedef struct st{
int x;
int y;
int z=0;
}ss;
int min1(int a,int b)
{
if(a<b)
return a;
return b;
}
int cmp(char a,char b)
{
return a>b;
}
int n;
int v;
int sheng=100000;
int sum=0;
int a[100];
void dfs(int t)
{
if(t==n+1)
{
if(sheng>sum&&sum<=v)
{
if(v-sum<sheng)
sheng=v-sum;
}
return;
}
sum+=a[t];
dfs(t+1);
sum-=a[t];
dfs(t+1);
}
int main()
{
cin>>v>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
dfs(1);
cout<<sheng;
return 0;
}