結果

問題 No.2139 K Consecutive Sushi
ユーザー LanatusLanatus
提出日時 2023-03-27 01:47:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,925 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 101,664 KB
実行使用メモリ 8,312 KB
最終ジャッジ日時 2023-10-19 13:57:55
合計ジャッジ時間 5,449 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 124 ms
8,272 KB
testcase_04 AC 135 ms
8,272 KB
testcase_05 AC 126 ms
8,272 KB
testcase_06 AC 120 ms
8,272 KB
testcase_07 AC 129 ms
8,272 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 132 ms
8,272 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 WA -
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 13 ms
4,348 KB
testcase_24 AC 41 ms
4,644 KB
testcase_25 AC 91 ms
8,008 KB
testcase_26 AC 14 ms
4,348 KB
testcase_27 AC 35 ms
4,644 KB
testcase_28 AC 36 ms
4,644 KB
testcase_29 AC 18 ms
4,348 KB
testcase_30 AC 47 ms
5,700 KB
testcase_31 AC 117 ms
8,008 KB
testcase_32 AC 19 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bitset>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>

using namespace std;

struct UnionFind {
  int n;
  vector<int> parent, siz;
 
  UnionFind(int n) : n(n) {
    parent.resize(n);
    siz.resize(n);
 
    for (int i = 0; i < n; ++i) {
      parent[i] = i;
      siz[i] = 1;
    }
  }
 
  int root(int x) {
    if (parent[x] == x) return x;
    return parent[x] = root(parent[x]);
  }
 
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
 
    if (x == y) return false;
 
    if (siz[x] < siz[y]) swap(x, y);
    parent[y] = x;
    siz[x] += siz[y];
 
    return true;
  }
 
  int same(int x, int y) {
    return root(x) == root(y);
  }
 
  int size(int x) {
    return siz[root(x)];
  }
};

using ll = long long;

struct RminQ {
  const ll INFLL = 1e18;
  int m;
  vector<long long> dat;
 
  RminQ(int n) {
    m = 1;
    while (m < n) m *= 2;
 
    dat.assign(2 * m - 1, INFLL);
  }
 
  void update(int i, long long x) {
    i += m - 1;
    dat[i] = x;
 
    while (i) {
      i = (i - 1) / 2;
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
 
  // [a, b)
  long long query(int a, int b, int k, int l, int r) {
    if (l >= b || r <= a) return INFLL;
    if (a <= l && r <= b) return dat[k];
    
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
 
    return min(vl, vr);
  }
 
  long long query_sub(int a, int b) {
    return query(a, b, 0, 0, m);
  }
};

int main(void) {
  int n, k;
  cin >> n >> k;

  vector<int> a(n + 2, 0);
  for (int i = 1; i <= n; ++i) cin >> a[i];

  RminQ seg(n + 5);
  seg.update(0, 0);

  for (int i = 1; i <= n + 1; ++i) {
    int mn = seg.query_sub(max(i - k, 0), i);
    seg.update(i, mn + a[i]);
  }

  long long ans = 0;
  for (int i = 0; i < n + 2; ++i) ans += a[i];

  ans -= seg.query_sub(n+1, n+2);

  cout << ans << endl;
  return 0;
}
0