結果

問題 No.2659 Manga App
ユーザー てんぷら
提出日時 2024-03-01 02:13:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,600 ms / 6,000 ms
コード長 2,422 bytes
コンパイル時間 4,602 ms
コンパイル使用メモリ 316,700 KB
実行使用メモリ 174,208 KB
最終ジャッジ日時 2024-09-29 13:24:34
合計ジャッジ時間 25,986 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
using namespace atcoder;
using mint = modint998244353;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll b, c, x;
    cin >> b >> c >> x;
    ll d = b + c;
    vector<ll> a(n - 1);
    rep(i, n - 1) cin >> a[i];
    vector<unordered_map<ll, ll>> dps(n), dpe(n);
    dps[0][0] = x;
    ll ans = longinf;
    rep(i, n - 1) {
        for(auto [ct, cx] : dps[i]) {
            int k = i;
            ll t = 0;
            while(k < n - 1) {
                t += a[k++];
                if(t % d > b) {
                    break;
                }
            }
            if(k == n - 1) {
                ll ret = max(ct + t - cx, 0ll);
                if(ret % d > b) {
                    ret = (ret + d - 1) / d * d;
                }
                ans = min(ans, ret);
            } else {
                ll s = (t + d - 1) / d * d;
                ll e = t / d * d + b;
                ll need = t - e;
                dps[k][ct + s] = max(dps[k][ct + s], cx);
                if(cx < need) {
                    continue;
                }
                dpe[k][ct + e] = max(dpe[k][ct + e], cx - need);
            }
        }
        for(auto [ct, cx] : dpe[i]) {
            int k = i;
            ll t = b;
            while(k < n - 1) {
                t += a[k++];
                if(t % d > b) {
                    break;
                }
            }
            if(k == n - 1) {
                t -= b;
                ll ret = max(ct + t - cx, 0ll);
                if(ret % d > b) {
                    ret = (ret + d - 1) / d * d;
                }
                ans = min(ans, ret);
            } else {
                ll s = (t + d - 1) / d * d;
                ll e = t / d * d + b;
                ll need = t - e;
                s -= b;
                e -= b;
                dps[k][ct + s] = max(dps[k][ct + s], cx);
                if(cx < need) {
                    continue;
                }
                dpe[k][ct + e] = max(dpe[k][ct + e], cx - need);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0