Day 4: Sub Rectangle Sum 🍫

hardBinary SearchPrefix Sum
You are given an integer matrix $A$ of size $n\times m$ and an integer $K$.

Find the maximum sum of any contiguous sub-rectangle of $A$ such that its sum is at most $K$, and print that maximum sum.

If such value does not exist print $-1.$

Input Format:

The first line contains three integers $n, m$ and $K$ ($1 \le n,m \le 200$, $-10^9 \le K \le 10^9$).

Each of the next $n$ lines contains $m$ integers $A_{i,j}$ ($-10^4 \le A_{i,j} \le 10^4$).

Output Format:

Print a single integer: the maximum sub-rectangle sum not exceeding $K$ if it exists, else print $-1$.

Examples:

Example 1:

Input:

3 3 5
7 2 -1
100 -100 100
0 2 1

Output:

4

Example 2:

Input:

2 3 2
1 0 1
0 -2 3

Output:

2

Example 3:

Input:

1 1 3
8

Output:

-1

Code

Loading Editor...