結果

問題 No.1248 Kth Sum
ユーザー 👑 Nachia
提出日時 2020-10-02 22:29:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,029 bytes
コンパイル時間 1,408 ms
コンパイル使用メモリ 172,376 KB
実行使用メモリ 47,408 KB
最終ジャッジ日時 2024-07-17 22:02:47
合計ジャッジ時間 5,063 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N, K;
LL A[200000];
int P[18][200000] = {};
LL S[18][200000] = {};

LL getsum(int p, int q) {
    LL ans = 0;
    for (int d = 17; d >= 0; d--) {
        if (q < (1 << d)) continue;
        ans += S[d][p];
        p = P[d][p];
        q -= (1 << d);
    }
    return ans;
}

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

    deque<int> wnd;
    rep(i, N) {
        if (i >= K) P[0][i] = wnd.front();
        while (wnd.size()) { if (A[wnd.back()] < A[i]) break; wnd.pop_back(); }
        wnd.push_back(i);
        if (wnd.front() + K <= i) wnd.pop_front();
    }
    
    rep(d, 17) rep(i, N) P[d + 1][i] = P[d][P[d][i]];

    rep(i, N) S[0][i] = A[i];
    rep(d, 17) rep(i, N) S[d + 1][i] = S[d][i] + S[d][P[d][i]];

    LL ans = 1000000000000000000;
    for (int i = 1; i * K <= N; i++) ans = min(ans, getsum(i * K - 1, i));
    cout << ans << endl;

    return 0;
}
0