Hide

Problem I
One Block Tetris

Tom has one job: rolling barrels. More specifically, Tom has to allocate barrels of rum according to a list into the ship storage, which is $w$-barrels wide and $h$-barrels long. The list is a sequence of integers between $0$ and $w-1$, where each integer represents the column where a barrel should go. When Tom places a barrel in storage, he always puts it in the lowest empty row of that column.

Additionally, every time Tom fills up a row, the crew celebrates by opening and drinking all the barrels in that row from storage—removing them in the process—and rolling all remaining barrels down.

Tom has to try to follow the list in order. However, if a column is filled, Tom can set the barrel aside, putting it at the back of the list so that he can hopefully fill out rows to make more room.

Tom cannot change the column a barrel is placed in because each column must only store rum of a particular kind, and his crew refuses to open up the barrels if a row is not completely filled.

For example, with a $3 \times 3$ storage, and Tom follows the list: $0, 0, 0, 0, 1, 2$. Tom would not be able to put the fourth barrel in as column $0$ is filled. Thus, he has to put it aside, changing the remaining barrels from $0, 1, 2$ to $1, 2, 0$. After placing barrels $1$ and $2$, the bottom row is emptied out by the crew, and Tom can place the final barrel.

Tom only sets aside a barrel when it cannot fit inside the storage.

Input

The first line contains three integers $1 \leq w \leq 1000$, $1 \leq h \leq 1000$, $0 \leq n \leq 100000$. Each of the next $N$ lines contains where a barrel should go according to the list (an integer $0 \leq A_ i < W$).

Output

Output the number of times Tom has to set aside a barrel, or $-1$ if it is impossible to finish the list.

Sample Input 1 Sample Output 1
3 3 6
0
0
0
0
1
2
1
Sample Input 2 Sample Output 2
1 1 0
0
Sample Input 3 Sample Output 3
1 2 3
0
0
0
0
Sample Input 4 Sample Output 4
2 2 3
0
0
0
-1
Sample Input 5 Sample Output 5
3 1 6
0
0
1
1
2
2
2
Sample Input 6 Sample Output 6
3 1 7
0
1
0
1
2
0
2
3

Please log in to submit a solution to this problem

Log in