結果

問題 No.860 買い物
ユーザー finefine
提出日時 2019-08-09 22:28:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,109 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 166,604 KB
実行使用メモリ 6,408 KB
最終ジャッジ日時 2023-09-26 18:58:14
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 22 ms
6,200 KB
testcase_12 AC 23 ms
6,224 KB
testcase_13 AC 28 ms
6,236 KB
testcase_14 AC 27 ms
6,112 KB
testcase_15 AC 16 ms
6,260 KB
testcase_16 WA -
testcase_17 AC 29 ms
6,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll INF = 1e17;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> c(n), d(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i] >> d[i];
    }

    vector<ll> dp(n, INF);
    vector<ll> dp2(n, INF);
    dp[n - 1] = c[n - 1] * 2;
    dp2[n - 1] = c[n - 1];

    for (int i = n - 2; i >= 0; i--) {
        ll minv1 = min(dp2[i + 1], c[i]);
        ll cost1 = dp[i + 1] + d[i + 1] - dp2[i + 1] + minv1 + c[i];
        
        ll minv2 = c[i];
        ll cost2 = dp[i + 1] + 2 * c[i];

        if (cost1 != cost2) {
            if (cost1 < cost2) {
                dp[i] = cost1;
                dp2[i] = minv1;
            } else {
                dp[i] = cost2;
                dp2[i] = minv2;
            }
        } else {
            if (minv1 > minv2) {
                dp[i] = cost1;
                dp2[i] = minv1;
            } else {
                dp[i] = cost2;
                dp2[i] = minv2;
            }
        }
    }
    cout << dp[0] << endl;
    return 0;
}
0