|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class SB_1749 { |
| 7 | + public static void main(String[] args) throws IOException { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 10 | + |
| 11 | + int N = Integer.parseInt(st.nextToken()); |
| 12 | + int M = Integer.parseInt(st.nextToken()); |
| 13 | + |
| 14 | + int[][] board = new int[N][M]; |
| 15 | + long[][] dp = new long[N + 1][M + 1]; |
| 16 | + for (int i = 0; i < N; i++) { |
| 17 | + st = new StringTokenizer(br.readLine()); |
| 18 | + for (int j = 0; j < M; j++) { |
| 19 | + board[i][j] = Integer.parseInt(st.nextToken()); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // 누적합 계산 |
| 24 | + for (int i = 1; i < N + 1; i++) { |
| 25 | + for (int j = 1; j < M + 1; j++) { |
| 26 | + dp[i][j] = board[i-1][j-1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // 부분 행렬의 최대값 찾기 |
| 31 | + long mx = Long.MIN_VALUE; |
| 32 | + for (int r1 = 1; r1 < N + 1; r1++) { |
| 33 | + for (int c1 = 1; c1 <= M + 1; c1++) { |
| 34 | + for (int r2 = r1; r2 <= N; r2++) { |
| 35 | + for (int c2 = c1; c2 <= M; c2++) { |
| 36 | + long tmp = dp[r2][c2] - dp[r1 - 1][c2] - dp[r2][c1 - 1] + dp[r1 - 1][c1 - 1]; |
| 37 | + mx = Math.max(mx, tmp); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + System.out.println(mx); |
| 44 | + } |
| 45 | +} |
0 commit comments