結果

問題 No.595 登山
ユーザー outline
提出日時 2021-03-11 21:07:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 56 ms / 1,500 ms
コード長 1,619 bytes
コンパイル時間 1,645 ms
コンパイル使用メモリ 134,108 KB
最終ジャッジ日時 2025-01-19 13:34:34
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    ll P;
    cin >> N >> P;
    vector<ll> H(N);
    for(int i = 0; i < N; ++i)  cin >> H[i];
    
    constexpr ll inf = 1e+17;
    vector<vector<ll>> dp(N, vector<ll>(2, inf));
    dp[0][0] = 0;
    for(int i = 0; i + 1 < N; ++i){
        if(dp[i][0] != inf){
            chmin(dp[i + 1][0], dp[i][0] + min(max(0LL, H[i + 1] - H[i]), P));
            chmin(dp[i + 1][1], dp[i][0] + P);
        }
        if(dp[i][1] != inf){
            chmin(dp[i + 1][0], dp[i][1] + P);
            chmin(dp[i + 1][1], dp[i][1] + min(max(0LL, H[i] - H[i + 1]), P));
        }
    }

    cout << min(dp[N - 1][0], dp[N - 1][1]) << endl;

    return 0;
}
0