結果

問題 No.2488 Mod Sum Maximization
ユーザー shiomusubi496shiomusubi496
提出日時 2023-09-29 11:55:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 1,533 bytes
コンパイル時間 2,126 ms
コンパイル使用メモリ 208,472 KB
実行使用メモリ 57,564 KB
最終ジャッジ日時 2023-09-29 20:59:38
合計ジャッジ時間 9,510 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 260 ms
57,296 KB
testcase_04 AC 247 ms
57,420 KB
testcase_05 AC 260 ms
57,564 KB
testcase_06 AC 249 ms
54,456 KB
testcase_07 AC 169 ms
39,676 KB
testcase_08 AC 241 ms
55,372 KB
testcase_09 AC 222 ms
42,316 KB
testcase_10 AC 136 ms
33,400 KB
testcase_11 AC 10 ms
8,048 KB
testcase_12 AC 123 ms
30,896 KB
testcase_13 AC 97 ms
25,500 KB
testcase_14 AC 24 ms
11,188 KB
testcase_15 AC 144 ms
35,136 KB
testcase_16 AC 11 ms
8,528 KB
testcase_17 AC 44 ms
15,244 KB
testcase_18 AC 16 ms
9,048 KB
testcase_19 AC 201 ms
45,544 KB
testcase_20 AC 219 ms
51,300 KB
testcase_21 AC 210 ms
47,588 KB
testcase_22 AC 240 ms
54,796 KB
testcase_23 AC 246 ms
56,568 KB
testcase_24 AC 87 ms
23,368 KB
testcase_25 AC 8 ms
7,796 KB
testcase_26 AC 14 ms
8,648 KB
testcase_27 AC 153 ms
31,168 KB
testcase_28 AC 159 ms
31,704 KB
testcase_29 AC 146 ms
30,892 KB
testcase_30 AC 92 ms
21,724 KB
testcase_31 AC 21 ms
9,860 KB
testcase_32 AC 244 ms
44,972 KB
testcase_33 AC 6 ms
7,288 KB
testcase_34 AC 296 ms
54,920 KB
testcase_35 AC 84 ms
20,936 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

using namespace std;

using ll = long long;
void chmin(ll& a, ll b) { if (a > b) a = b; }

class SparseTable {
private:
    int n, lg;
    vector<vector<ll>> dat;

public:
    SparseTable(int n_) : n(n_) {
        lg = 0;
        while ((1 << lg) < n) ++lg;
        dat.assign(lg + 1, vector<ll>(n, 1e18));
    }
    void set(int k, ll x) {
        dat[0][k] = x;
        rep (i, lg) {
            if (k + (1 << i) >= n) break;
            dat[i + 1][k] = min(dat[i][k], dat[i][k + (1 << i)]);
        }
    }
    ll get(int k) {
        return dat[0][k];
    }
    ll prod(int l, int r) {
        if (l >= r) return 1e18;
        int h = __lg(r - l);
        return min(dat[h][l], dat[h][r - (1 << h)]);
    }
};

int main() {
    int N; cin >> N;
    vector<int> A(N);
    rep (i, N) cin >> A[i];

    const int M = A[N - 1];

    vector<int> idx(M + 1, N);
    rep (i, N) idx[A[i]] = i;
    rrep (i, M + 1) {
        if (idx[i] == N) idx[i] = idx[i + 1];
    }

    SparseTable seg(N);
    seg.set(N - 1, 0);
    rrep (i, N - 1) {
        int j = i + 1;
        ll mn = 1e18;
        while (j != N) {
            int p = A[j] / A[i];
            int k = (p + 1) * A[i] > M ? N : idx[(p + 1) * A[i]];
            chmin(mn, seg.prod(j, k) + p * A[i]);
            j = k;
        }
        seg.set(i, mn);
    }

    ll sm = 0;
    rep (i, N) sm += A[i];
    cout << sm - seg.get(0) << endl;
}
0