結果

問題 No.2488 Mod Sum Maximization
ユーザー sotanishysotanishy
提出日時 2023-09-29 23:59:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 3,558 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 205,108 KB
実行使用メモリ 15,988 KB
最終ジャッジ日時 2023-09-29 23:59:49
合計ジャッジ時間 11,324 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 433 ms
15,940 KB
testcase_04 AC 398 ms
15,988 KB
testcase_05 AC 402 ms
15,940 KB
testcase_06 AC 388 ms
15,788 KB
testcase_07 AC 254 ms
10,264 KB
testcase_08 AC 378 ms
15,672 KB
testcase_09 AC 303 ms
10,804 KB
testcase_10 AC 213 ms
9,784 KB
testcase_11 AC 9 ms
4,380 KB
testcase_12 AC 188 ms
9,644 KB
testcase_13 AC 143 ms
7,076 KB
testcase_14 AC 29 ms
4,376 KB
testcase_15 AC 216 ms
9,732 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 58 ms
4,780 KB
testcase_18 AC 16 ms
4,376 KB
testcase_19 AC 319 ms
10,792 KB
testcase_20 AC 339 ms
15,256 KB
testcase_21 AC 317 ms
11,056 KB
testcase_22 AC 400 ms
15,864 KB
testcase_23 AC 384 ms
15,724 KB
testcase_24 AC 128 ms
6,640 KB
testcase_25 AC 8 ms
4,380 KB
testcase_26 AC 15 ms
4,376 KB
testcase_27 AC 313 ms
9,628 KB
testcase_28 AC 353 ms
9,652 KB
testcase_29 AC 266 ms
9,528 KB
testcase_30 AC 185 ms
6,424 KB
testcase_31 AC 27 ms
4,376 KB
testcase_32 AC 545 ms
10,900 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 542 ms
15,660 KB
testcase_35 AC 149 ms
6,372 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <typename M>
class SegmentTree {
    using T = typename M::T;

   public:
    SegmentTree() = default;
    explicit SegmentTree(int n) : SegmentTree(std::vector<T>(n, M::id())) {}
    explicit SegmentTree(const std::vector<T>& v) {
        size = 1;
        while (size < (int)v.size()) size <<= 1;
        node.resize(2 * size, M::id());
        std::copy(v.begin(), v.end(), node.begin() + size);
        for (int i = size - 1; i > 0; --i)
            node[i] = M::op(node[2 * i], node[2 * i + 1]);
    }

    T operator[](int k) const { return node[k + size]; }

    void update(int k, const T& x) {
        k += size;
        node[k] = x;
        while (k >>= 1) node[k] = M::op(node[2 * k], node[2 * k + 1]);
    }

    T fold(int l, int r) const {
        T vl = M::id(), vr = M::id();
        for (l += size, r += size; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = M::op(vl, node[l++]);
            if (r & 1) vr = M::op(node[--r], vr);
        }
        return M::op(vl, vr);
    }

    template <typename F>
    int find_first(int l, F cond) const {
        T v = M::id();
        for (l += size; l > 0; l >>= 1) {
            if (l & 1) {
                T nv = M::op(v, node[l]);
                if (cond(nv)) {
                    while (l < size) {
                        nv = M::op(v, node[2 * l]);
                        if (cond(nv))
                            l = 2 * l;
                        else
                            v = nv, l = 2 * l + 1;
                    }
                    return l + 1 - size;
                }
                v = nv;
                ++l;
            }
        }
        return -1;
    }

    template <typename F>
    int find_last(int r, F cond) const {
        T v = M::id();
        for (r += size; r > 0; r >>= 1) {
            if (r & 1) {
                --r;
                T nv = M::op(node[r], v);
                if (cond(nv)) {
                    while (r < size) {
                        nv = M::op(node[2 * r + 1], v);
                        if (cond(nv))
                            r = 2 * r + 1;
                        else
                            v = nv, r = 2 * r;
                    }
                    return r - size;
                }
                v = nv;
            }
        }
        return -1;
    }

   private:
    int size;
    std::vector<T> node;
};

struct MaxMonoid {
    using T = ll;
    static T id() { return -1e18; }
    static T op(T a, T b) { return max(a, b); }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    vector<ll> A(N);
    for (auto& x : A) cin >> x;
    SegmentTree<MaxMonoid> st(N);
    st.update(0, 0);
    rep(i, 0, N) {
        ll d = st.fold(i, N);
        rep(n, 1, A.back() / A[i] + 2) {
            int x = A[i] * n;
            int k = lower_bound(all(A), x) - A.begin();
            if (k > 0) {
                st.update(k - 1,
                          max(st[k - 1], d + A[k - 1] % A[i] - A[k - 1]));
            }
        }
    }
    cout << accumulate(all(A), 0LL) + st[N - 1] << endl;
}
0