PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000489. Tổng hình chữ nhật lớn nhất không vượt quá K (Max Sum of Rectangle No Larger Than K)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Tổng hình chữ nhật lớn nhất không vượt quá K (Max Sum of Rectangle No Larger Than K)

Max Sum of Rectangle No Larger Than K

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An integer grid has mm rows and nn columns, numbered from 11 to mm and from 11 to nn, respectively. Cell (i,j)(i,j) contains an integer ai,ja_{i,j}. An integer bound kk is also given.

A rectangle consists of consecutive rows r1r_1 through r2r_2 and consecutive columns c1c_1 through c2c_2, where 1≤r1≤r2≤m1\le r_1\le r_2\le m and 1≤c1≤c2≤n1\le c_1\le c_2\le n. It includes all cells in those ranges, including the boundary, and must contain at least one cell. Its sum is the total of the selected cells.

Find the largest sum among rectangles whose sums do not exceed kk. At least one such rectangle is guaranteed to exist.

Input

  • The first line contains three integers m,n,km,n,k: the number of rows, the number of columns, and the upper bound on the sum, respectively.
  • Each of the next mm lines describes one row. The ii-th of these lines contains exactly nn integers ai,1,ai,2,…,ai,na_{i,1},a_{i,2},\ldots,a_{i,n} from left to right.

Output

Print one integer: the largest sum not exceeding kk of a nonempty rectangle in the grid. The answer may be negative.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤101\le m,n\le 10; −105≤k≤105-10^5\le k\le 10^5; −100≤ai,j≤100-100\le a_{i,j}\le 100 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; at least one nonempty rectangle has a sum not exceeding kk.
  • Subtask 2 (30 points): 1≤m,n≤1001\le m,n\le 100; −105≤k≤105-10^5\le k\le 10^5; 0≤ai,j≤1000\le a_{i,j}\le 100 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; at least one nonempty rectangle has a sum not exceeding kk.
  • Subtask 3 (50 points): 1≤m,n≤1001\le m,n\le 100; −105≤k≤105-10^5\le k\le 10^5; −100≤ai,j≤100-100\le a_{i,j}\le 100 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; at least one nonempty rectangle has a sum not exceeding kk.

Examples

Input 1

2 3 2
1 0 1
0 -2 3

Output 1

2

Explanation 1

The grid has two rows and three columns, with k=2k=2. Take both rows and the last two columns. Their values are 0,1,−2,30,1,-2,3, giving 0+1−2+3=20+1-2+3=2. The sum reaches the upper bound, so no valid rectangle has a greater sum.

The selected rectangle

Input 2

1 3 3
2 2 -1

Output 2

3

Explanation 2

There is one row with values 2,2,−12,2,-1. Taking all three cells gives 2+2−1=32+2-1=3, equal to k=3k=3. This is the greatest valid sum.

Input 3

2 2 -12
-5 -6
-7 -8

Output 3

-12

Explanation 3

The single-cell rectangles have sums −5,−6,−7,−8-5,-6,-7,-8, all greater than −12-12. The two-cell rectangles have sums −11,−15,−12,−14-11,-15,-12,-14. The first column has sum −5−7=−12-5-7=-12, which reaches the bound. The entire grid sums to −26-26, below −12-12. Therefore the answer is −12-12.