結果

問題 No.595 登山
ユーザー simansiman
提出日時 2021-07-30 17:18:53
言語 Ruby
(3.3.0)
結果
AC  
実行時間 288 ms / 1,500 ms
コード長 434 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 11,248 KB
実行使用メモリ 38,348 KB
最終ジャッジ日時 2023-10-13 16:48:23
合計ジャッジ時間 8,519 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,228 KB
testcase_01 AC 82 ms
15,024 KB
testcase_02 AC 85 ms
15,084 KB
testcase_03 AC 81 ms
15,136 KB
testcase_04 AC 273 ms
36,712 KB
testcase_05 AC 140 ms
21,260 KB
testcase_06 AC 252 ms
33,552 KB
testcase_07 AC 97 ms
16,380 KB
testcase_08 AC 113 ms
18,040 KB
testcase_09 AC 253 ms
33,620 KB
testcase_10 AC 200 ms
28,152 KB
testcase_11 AC 189 ms
26,744 KB
testcase_12 AC 95 ms
16,168 KB
testcase_13 AC 132 ms
20,540 KB
testcase_14 AC 272 ms
36,748 KB
testcase_15 AC 275 ms
36,896 KB
testcase_16 AC 275 ms
36,944 KB
testcase_17 AC 273 ms
36,680 KB
testcase_18 AC 275 ms
36,680 KB
testcase_19 AC 284 ms
38,160 KB
testcase_20 AC 286 ms
38,156 KB
testcase_21 AC 288 ms
38,224 KB
testcase_22 AC 284 ms
38,060 KB
testcase_23 AC 287 ms
38,240 KB
testcase_24 AC 80 ms
15,096 KB
testcase_25 AC 79 ms
15,132 KB
testcase_26 AC 288 ms
38,348 KB
testcase_27 AC 281 ms
37,496 KB
testcase_28 AC 278 ms
37,520 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, P = gets.split.map(&:to_i)
H = gets.split.map(&:to_i)

dp = Array.new(N) { Array.new(2, Float::INFINITY) }

dp[0][0] = 0
dp[0][1] = 0

1.upto(N - 1) do |i|
  bh = H[i - 1]
  h = H[i]

  cost1 = dp[i - 1].min + P
  cost2 = dp[i - 1][0] + [0, h - bh].max
  cost3 = dp[i - 1][1] + [0, bh - h].max

  dp[i][0] = [cost1, cost2].min

  if i > 1
    dp[i][1] = [cost1, cost3].min
  else
    dp[i][1] = cost1
  end
end

puts dp[N - 1].min
0