結果

問題 No.2095 High Rise
ユーザー ks2mks2m
提出日時 2022-10-07 21:57:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 75,056 KB
実行使用メモリ 74,308 KB
最終ジャッジ日時 2023-09-03 01:45:40
合計ジャッジ時間 8,735 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,336 KB
testcase_01 AC 43 ms
49,300 KB
testcase_02 AC 42 ms
49,852 KB
testcase_03 AC 44 ms
49,476 KB
testcase_04 AC 45 ms
49,456 KB
testcase_05 AC 43 ms
49,388 KB
testcase_06 AC 43 ms
49,260 KB
testcase_07 AC 43 ms
49,352 KB
testcase_08 AC 43 ms
47,308 KB
testcase_09 AC 42 ms
49,444 KB
testcase_10 AC 44 ms
49,560 KB
testcase_11 AC 43 ms
49,592 KB
testcase_12 AC 60 ms
50,672 KB
testcase_13 AC 59 ms
51,344 KB
testcase_14 AC 73 ms
52,504 KB
testcase_15 AC 61 ms
50,620 KB
testcase_16 AC 57 ms
51,360 KB
testcase_17 AC 220 ms
57,196 KB
testcase_18 AC 173 ms
57,076 KB
testcase_19 AC 125 ms
55,148 KB
testcase_20 AC 225 ms
56,576 KB
testcase_21 AC 296 ms
59,352 KB
testcase_22 AC 624 ms
73,240 KB
testcase_23 AC 649 ms
74,308 KB
testcase_24 AC 578 ms
73,772 KB
testcase_25 AC 586 ms
73,964 KB
testcase_26 AC 579 ms
73,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			for (int j = 0; j < m; j++) {
				a[i][j] = Integer.parseInt(sa[j]);
			}
		}
		br.close();

		if (n == 1) {
			System.out.println(0);
			return;
		}

		long inf = 2000000000000000000L;
		long[][] dp = new long[n][m];
		for (int i = 0; i < m; i++) {
			dp[0][i] = a[0][i];
		}
		for (int i = 1; i < n; i++) {
			long[] minL = new long[m + 1];
			minL[0] = inf;
			for (int j = 0; j < m; j++) {
				minL[j + 1] = Math.min(minL[j], dp[i - 1][j]);
			}

			long[] minR = new long[m + 1];
			minR[m] = inf;
			for (int j = m - 1; j >= 0; j--) {
				minR[j] = Math.min(minR[j + 1], dp[i - 1][j]);
			}

			for (int j = 0; j < m; j++) {
				long v0 = dp[i - 1][j] + a[i][j];
				long v1 = minL[j] + a[i - 1][j] + a[i][j];
				long v2 = minR[j + 1] + a[i - 1][j] + a[i][j];
				dp[i][j] = Math.min(Math.min(v0, v1), v2);
			}
		}

		long ans = Long.MAX_VALUE;
		for (int i = 0; i < m; i++) {
			ans = Math.min(ans, dp[n - 1][i]);
		}
		System.out.println(ans);
	}
}
0