結果

問題 No.2028 Even Choice
ユーザー tnakao0123tnakao0123
提出日時 2022-08-13 01:53:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 58,684 KB
実行使用メモリ 15,016 KB
最終ジャッジ日時 2023-10-24 13:38:55
合計ジャッジ時間 4,406 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
10,300 KB
testcase_01 AC 141 ms
12,536 KB
testcase_02 AC 100 ms
8,852 KB
testcase_03 AC 84 ms
15,016 KB
testcase_04 AC 76 ms
13,536 KB
testcase_05 AC 24 ms
5,308 KB
testcase_06 AC 91 ms
8,564 KB
testcase_07 AC 29 ms
5,852 KB
testcase_08 AC 44 ms
5,600 KB
testcase_09 AC 84 ms
9,360 KB
testcase_10 AC 31 ms
6,392 KB
testcase_11 AC 13 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 107 ms
12,084 KB
testcase_14 AC 20 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 31 ms
4,756 KB
testcase_29 AC 21 ms
4,348 KB
testcase_30 AC 14 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2028.cc:  No.2028 Even Choice - yukicoder
 */

#include<cstdio>
#include<set>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef set<pii> spii;

/* global variables */

int as[MAX_N];
pii ps[MAX_N];

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++) ps[i] = pii(as[i], i);
  sort(ps, ps + n, greater<pii>());

  ll sum = 0;
  spii q;
  for (int i = 0; i < k - 1; i++) {
    sum += ps[i].first;
    q.insert(pii(ps[i].second, ps[i].first));
  }

  ll maxs = 0;
  for (int i = 1, j = k - 1; i < n; i += 2) {
    while (! q.empty() && q.begin()->first <= i) {
      sum -= q.begin()->second;
      q.erase(q.begin());
    }

    while (j < n && q.size() < k - 1) {
      if (ps[j].second > i) {
	sum += ps[j].first;
	q.insert(pii(ps[j].second, ps[j].first));
      }
      j++;
    }
    if (q.size() < k - 1) break;

    maxs = max(maxs, sum + as[i]);
  }

  printf("%lld\n", maxs);
  return 0;
}
0