結果

問題 No.2029 Swap Min Max Min
ユーザー tnakao0123
提出日時 2022-08-13 02:39:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,045 bytes
コンパイル時間 519 ms
コンパイル使用メモリ 58,624 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-09-23 06:34:35
合計ジャッジ時間 5,211 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 42
権限があれば一括ダウンロードができます

ソースコード

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>());

  spii q;
  ll maxs = 0, sum = 0;
  for (int i = 1, j = 0; 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