結果

問題 No.2370 He ate many cakes
ユーザー simansiman
提出日時 2023-07-04 01:51:04
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,806 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 108,712 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 23:19:22
合計ジャッジ時間 2,971 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 13 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 9 ms
4,376 KB
testcase_11 AC 13 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 18 ms
4,380 KB
testcase_14 AC 12 ms
4,376 KB
testcase_15 AC 15 ms
4,376 KB
testcase_16 AC 18 ms
4,380 KB
testcase_17 AC 31 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

vector<ll> A;

vector<ll> build_values(int from, int to) {
  vector<ll> values;
  values.push_back(0);

  for (int i = from; i <= to; ++i) {
    vector<ll> temp = values;

    for (ll v : values) {
      ll nv = v + A[i];
      temp.push_back(nv);
    }

    values = temp;
  }

  sort(values.begin(), values.end());
  return values;
}

int main() {
  ll N, K;
  cin >> N >> K;
  A.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  if (N == 1) {
    if (K == 1) {
      cout << 0 << endl;
    } else {
      cout << A[0] << endl;
    }
  } else {
    int mid = N / 2;
    vector<ll> memo1 = build_values(0, mid - 1);
    vector<ll> memo2 = build_values(mid, N - 1);

    /*
    for (ll v1 : memo1) {
      cerr << v1 << " ";
    }
    cerr << endl;
    for (ll v2 : memo2) {
      cerr << v2 << " ";
    }
    cerr << endl;
    */

    ll ok = LLONG_MAX - 1;
    ll ng = LLONG_MIN;

    while (abs(ok - ng) >= 2) {
      ll x = (ok + ng) / 2;
      ll cnt = 0;

      for (ll v1 : memo1) {
        if (v1 + memo2.back() < x) continue;
        ll ok2 = memo2.size() - 1;
        ll ng2 = -1;

        while (abs(ok2 - ng2) >= 2) {
          ll idx = (ok2 + ng2) / 2;
          ll nv = v1 + memo2[idx];

          if (nv >= x) {
            ok2 = idx;
          } else {
            ng2 = idx;
          }
        }

        cnt += memo2.size() - ok2;
      }

      // fprintf(stderr, "x: %lld, cnt: %lld\n", x, cnt);

      if (cnt >= K) {
        ok = x;
      } else {
        ng = x;
      }
    }

    cout << ok << endl;
  }

  return 0;
}
0