結果

問題 No.2488 Mod Sum Maximization
ユーザー 👑 NachiaNachia
提出日時 2023-09-29 23:59:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 4,768 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 95,312 KB
実行使用メモリ 12,508 KB
最終ジャッジ日時 2023-09-29 23:59:31
合計ジャッジ時間 9,903 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 382 ms
12,412 KB
testcase_04 AC 354 ms
12,508 KB
testcase_05 AC 357 ms
12,352 KB
testcase_06 AC 330 ms
12,272 KB
testcase_07 AC 248 ms
8,008 KB
testcase_08 AC 343 ms
12,232 KB
testcase_09 AC 276 ms
7,896 KB
testcase_10 AC 182 ms
7,776 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 163 ms
7,624 KB
testcase_13 AC 128 ms
5,612 KB
testcase_14 AC 27 ms
4,376 KB
testcase_15 AC 194 ms
7,612 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 55 ms
4,380 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 290 ms
8,056 KB
testcase_20 AC 304 ms
12,240 KB
testcase_21 AC 289 ms
8,144 KB
testcase_22 AC 333 ms
12,248 KB
testcase_23 AC 377 ms
12,360 KB
testcase_24 AC 114 ms
5,392 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 14 ms
4,380 KB
testcase_27 AC 315 ms
7,788 KB
testcase_28 AC 347 ms
7,620 KB
testcase_29 AC 276 ms
7,632 KB
testcase_30 AC 188 ms
5,588 KB
testcase_31 AC 28 ms
4,380 KB
testcase_32 AC 508 ms
7,900 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 551 ms
12,336 KB
testcase_35 AC 143 ms
5,424 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "..\\Main.cpp"

#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
#include <atcoder/modint>
#line 3 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\array\\segtree.hpp"

namespace nachia{

template<
    class S,
    S op(S l, S r)
>
struct Segtree {
private:
    int N;
    std::vector<S> A;
    int xN;

    void mergev(int i){
        if(i < N) A[i] = op(A[i*2], A[i*2+1]);
    }

    template<class E>
    int minLeft2(int r, E cmp, int a = 0, int b = 0, int i = -1){
        static S x;
        if(i == -1){ a=0; b=N; i=1; x=A[0]; }
        if(r <= a) return a;
        if(b <= r){
            S nx = op(A[i], x);
            if(cmp(nx)){ x = nx; return a; }
        }
        if(b - a == 1) return b;
        int q = minLeft2(r, cmp, (a+b)/2, b, i*2+1);
        if(q > (a+b)/2) return q;
        return minLeft2(r, cmp, a, (a+b)/2, i*2);
    }
    
    template<class E>
    int maxRight2(int l, E cmp, int a = 0, int b = 0, int i = -1){
        static S x;
        if(i == -1){ a=0; b=N; i=1; x=A[0]; }
        if(b <= l) return b;
        if(l <= a){
            S nx = op(x, A[i]);
            if(cmp(nx)){ x = nx; return b; }
        }
        if(b - a == 1) return a;
        int q = maxRight2(l, cmp, a, (a+b)/2, i*2);
        if(q < (a+b)/2) return q;
        return maxRight2(l, cmp, (a+b)/2, b, i*2+1);
    }

public:
    Segtree() : N(0) {}
    Segtree(int n, S e) : xN(n) {
        N = 1; while (N < n) N *= 2;
        A.assign(N * 2, e);
    }
    Segtree(const std::vector<S>& a, S e) : Segtree(a.size(), e){
        for(int i=0; i<(int)a.size(); i++) A[i + N] = a[i];
        for(int i=N-1; i>=1; i--) mergev(i);
    }

    void set(int p, S x){
        p += N; A[p] = x;
        for(int d=1; (1<<d)<=N; d++) mergev(p>>d);
    }

    S get(int p) const { return A[N+p]; }

    S prod(int l, int r) const {
        l += N; r += N;
        S ql = A[0], qr = A[0];
        while(l<r){
            if(l&1) ql = op(ql, A[l++]);
            if(r&1) qr = op(A[--r], qr);
            l /= 2;
            r /= 2;
        }
        return op(ql, qr);
    }

    S allProd() const { return A[1]; }

    // bool cmp(S)
    template<class E>
    int minLeft(int r, E cmp){
        return minLeft2(r, cmp);
    }

    // bool cmp(S)
    template<class E>
    int maxRight(int l, E cmp){
        int x = maxRight2(l, cmp);
        return x > xN ? xN : x;
    }
};

} // namespace nachia
#line 3 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\range-query\\point-set-range-min.hpp"
#include <functional>

namespace nachia {
    template<class T, class Cmp = std::less<T>>
    struct PointSetRangeMin{
    private:
        static T minop(T l, T r){ return std::min(l, r, Cmp()); }
        using Base = Segtree<T, minop>;
        Base base;
        Cmp cmpx;
    public:
        PointSetRangeMin() {}
        PointSetRangeMin(int len, T INF)
            : base(len, INF){}
        PointSetRangeMin(const std::vector<T>& init, T INF)
            : base(init, INF){}
        T min(int l, int r){ return base.prod(l, r); }
        T min(){ return base.allProd(); }
        void set(int pos, T val){ base.set(pos, val); }
        T get(int pos){ return base.get(pos); }
        int lBoundLeft(int from, T val){ return base.minLeft(from, [this,val](const T& x){ return cmpx(val, x); }); }
        int uBoundLeft(int from, T val){ return base.minLeft(from, [this,val](const T& x){ return !cmpx(x, val); }); }
        int lBoundRight(int from, T val){ return base.maxRight(from, [this,val](const T& x){ return cmpx(val, x); }); }
        int uBoundRight(int from, T val){ return base.maxRight(from, [this,val](const T& x){ return !cmpx(x, val); }); }
        template<class E>
        int minLeft(int r, E cmp){ return base.minLeft(r, cmp); }
        template<class E>
        int maxRight(int l, E cmp){ return base.maxRight(l, cmp); }
    };
} // namespace nachia
#line 9 "..\\Main.cpp"
using Modint = atcoder::static_modint<998244353>;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

void testcase(){
    int N; cin >> N;
    vector<int> A(N); rep(i,N) cin >> A[i];
    i64 ans = 0;
    rep(i,N) ans += A[i];
    nachia::PointSetRangeMin<i64> ds(N,INF);
    ds.set(0, 0);
    rep(i,N){
        int a = A[i];
        i64 x = ds.min(i, N);
        for(int t=2; ; t++){
            int pos = lower_bound(A.begin(), A.end(), A[i]*t) - A.begin();
            ds.set(pos-1, min(ds.get(pos-1), x+a*(t-1)));
            if(pos == N) break;
        }
    }
    ans -= ds.get(N-1);
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    testcase();
    return 0;
}
0