結果

問題 No.2139 K Consecutive Sushi
ユーザー tnakao0123tnakao0123
提出日時 2022-12-03 18:13:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,743 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 56,088 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-19 04:23:39
合計ジャッジ時間 2,931 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 66 ms
8,192 KB
testcase_04 AC 79 ms
8,320 KB
testcase_05 AC 71 ms
8,320 KB
testcase_06 AC 62 ms
8,320 KB
testcase_07 AC 71 ms
8,268 KB
testcase_08 AC 70 ms
8,116 KB
testcase_09 AC 77 ms
8,192 KB
testcase_10 AC 79 ms
8,296 KB
testcase_11 AC 71 ms
8,320 KB
testcase_12 AC 72 ms
8,320 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 1 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 25 ms
5,376 KB
testcase_25 AC 55 ms
7,936 KB
testcase_26 AC 8 ms
5,376 KB
testcase_27 AC 22 ms
5,376 KB
testcase_28 AC 19 ms
5,376 KB
testcase_29 AC 11 ms
5,376 KB
testcase_30 AC 26 ms
5,760 KB
testcase_31 AC 70 ms
8,112 KB
testcase_32 AC 10 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2139.cc:  No.2139 K Consecutive Sushi - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

template <typename T>
struct SegTreeMin {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeMin() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int as[MAX_N + 2];
SegTreeMin<ll> st;

/* subroutines */

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", as + i);
  as[0] = as[n + 1] = 0;

  st.init(n + 2, LINF);
  st.set(0, 0);

  for (int i = 1; i <= n + 1; i++) {
    ll e = st.min_range(i - k, i);
    st.set(i, e + as[i]);
  }

  ll sum = 0;
  for (int i = 1; i <= n; i++) sum += as[i];

  printf("%lld\n", sum - st.geti(n + 1));
  return 0;
}
0