結果

問題 No.2139 K Consecutive Sushi
ユーザー otoshigootoshigo
提出日時 2022-12-03 03:55:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,773 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 200,480 KB
実行使用メモリ 8,924 KB
最終ジャッジ日時 2024-04-18 12:57:09
合計ジャッジ時間 4,441 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 50 ms
8,832 KB
testcase_04 AC 70 ms
8,832 KB
testcase_05 AC 54 ms
8,704 KB
testcase_06 AC 38 ms
8,892 KB
testcase_07 AC 52 ms
8,756 KB
testcase_08 AC 65 ms
8,832 KB
testcase_09 AC 69 ms
8,748 KB
testcase_10 AC 67 ms
8,896 KB
testcase_11 AC 66 ms
8,924 KB
testcase_12 AC 64 ms
8,832 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 19 ms
5,376 KB
testcase_25 AC 45 ms
8,356 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
testcase_29 AC 8 ms
5,376 KB
testcase_30 AC 18 ms
5,888 KB
testcase_31 AC 53 ms
8,576 KB
testcase_32 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

template <class S, S (*op)(S, S), S (*e)()>
struct segtree{
    int n;
    vector<S> dat;
    segtree(int N){
        int x = 1;
        while (x < N) x *= 2;
        n = x;
        dat.resize(2 * n - 1, e());
    }
    void set(int i, S x){
        assert(i >= 0 && i < n);
        i += n - 1;
        dat[i] = x;
        while (i > 0){
            i = (i - 1) / 2;
            dat[i] = op(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }
    S get(int i){
        assert(i >= 0 && i < n);
        i += n - 1;
        return dat[i];
    }
    S prod(int a, int b, int k = 0, int l = 0, int r = -1){
        if (r < 0) r = n;
        if (r <= a || l >= b) return e();
        if (l >= a && r <= b) return dat[k];
        else{
            S vl = prod(a, b, 2 * k + 1, l, (l + r) / 2);
            S vr = prod(a, b, 2 * k + 2, (l + r) / 2, r);
            return op(vl, vr);
        }
    }
};

ll op(ll l,ll r){
    return min(l,r);
}

ll e(){
    return 1LL<<60;
}

int n,k;
ll A[200010];
int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cin>>n>>k;
    rep(i,n)cin>>A[i];
    segtree<ll,op,e>seg(n);
    rep(i,k)seg.set(i,A[i]);
    for(int i=k;i<n;i++){
        ll m=seg.prod(i-k,i);
        seg.set(i,m+A[i]);
    }
    ll ans=1LL<<60;
    for(int i=n-k;i<n;i++)chmin(ans,seg.get(i));
    ans=-ans;
    rep(i,n)ans+=A[i];
    cout<<ans<<"\n";
}
0