結果

問題 No.2028 Even Choice
ユーザー Iván SotoIván Soto
提出日時 2022-08-05 23:05:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 780 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 203,236 KB
実行使用メモリ 4,960 KB
最終ジャッジ日時 2023-10-14 00:51:42
合計ジャッジ時間 4,100 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
4,496 KB
testcase_01 AC 35 ms
4,960 KB
testcase_02 AC 34 ms
4,440 KB
testcase_03 AC 26 ms
4,828 KB
testcase_04 AC 26 ms
4,828 KB
testcase_05 AC 9 ms
4,352 KB
testcase_06 AC 31 ms
4,480 KB
testcase_07 AC 10 ms
4,348 KB
testcase_08 AC 20 ms
4,352 KB
testcase_09 AC 24 ms
4,348 KB
testcase_10 AC 9 ms
4,348 KB
testcase_11 AC 7 ms
4,348 KB
testcase_12 AC 3 ms
4,352 KB
testcase_13 AC 22 ms
4,644 KB
testcase_14 AC 9 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 15 ms
4,348 KB
testcase_29 AC 11 ms
4,348 KB
testcase_30 AC 8 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <algo/debug.h>
#else
#define debug(...) 42
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  priority_queue<int, vector<int>, greater<int>> big;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  long long sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (i % 2 == 1 && big.size() == k - 1) {
      ans = max(ans, sum + a[i]);
    }
    if (big.size() < k - 1) {
      big.push(a[i]);
      sum += a[i];
    } else {
      if (!big.empty() && big.top() < a[i]) {
        int top = big.top(); big.pop();
        big.push(a[i]);
        sum += a[i] - top;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
0