#P3444. [POI 2006] ORK-Ploughing

[POI 2006] ORK-Ploughing

Description

Byteasar, the farmer, wants to plough his rectangular field. He can begin by ploughing a slice from any of the field’s edges; then he can plough a slice from any edge of the remaining unploughed field, and so on, until the whole field is ploughed. After each slice is ploughed, the yet-unploughed field is still a rectangle. Each slice has a span of 11, and the field is divided into m×nm\times n unit tiles.

We identify the tiles by their coordinates (i,j)(i,j), for 1im1\le i\le m and 1jn1\le j\le n. Each tile has a non-negative ploughing-difficulty. Let ti,jt_{i,j} denote the difficulty of the tile with coordinates (i,j)(i,j).

Byteasar has only one puny and frail nag (horse). Once the nag starts to plough a slice, it will not stop until the slice is completely ploughed. However, if the slice is too much for the nag to bear, it will die of exhaustion, so Byteasar has to be careful. After every ploughed slice, the nag can rest and gather strength. For every slice, the sum of ploughing-difficulties of the tiles forming it cannot exceed a certain constant kk.

Determine the minimum number of slices required to plough the entire field without exceeding the limit kk on any slice. It is guaranteed that there exists a way to plough the field according to the above rules.

Input Format

  • The first line contains three positive integers kk, mm and nn, separated by single spaces, where 1k200 000 0001\le k\le 200\ 000\ 000 and 1m,n20001\le m,n\le 2000.
  • The next mm lines contain the ploughing-difficulty coefficients. Specifically, line i+1i+1 (1im1\le i\le m) contains nn integers ti,1,ti,2,,ti,nt_{i,1}, t_{i,2}, \ldots, t_{i,n}, separated by single spaces, where 0ti,j100 0000\le t_{i,j}\le 100\ 000.

Output Format

Output one integer: the minimum number of slices required to plough the field while satisfying the given conditions. It is guaranteed that a valid ploughing exists.

12 6 4
6 0 4 8 0 5
0 4 5 4 6 0
0 5 6 5 6 0
5 4 0 0 5 4
8

Hint

Thanks to @NaVi_Awson for the translation.

Translated by ChatGPT 5