题库练习 Gifts from Grandfather Ahmed
← 上一题 下一题 →

A15859 | Gifts from Grandfather Ahmed

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Grandfather Ahmed's School has $n+1$ students. The students are divided into $k$ classes, and $s_i$ students study in the $i$ -th class. So, $s_1 + s_2 + \ldots + s_k = n+1$ .

Due to the upcoming April Fools' Day, all students will receive gifts!

Grandfather Ahmed planned to order $n+1$ boxes of gifts. Each box can contain one or more gifts. He plans to distribute the boxes between classes so that the following conditions are satisfied:

1. Class number $i$ receives exactly $s_i$ boxes (so that each student can open exactly one box).
2. The total number of gifts in the boxes received by the $i$ -th class should be a multiple of $s_i$ (it should be possible to equally distribute the gifts among the $s_i$ students of this class).

Unfortunately, Grandfather Ahmed ordered only $n$ boxes with gifts, the $i$ -th of which contains $a_i$ gifts.

Ahmed has to buy the missing gift box, and the number of gifts in the box should be an integer between $1$ and $10^6$ . Help Ahmed to determine, how many gifts should the missing box contain, and build a suitable distribution of boxes to classes, or report that this is impossible.

输入格式

The first line of the input contains two integers $n$ and $k$ ( $1 \le n, k \le 200$ , $k \le n + 1$ ).

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^6$ ) — the number of gifts in the available boxes.

The third line contains $k$ integers $s_1, s_2, \ldots, s_k$ ( $1 \le s_i \le n+1$ ) — the number of students in classes. It is guaranteed that $\sum s_i = n+1$ .

输出格式

If there is no way to buy the remaining box, output the integer $-1$ in a single line.

Otherwise, in the first line, output a single integer $s$ — the number of gifts in the box that Grandfather Ahmed should buy ( $1 \le s \le 10^6$ ).

Next, in $k$ lines, print the distribution of boxes to classes. In the $i$ -th line print $s_i$ integers — the sizes of the boxes that should be sent to the $i$ -th class.

If there are multiple solutions, print any of them.

输入输出样例

输入 #1
4 2
7 7 7 127
2 3
输出 #1
1
7 7 
7 127 1
输入 #2
18 4
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
6 1 9 3
输出 #2
9
7 1 7 6 5 4 
9 
1 2 3 8 3 2 9 8 9 
6 5 4
C++ 编辑器
输入
输出