結果

問題 No.595 登山
ユーザー 37zigen37zigen
提出日時 2017-11-11 02:04:08
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,038 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 77,440 KB
実行使用メモリ 77,836 KB
最終ジャッジ日時 2024-05-03 15:16:59
合計ジャッジ時間 18,055 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
41,136 KB
testcase_01 AC 123 ms
41,424 KB
testcase_02 AC 119 ms
40,240 KB
testcase_03 AC 125 ms
41,308 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 102 ms
39,824 KB
testcase_25 AC 102 ms
40,268 KB
testcase_26 AC 785 ms
61,816 KB
testcase_27 AC 629 ms
64,348 KB
testcase_28 AC 636 ms
63,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	static void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = sc.nextLong();
		long[] h = new long[n];
		for (int i = 0; i < n; ++i) {
			h[i] = sc.nextLong();
		}
		long[][] dp = new long[n][2];
		for (int i = 0; i < n; ++i) {
			dp[i][0] = Long.MAX_VALUE / 3;
			dp[i][1] = Long.MAX_VALUE / 3;
		}
		dp[0][0] = 0;
		dp[0][1] = p;
		for (int i = 1; i < n; ++i) {
			dp[i][0] = Math.min(dp[i][0], dp[i - 1][0] + Math.max(0, h[i] - h[i - 1]));
			dp[i][0] = Math.min(dp[i][0], dp[i - 1][1] + Math.max(0, h[i] - h[i - 1]));
			dp[i][1] = Math.min(dp[i][1], dp[i - 1][0] + p);
			dp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + p);
			dp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + Math.max(0, h[i - 1] - h[i]));
		}
		System.out.println(Math.min(dp[n - 1][0], dp[n - 1][1]));
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0