結果

問題 No.595 登山
コンテスト
ユーザー nebukuro09
提出日時 2017-11-12 00:37:53
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 70 ms / 1,500 ms
コード長 729 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 601 ms
コンパイル使用メモリ 100,416 KB
実行使用メモリ 16,004 KB
最終ジャッジ日時 2026-03-05 17:24:30
合計ジャッジ時間 2,433 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long INF = 1L << 60;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto P = s[1].to!long;
    auto H = readln.split.map!(to!long).array;

    auto dp = new long[][](2, N);
    dp[0][0] = 0;
    dp[1][0] = INF;

    foreach (i; 1..N) {
        dp[0][i] = min(dp[0][i-1] + max(0, H[i]-H[i-1]),
                       min(dp[0][i-1], dp[1][i-1]) + P);
        dp[1][i] = min(dp[1][i-1] + max(0, H[i-1]-H[i]),
                       min(dp[0][i-1], dp[1][i-1]) + P);
    }

    min(dp[0][N-1], dp[1][N-1]).writeln;
}
0