Birthday Candles

시간 제한1초메모리 제한2048 MB

요약
각 손님이 남기는 양초 수의 차이가 1 이하가 되게 하면서, 총 노력 C 안에서 최대한 많은 양초를 끌 수 있는 개수를 구한다.
난이도

보통10점 중 6점

유형
정렬, 누적 합, 그리디
정답자
아직 제출이 없습니다

문제

Minka the Martian is celebrating their birthday with many friends. Similar to Earth birthdays, the occasion is commemorated by blowing candles out on a cake. However, on Mars the candles are placed on the cake differently: each of the NN guests attending the party will place HH candles on the cake representing the number of hours the Martian has been alive. So the cake will have exactly N⋅HN \cdot H candles in total. Even more surprising is that each guest hand-crafted the HH candles that they placed on the cake, so the candles are not necessarily identical.

Minka is not feeling particularly well today and fears they won’t be able to blow out all candles. Each candle takes a certain effort to blow out, the effort is given as a positive integer. Minka has a capacity CC to blow out candles, which may not be enough to blow out all candles.

There is one further complication. Minka wants does not want any guest to feel neglected. When they finish blowing out candles, the number of remaining candles between any two guests should differ by at most one. That is, if for each guest ii we let r_ir\_i denote the number of their candles remaining after Minka blows some out, then it should be that ∣r_i−r_j∣≤1|r\_i- r\_j|≤1 for any two guests ii and jj.

Determining the maximum number of candles that Minka can blow out such that the total capacity of all extinguished candles is at most CC and such that ∣r_i−r_j∣≤1|r\_i-r\_j|≤1 for any two guests ii and jj.

입력

The first line of input contains three integers NN (1≤N≤1001≤N≤100), HH (1≤H≤10001≤H≤1000), and CC (1≤C≤1091≤C≤10^9).

The next NN lines describe the candles brought by the guests. The ii’th such line contains HH integers indicating the effort to blow out each of the HH candles brought by guest ii. Each of these values is given as a positive integer at most 10910^9.

출력

Output a single line indicating the maximum number of candles that Minka can blow out subject to the restrictions mentioned above.

예제3

  1. 예제 1

    입력
    2 3 6
    1 2 1
    3 2 1
    
    예상 출력
    4
    
  2. 예제 2

    입력
    4 3 30
    7 4 5
    3 2 4
    5 1 2
    1 2 6
    
    예상 출력
    10
    
  3. 예제 3

    입력
    2 3 3
    1 1 1
    4 5 7
    
    예상 출력
    1