#P9457. [入门赛 #14] 魔法少女扶苏 (Hard Version)

[入门赛 #14] 魔法少女扶苏 (Hard Version)

Problem Description

You are given a numeric matrix with nn rows and mm columns. The number in row ii and column jj is called ai,ja_{i,j}.

Fusu can cast magic any number of times. Each time she casts the magic, every number in the matrix is decreased by 11.

Now Fusu wants to know: at least how many times does she need to cast the magic so that there exist at least kk positions (x,y)(x, y) in the matrix such that ax,ya_{x, y} is greater than or equal to the sum of the elements in its row and its column.

Formally, you need to compute the minimum number of magic casts such that after casting, there exist at least kk positions (x,y)(x, y) satisfying $a_{x, y} \geq \sum \limits _{i = 1}^n a_{i,y} + \sum \limits _{i = 1}^m a_{x,i}$.

Input Format

The first line contains three integers, representing the number of rows nn, the number of columns mm, and the required number of positions kk.

Then follow nn lines, each containing mm integers. The jj-th integer in the ii-th line represents the initial value of ai,ja_{i,j}.

Output Format

Output one line with one integer, representing the answer.

2 3 1
1 2 3
4 5 6

3

Hint

Sample 1 Explanation

After casting the magic 33 times, the matrix becomes

210123\begin{matrix}-2 & -1 & 0\\1& 2&3\\\end{matrix}

Then $a_{1,1} = -2 > (-1) + (-3) = \sum\limits_{i =1}^n a_{i,1} + \sum\limits_{i = 1}^m a_{1, i}$.

Constraints

  • For 100%100\% of the testdata, it is guaranteed that 1n,m3×1031 \leq n, m \leq 3 \times 10^3, 1kn×m1 \leq k \leq n \times m, 0ai10110 \leq a_i \leq 10^{11}.

Hint

Please use a reasonable input method to avoid TLE.

Translated by ChatGPT 5