PCPC12G - Mosque

no tags 

Islam prayer in congregation (jama'ah) is considered to have more social and spiritual benefit than praying by oneself. When praying in congregation, the people stand in straight parallel rows behind the chosen imam, facing Qibla (The Qibla is the direction that should be faced when a Muslim prays). Sometimes some of these rows are cut by poles (n poles divide the row into n+1 parts), and some of the rows do not contain poles (the whole row is just one part). Muslims prefer to stand in the rows which are free of poles, so they may leave one row empty and use the next one if it has fewer poles, but they can’t leave two consecutive rows empty.

A mosque is divided into n rows each row may contains one or more poles. Each row free of poles can have m Muslims and every additional pole decreases this number by 2, so if the row contains 2 poles, number of Muslims who can stand in this row is m - 4.

In this problem you are required to arrange the Muslims in rows where number of poles cutting these rows is minimized.

Input

Input contains multiple test cases. Each test case will start with the number of n (0 < n <= 100), m (10 <= m <= 200), and t (0 < t <= 20,000), where n is number of rows, m is number of Muslims per row, and t is the total number of people in the mosque, followed by n lines each containing the number of poles in each row. It is guaranteed that the mosque always can fit all the Muslims. Input will be terminated by end of file.

Output

For each test case, print one line containing the minimum number of poles cutting the prayer rows.

Sample

Input:
8 10 26
1
2
0
2
1
1
1
2
8 10 27
1
2
0
2
1
1
1
2
8 10 27
1
2
1
2
2
1
1
1

Output:
2
3
5

hide comments
Jacob Plachta: 2015-12-31 22:49:34

t can be 0

:D: 2013-01-13 19:34:17

Re: Kunal

I completely agree. The description should be more clear. You can leave any number of rows from the end and those won't be counted as skipped. From be beginning and between used rows, only one. I think it should represent a situation where prayers come to row 1 and move further. They are then skipping first rows but not the last.

danish: 2013-01-02 07:23:32

Is there a blank line after each input and output line.

>>NO .

Last edit: 2013-01-03 09:11:43
Kunal: 2012-12-28 18:24:16

@ProblemSetter:
Consider the following test case:
5 10 10
0
0
0
0
0

In this case any row u choose for the 10ppl, consecutive rows will remain empty...pls explain...!!!

Last edit: 2013-01-01 06:32:20

Added by:abdelkarim
Date:2012-12-28
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:The First Palestinian Collegiate Programming Contest