Value of Array B
InterviewTime limit2sMemory limit512 MB
Swap at most one pair of rows or columns in an N by M grid to maximize the sum of all 2 by 2 block sums.
- Level
Medium5 of 10
- Topics
- Array, Greedy, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Given an array A of size N×M, we can build an array B of size (N-1)×(M-1) as follows.
- B[i][j] = A[i][j] + A[i+1][j] + A[i+1][j+1] + A[i][j+1] (1 ≤ i < N, 1 ≤ j < M)
The value of an array is the sum of all its elements.
In array A, we may swap the positions of any two rows or any two columns. Find the maximum possible value of array B after performing at most one swap in array A.
Input
The first line gives the size of array A, N and M. The next N lines give the elements of the array. The array consists of integers only.
Output
Print the maximum value among all arrays B that can be built.
Constraints
- 2 ≤ N, M ≤ 1,000
- -1,000 ≤ Ai,j ≤ 1,000