Maximum Sum of K Non-Overlapping Submatrices
Time limit2sMemory limit32 MB
Pick exactly K pairwise non-overlapping rectangular submatrices from an N x M matrix to maximize the sum of their elements.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Implementation, Brute force
- Solved
- No attempts yet
Problem
You are given an matrix of integers. Choose exactly pairwise non-overlapping submatrices so that the total sum of all elements contained in the chosen submatrices is as large as possible.
A submatrix is a rectangular block cut from the matrix, formed by consecutive rows and consecutive columns, and its size is at least . Two submatrices overlap if they share at least one common cell. Touching only along an edge or at a corner does not count as overlapping.
Input
The first line contains three space-separated integers , , and : the number of rows, the number of columns, and the number of submatrices you must choose. (, , )
Each of the next lines contains space-separated integers. The -th integer on the -th line is the element at row and column . ()
Output
Print, on a single line, the maximum possible sum of all elements contained in the chosen submatrices.
Hint
You must choose exactly submatrices, and each submatrix is at least . Even when every element is negative you still have to choose of them, so pick them to minimize the loss.