No.50 Toy Box
タグ : / 解いたユーザー数 167
作問者 :
nmnmnmnmnmnmnm
Problem Statement
There are N toys and M toy boxes.
Each toy and each toy box has a fixed volume.
A toy whose volume is at most the volume of a toy box can be put into that toy box.
A toy box can hold any number of toys as long as the total does not exceed the volume of the toy box.
A toy box must not be put into another toy box.
For example, suppose there are a toy of volume 2, a toy of volume 3, and a toy box of volume 5.
In this case, the total volume of the two toys is 5, so they fit into the toy box of volume 5.
For example, suppose there are a toy of volume 8, a toy of volume 12, and a toy box of volume 10.
In this case, the total volume of the two toys is 20, so they do not fit into the toy box of volume 10.
However, the toy of volume 8 alone fits into the toy box of volume 10.
You are given the information about the toys and the toy boxes.
Find the minimum number of toy boxes with which all the toys can be put away.
However, if not all of the toys can fit into the toy boxes, the answer is -1.
Input
\(N\)
\(A_0\,A_1\, \dots\,A_{n-1}\)
\(M\)
\(B_0\,B_1\, \dots\,B_{m-1}\)
\(N\) is a positive integer. \(1 \le N \le 10 \).
The next line gives the volumes of the N toys on a single line.
\(A_i \) is a positive integer. \(1\le A_i \le 20\).
\(M \) is a positive integer. \(1\le M \le 10\).
The next line gives the volumes of the \(M \) toy boxes on a single line.
\(B_i \) is a positive integer. \(1\le B_i \le 20\).
Output
Find the minimum number of toy boxes.
If it is impossible, return -1.
Do not forget the newline.
Sample
Sample 1
Input
2 2 3 1 5
Output
1
There are toys of volume 2 and volume 3 and a toy box of volume 5.
All the toys fit into one toy box.
Sample 2
Input
2 8 12 2 10 10
Output
-1
There are toys of volume 8 and volume 12 and 3 toy boxes of volume 10.
The toy of volume 8 fits into one box of volume 10.
However, the toy of volume 12 does not fit into a toy box of volume 10.
Therefore, it is impossible to put all the toys into the toy boxes.
Sample 3
Input
10 1 1 1 1 1 1 1 1 1 1 10 9 9 9 9 9 9 9 9 9 9
Output
2
There are 10 toys of volume 1 and 10 toy boxes of volume 9.
For example, put 9 toys of volume 1 into one toy box.
Put 1 toy of volume 1 into one toy box.
In this case, 2 toy boxes are enough.
Sample 4
Input
9 2 1 3 11 5 17 13 19 7 6 20 14 18 12 16 10
Output
5
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。