装箱问题


Description

有一个箱子容量为 V V V(正整数,0 ≤ V V V ≤200000),同时有 n n n个物品(0< n n n≤30,每个物品有一个体积(正整数))。
要求 n n n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小。

Input

1个整数,表示箱子容量

11个整数,表示有 n n n个物品

接下来 n n n行,分别表示这 n n n个物品的各自体积

Output

1个整数,表示箱子剩余空间。

Sample Input

24
6
8
3
12
7
9
7

Sample Output

0

Hink

NOIp2001普及组 第4题

解题思路

用搜索看装进哪个箱子比较好

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=101;
int a[maxn],n,m,t;
void DFS(int dep,int s)
{
   
	if(s>m) return;//如果大于总数,退出
	if(dep>n)
	{
   
		t=max(t,s);//求最优方案
		return;
	}
	DFS(dep+1,s+a[dep]);
	DFS(dep+1,s);
}
int main()
{
   
	cin>>m>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	DFS(0,0);
	cout<<m-t;//输出剩下的空间
	return 0;
}