結果

問題 No.595 登山
ユーザー kimiyukikimiyuki
提出日時 2017-11-12 11:12:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 1,500 ms
コード長 755 bytes
コンパイル時間 519 ms
コンパイル使用メモリ 49,028 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 10:54:09
合計ジャッジ時間 2,685 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 23 ms
4,380 KB
testcase_05 AC 8 ms
4,376 KB
testcase_06 AC 20 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 15 ms
4,380 KB
testcase_11 AC 13 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 18 ms
4,380 KB
testcase_16 AC 19 ms
4,380 KB
testcase_17 AC 18 ms
4,380 KB
testcase_18 AC 19 ms
4,376 KB
testcase_19 AC 28 ms
4,380 KB
testcase_20 AC 28 ms
4,376 KB
testcase_21 AC 28 ms
4,380 KB
testcase_22 AC 28 ms
4,376 KB
testcase_23 AC 28 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 28 ms
4,380 KB
testcase_27 AC 22 ms
4,380 KB
testcase_28 AC 22 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }

constexpr ll inf = ll(1e18)+9;
int main() {
    // input
    int n, p; scanf("%d%d", &n, &p);
    vector<int> h(n); repeat (i, n) scanf("%d", &h[i]);
    // solve
    ll fwd = 0;
    ll bck = inf;
    repeat (i, n - 1) {
        ll prv = min(fwd, bck);
        if (h[i] < h[i + 1]) {
            fwd += h[i + 1] - h[i];
        } else {
            bck += h[i] - h[i + 1];
        }
        setmin(fwd, prv + p);
        setmin(bck, prv + p);
    }
    ll result = min(fwd, bck);
    // output
    printf("%lld\n", result);
    return 0;
}
0