結果

問題 No.2139 K Consecutive Sushi
ユーザー pitPpitP
提出日時 2022-12-19 11:21:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 4,865 bytes
コンパイル時間 4,421 ms
コンパイル使用メモリ 265,584 KB
実行使用メモリ 13,484 KB
最終ジャッジ日時 2024-05-05 19:24:02
合計ジャッジ時間 5,577 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 32 ms
12,000 KB
testcase_04 AC 29 ms
7,020 KB
testcase_05 AC 31 ms
10,444 KB
testcase_06 AC 33 ms
13,484 KB
testcase_07 AC 30 ms
10,476 KB
testcase_08 AC 26 ms
5,376 KB
testcase_09 AC 28 ms
5,376 KB
testcase_10 AC 28 ms
5,376 KB
testcase_11 AC 28 ms
5,376 KB
testcase_12 AC 28 ms
5,376 KB
testcase_13 AC 1 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 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 19 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 9 ms
5,376 KB
testcase_28 AC 10 ms
5,456 KB
testcase_29 AC 6 ms
5,376 KB
testcase_30 AC 12 ms
6,052 KB
testcase_31 AC 25 ms
7,424 KB
testcase_32 AC 6 ms
5,376 KB
testcase_33 AC 25 ms
9,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); } 

typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const int MOD = 998244353;
const int MOD2 = 1e9+7;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};

template<typename T>
void print_2dvector(vector<vector<T>> &vec){
    int H = vec.size();
    for(int i=0;i<H;i++){
        cout << i << ' ' << '[';
        for(int j=0;j<vec[i].size();j++){
            if (j) cout << ',' << ' ';
            cout << vec[i][j];
        }
        cout << ']' << endl;
    }
}

template<typename T>
void print_vector(vector<T> &vec){
    int N = vec.size();
    cout << '[';
    for(int i=0;i<N;i++){
        if (i) cout << ',' << ' ';
        cout << vec[i];
    }
    cout << ']' << endl;
}

template <class T, T (*op)(T,T), T (*e)()>
struct SWAG {
    vector<T> top, bottom, topfold, bottomfold;
    SWAG(){
        topfold.push_back(e());
        bottomfold.push_back(e());
    }
    void _pushbottom(T x){
        bottom.push_back(x);
        bottomfold.push_back(op(bottomfold.back(), x));
    }
    T _popbottom(){
        auto res = bottom.back();
        bottom.pop_back();
        bottomfold.pop_back();
        return res;
    }
    void _pushtop(T x){
        top.push_back(x);
        topfold.push_back(op(x, topfold.back()));
    }
    T _poptop(){
        auto res = top.back();
        top.pop_back();
        topfold.pop_back();
        return res;
    }
    void push(T x){
        _pushbottom(x);
    }
    void pushleft(T x){
        _pushtop(x);
    }
    T fold(){
        return op(topfold.back(), bottomfold.back());
    }
    T popleft(){
        if (top.empty()){
            vector<T> stack_;
            while(!bottom.empty()){
                auto x = _popbottom();
                stack_.push_back(x);
            }
            int N = stack_.size();
            reverse(all(stack_));
            vector<T> stack1, stack2;
            for(int i=0;i<N;i++){
                if (i < (N+1)/2) stack1.push_back(stack_[i]);
                else stack2.push_back(stack_[i]);
            }
            reverse(all(stack2));
            for(int i=0;i<(N+1)/2;i++){
                _pushtop(stack1.back());
                stack1.pop_back();
            }
            for(int i=0;i<(N/2);i++){
                _pushbottom(stack2.back());
                stack2.pop_back();
            }
        }
        if (top.empty()) return e();
        else return _poptop();
    }
    T pop(){
        if (bottom.empty()){
            vector<T> stack_;
            while(!top.empty()){
                auto x = _poptop();
                stack_.push_back(x);
            }
            int N = stack_.size();
            vector<T> stack1, stack2;
            for(int i=0;i<N;i++){
                if (i < (N/2)) stack1.push_back(stack_[i]);
                else stack2.push_back(stack_[i]);
            }
            reverse(all(stack2));
            for(int i=0;i<(N+1)/2;i++){
                _pushtop(stack2.back());
                stack2.pop_back();
            }
            for(int i=0;i<(N/2);i++){
                _pushtop(stack1.back());
                stack1.pop_back();
            }
        }
        if (bottom.empty()) return e();
        else return _popbottom();
    }
};

using S = ll;
S op(S a, S b){return min(a, b);}
S e(){return LINF;}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    SWAG<S,op,e> sw;
    int N,K; cin >> N >> K;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    ll ans = 0ll;
    rep(i,N) ans += A[i];
    for(int i=0;i<K;i++){
        sw.push(A[i]);
    }
    for(int i=K;i<N;i++){
        sw.push(sw.fold() + A[i]);
        sw.popleft();
    }
    ans -= sw.fold();
    cout << ans << endl;
}
0