Under construction

Still being built. Things may change

Cinema

Time limit2sMemory limit512 MB

Summary
Given rows of seat comfort values and a budget k for raising them, maximize the audiences seated, where each person takes the most comfortable reachable seat.
Level

Hard9 of 10

Topics
Dynamic programming, Stack, Binary search, Array
Solved
No attempts yet

Problem

The main movie theater of the city has a single auditorium with rows of padded seats. Each seat has a comfort value, which is a non-negative integer. A seat with a larger comfort value is more comfortable.

A row can only be entered from the left side of the auditorium. The seats in a row are numbered 1 to mm from left to right. When a person enters a row, they sit on the most comfortable seat that is free and accessible to them. If they sit at seat ii, every person who comes later is blocked from seats i+1i+1 to mm. If several free and accessible seats tie for the highest comfort value, the person sits on the leftmost one.

The owner plans to raise the comfort values of some seats to attract more audiences. Raising a seat's comfort value by one unit costs a fixed amount. The total increase across all seats must not exceed kk. Find the way to raise comfort values by at most kk units in total that gives the maximum number of audiences in the auditorium.

Input

The first line contains three non-negative integers nn, mm, and kk, where nn is the number of rows, mm is the number of seats in each row, and kk is the total comfort increase allowed. The constraints are 1≤n⋅m≤3×1051 \le n \cdot m \le 3 \times 10^5 and 0≤k≤10120 \le k \le 10^{12}.

Each of the next nn lines contains mm non-negative integers, each at most 10610^6, giving the comfort values of the seats in that row from left to right.

Output

Print a single line containing the maximum number of audiences.

Examples3

  1. Example 1

    Input
    2 3 10
    10 1 12
    8 3 6
    
    Expected output
    5
    
  2. Example 2

    Input
    1 4 6
    9 8 10 8
    
    Expected output
    3
    
  3. Example 3

    Input
    1 3 2
    10 10 10
    
    Expected output
    2