結果

問題 No.2370 He ate many cakes
ユーザー 👑 tatyamtatyam
提出日時 2023-07-03 21:10:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 793 bytes
コンパイル時間 4,589 ms
コンパイル使用メモリ 251,980 KB
実行使用メモリ 79,576 KB
最終ジャッジ日時 2023-09-24 18:16:03
合計ジャッジ時間 5,435 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 157 ms
77,556 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 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 16 ms
11,380 KB
testcase_11 AC 54 ms
26,892 KB
testcase_12 AC 175 ms
66,856 KB
testcase_13 AC 201 ms
53,840 KB
testcase_14 AC 97 ms
48,452 KB
testcase_15 AC 291 ms
79,576 KB
testcase_16 AC 204 ms
65,404 KB
testcase_17 AC 279 ms
73,356 KB
testcase_18 AC 68 ms
5,200 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    vector<ll> A(N);
    ll max = 0;
    for(ll& a : A) {
        cin >> a;
        if(a >= 0) max += a;
        else a = -a;
    }
    static bitset<1 << 30> used;
    priority_queue q(greater<>{}, vector<pair<ll, int>>{});
    auto push = [&](int bit, ll cost) {
        if(used[bit]) return;
        used[bit] = 1;
        q.emplace(cost, bit);
    };
    push(0, 0);
    while(1) {
        auto [c, bit] = q.top();
        q.pop();
        if(--K == 0) {
            cout << max - c << endl;
            return 0;
        }
        for(int i = 0; i < N; i++) if(~bit & 1 << i) push(bit | 1 << i, c + A[i]);
    }
}
0