結果

問題 No.595 登山
ユーザー cormorancormoran
提出日時 2017-11-10 23:55:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,351 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 179,080 KB
実行使用メモリ 11,020 KB
最終ジャッジ日時 2024-05-03 14:54:17
合計ジャッジ時間 3,158 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 WA -
testcase_27 AC 28 ms
9,736 KB
testcase_28 AC 30 ms
9,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;


class Solver {
  public:
    bool solve() {
        ll N, P; cin >> N >> P;
        vector<ll> H(N); cin >> H;
        auto gezan = [&] (int &i) {
            ll sum = 0;
            while(i + 1 < N and H[i] >= H[i + 1]) {
                sum += H[i] - H[i + 1];
                i++;                
            }
            return sum;
        };
        auto tozan = [&] (int &i) {
            ll sum = 0;
            while(i + 1 < N and H[i] <= H[i + 1]) {
                sum += H[i + 1] - H[i];
                i++;
            }
            return sum;
        };
        int i = 0;
        vector<pair<ll, ll>> arr;
        gezan(i);
        while(i + 1 < N) {
            arr.push_back(make_pair(tozan(i), arr.size()));
            arr.push_back(make_pair(gezan(i), arr.size()));
        }
        if(arr.size() > 0 and arr.back().first == 0) arr.pop_back();        
        sort(all(arr));
        vector<int> used(N);
        ll ans = 0;
        rep(i, arr.size()) {
            ll cost = arr[i].first;
            int idx = arr[i].second;
            if(used[idx]) continue;
            used[idx] = true;
            if(idx + 1 < N) used[idx + 1] = true;
            if(idx > 0) used[idx - 1] = true;
            ans += min(P * 2, cost);
        }
        cout << ans << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0