結果

問題 No.595 登山
ユーザー 37zigen37zigen
提出日時 2017-11-11 02:15:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 870 ms / 1,500 ms
コード長 1,064 bytes
コンパイル時間 3,540 ms
コンパイル使用メモリ 77,592 KB
実行使用メモリ 65,272 KB
最終ジャッジ日時 2024-05-03 15:18:53
合計ジャッジ時間 20,720 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
41,372 KB
testcase_01 AC 119 ms
41,516 KB
testcase_02 AC 121 ms
41,364 KB
testcase_03 AC 129 ms
41,096 KB
testcase_04 AC 778 ms
58,168 KB
testcase_05 AC 492 ms
48,916 KB
testcase_06 AC 674 ms
55,496 KB
testcase_07 AC 317 ms
47,504 KB
testcase_08 AC 373 ms
48,580 KB
testcase_09 AC 746 ms
56,208 KB
testcase_10 AC 602 ms
50,664 KB
testcase_11 AC 582 ms
50,264 KB
testcase_12 AC 267 ms
47,564 KB
testcase_13 AC 475 ms
48,328 KB
testcase_14 AC 715 ms
58,064 KB
testcase_15 AC 739 ms
60,164 KB
testcase_16 AC 710 ms
59,396 KB
testcase_17 AC 609 ms
58,804 KB
testcase_18 AC 699 ms
58,664 KB
testcase_19 AC 870 ms
62,400 KB
testcase_20 AC 794 ms
61,664 KB
testcase_21 AC 834 ms
61,756 KB
testcase_22 AC 832 ms
61,772 KB
testcase_23 AC 862 ms
65,272 KB
testcase_24 AC 117 ms
41,044 KB
testcase_25 AC 112 ms
40,236 KB
testcase_26 AC 859 ms
61,764 KB
testcase_27 AC 699 ms
61,324 KB
testcase_28 AC 671 ms
62,484 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] + p);
			dp[i][0] = Math.min(dp[i][0], dp[i - 1][0] + p);

			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