結果

問題 No.2617 容量3のナップザック
ユーザー Today03Today03
提出日時 2024-01-26 23:00:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,209 bytes
コンパイル時間 2,819 ms
コンパイル使用メモリ 214,596 KB
実行使用メモリ 13,352 KB
最終ジャッジ日時 2024-01-26 23:01:01
合計ジャッジ時間 6,680 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,352 KB
testcase_01 AC 3 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include "./debug.cpp"
#else
#define debug(...)
#define print_line
#endif
using namespace std;
using ll = long long;

int main() {
    int N, K;
    ll seed, a, b, m;
    cin >> N >> K >> seed >> a >> b >> m;
    vector<ll> F(2 * N + 1), W(N), V(N);
    F[0] = seed;
    for (int i = 0; i < 2 * N; i++) F[i + 1] = (a * F[i] + b) % m;
    for (int i = 0; i < N; i++) {
        W[i] = F[i] % 3 + 1;
        V[i] = W[i] * F[N + i];
    }

    priority_queue<ll> PQ1, PQ2, PQ3;
    for (int i = 0; i < N; i++) {
        if (W[i] == 1) {
            PQ1.push(V[i]);
        } else if (W[i] == 2) {
            PQ2.push(V[i]);
        } else {
            PQ3.push(V[i]);
        }
    }

    while (!PQ1.empty()) {
        if (PQ1.size() >= 3) {
            ll a = PQ1.top();
            PQ1.pop();
            ll b = PQ1.top();
            PQ1.pop();
            ll c = PQ1.top();
            PQ1.pop();
            if (!PQ2.empty()) {
                ll d = PQ2.top();
                PQ2.pop();
                if (b + c > d) {
                    PQ3.push(a + b + c);
                    PQ2.push(d);
                } else {
                    PQ3.push(a + d);
                    PQ1.push(b);
                    PQ1.push(c);
                }
            }
        } else if (PQ1.size() == 2) {
            ll a = PQ1.top();
            PQ1.pop();
            ll b = PQ1.top();
            PQ1.pop();
            if (!PQ2.empty()) {
                ll d = PQ2.top();
                PQ2.pop();
                if (a + b > d) {
                    PQ2.push(a + b);
                    PQ2.push(d);
                } else {
                    break;
                }
            }
        }
    }

    while (!PQ2.empty()) {
        ll b = PQ2.top();
        PQ2.pop();
        if (!PQ1.empty()) {
            ll a = PQ1.top();
            PQ1.pop();
            PQ3.push(a + b);
        } else {
            PQ3.push(b);
        }
    }

    while (!PQ1.empty()) {
        ll a = PQ1.top();
        PQ1.pop();
        PQ3.push(a);
    }

    ll ans = 0;
    while (K-- && !PQ3.empty()) {
        ans += PQ3.top();
        PQ3.pop();
    }

    cout << ans << endl;
}
0