web123456

Informatics Olympiad Book 1295: Boxing Problems (evd)

[Title Description]
There is a box with capacity V (positive integer, 0 ≤ v ≤ 20,000) and also n items (0 & lt; n ≤ 30), each with a volume (positive integer).

It is required that any number of the n items be loaded into the box so that the remaining space in the box is minimized.

[Input]
The first line is an integer V, indicating the box capacity.

The second line is an integer n, representing the number of items.

The next n rows, each with a positive integer (no more than 10000), represent the respective volumes of each of these n items.

[Output]
An integer indicating the space remaining in the box.

[Input Sample]
24
6
8
3
12
7
9
7
[Sample output]
0
[Insights] It can be a good question for beginners. It's not that the backpack has to have w and c, they can be one sometimes!
[AC Code]

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=20010;
int w[35],f[N];
int main()
{
	int v,n;
	cin>>v>>n;
	for(int i=1;i<=n;i++) cin>>w[i];
	for(int i=1;i<=n;i++)
	{
		for(int j=v;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+w[i]);
		}
	}
	cout<<v-f[v];
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22