#P9339. Cookie Packing
Cookie Packing
Cookie Packing
Rie likes to make cookies. She made \(N\) types of cookies and produced \(A_i\) cookies of type \(i\) (\(1 \le i \le N\)). In order to sell her cookies, she wants to pack them into boxes subject to the following conditions:
- In every box, the cookies must be of different types.
- For every box, the total number of cookies must equal one of the \(M\) allowed numbers: \(B_1, B_2, \cdots, B_M\).
Your task is to determine whether it is possible to pack all the cookies into boxes. If it is possible, you must output a packing that uses the minimum number of boxes. If it is impossible to pack the cookies under these conditions, output -1
.
Notes:
- Since in each box a type can appear at most once, if a cookie type \(i\) appears \(A_i\) times, these cookies must go to \(A_i\) different boxes.
- Thus the minimum number of boxes is at least \(\max_{1 \le i \le N} A_i\).
- If we decide to use \(K\) boxes and assign each box a capacity \(c_j\) (which must be one of the allowed numbers \(B_k\)), then we must have \(\sum_{j=1}^{K} c_j = \sum_{i=1}^{N} A_i\). Moreover, one must be able to assign each cookie to one box (each cookie type can contribute at most one cookie per box), a condition that can be verified via the Gale–Ryser theorem.
Input Format:
The first line contains two integers \(N\) and \(M\).
The second line contains \(N\) integers, where the \(i\)th integer is \(A_i\) (the number of cookies of type \(i\)).
The third line contains \(M\) integers: \(B_1, B_2, \cdots, B_M\) (the allowed numbers of cookies per box).
Output Format:
If it is impossible to pack all the cookies under the given conditions, output -1
(without quotes).
If it is possible, then on the first line output the minimum number \(K\) of boxes. The following \(K\) lines describe the boxes. Each line begins with an integer \(c_j\) (the number of cookies in the box, which must be one of the allowed numbers) followed by \(c_j\) integers representing the indices of the cookie types placed in that box (the indices are between 1 and \(N\)).
You may output any valid solution that uses the minimum number of boxes.
inputFormat
The first line contains two positive integers \(N\) and \(M\).
The second line contains \(N\) positive integers \(A_1, A_2, \dots, A_N\).
The third line contains \(M\) positive integers \(B_1, B_2, \dots, B_M\).
outputFormat
If it is impossible to pack the cookies satisfying all conditions, output -1
.
Otherwise, on the first line output an integer \(K\) denoting the minimum number of boxes. Then output \(K\) lines: the \(j\)th line begins with the integer \(c_j\) (which must be one of the allowed numbers \(B_1, \dots, B_M\)), followed by \(c_j\) distinct integers representing the types of cookies in that box (each integer is between 1 and \(N\)).
sample
3 2
1 1 1
1 3
1
3 1 2 3
</p>