import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] a = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a[i][j] = in.nextInt(); } } int[][] sum = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { sum[i][j] = sum[i - 1][j] + a[i][j]; } } int maxSum = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { int[] dp = new int[n + 1]; for (int k = 1; k <= n; k++) { int x = sum[j][k] - sum[i - 1][k]; dp[k] = Math.max(x + dp[k - 1], x); maxSum = Math.max(maxSum, dp[k]); } } } System.out.println(maxSum); } }
二维前缀和+二维数组dp