結果

問題 No.2617 容量3のナップザック
ユーザー KKT89KKT89
提出日時 2024-01-31 13:58:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,662 bytes
コンパイル時間 3,124 ms
コンパイル使用メモリ 222,100 KB
実行使用メモリ 46,852 KB
最終ジャッジ日時 2024-01-31 13:58:12
合計ジャッジ時間 8,232 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 118 ms
32,940 KB
testcase_13 AC 146 ms
39,868 KB
testcase_14 AC 117 ms
31,748 KB
testcase_15 AC 93 ms
26,412 KB
testcase_16 AC 84 ms
24,264 KB
testcase_17 AC 110 ms
30,520 KB
testcase_18 AC 113 ms
30,796 KB
testcase_19 AC 20 ms
8,204 KB
testcase_20 AC 136 ms
37,980 KB
testcase_21 AC 59 ms
17,888 KB
testcase_22 AC 147 ms
39,712 KB
testcase_23 AC 142 ms
39,004 KB
testcase_24 AC 8 ms
6,676 KB
testcase_25 AC 88 ms
25,556 KB
testcase_26 AC 91 ms
25,384 KB
testcase_27 AC 49 ms
15,220 KB
testcase_28 AC 6 ms
6,676 KB
testcase_29 AC 56 ms
17,512 KB
testcase_30 AC 138 ms
40,976 KB
testcase_31 AC 110 ms
29,956 KB
testcase_32 AC 152 ms
46,096 KB
testcase_33 AC 161 ms
46,088 KB
testcase_34 AC 162 ms
43,968 KB
testcase_35 AC 160 ms
46,020 KB
testcase_36 AC 189 ms
42,944 KB
testcase_37 AC 162 ms
46,096 KB
testcase_38 AC 161 ms
46,852 KB
testcase_39 AC 162 ms
42,740 KB
testcase_40 AC 163 ms
46,548 KB
testcase_41 AC 83 ms
42,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    ll seed, a, b, m;
    cin >> seed >> a >> b >> m;
    vector<ll> f(2 * n);
    f[0] = seed;
    for (int i = 1; i < 2 * n; i++) {
        f[i] = (f[i - 1] * a + b) % m;
    }
    vector<ll> v[3];
    vector<ll> rs[3];
    for (int i = 0; i < n; i++) {
        int w = f[i] % 3 + 1;
        v[w - 1].push_back(w * f[n + i]);
    }
    for (int i = 0; i < 3; i++) {
        sort(v[i].rbegin(), v[i].rend());
        rs[i].resize(v[i].size() + 1);
        for (int j = 0; j < v[i].size(); j++) {
            rs[i][j + 1] = rs[i][j] + v[i][j];
        }
    }

    vector<ll> dp(n + 1);
    int r = 0;
    for (int i = 1; i <= n; i++) {
        auto cal = [&](int t) -> ll {
            ll res = rs[1][min((int)v[1].size(), t)] + rs[0][min((int)v[0].size(), 3 * i - t * 2)];
            return res;
        };
        while (r < v[1].size() and r < i) {
            ll pre = cal(r);
            ll nxt = cal(r + 1);
            if (pre <= nxt) {
                r += 1;
            } else {
                break;
            }
        }
        dp[i] = cal(r);
    }

    ll res = 0;
    ll sum = 0;
    for (int i = 0; i <= v[2].size(); i++) {
        if (i > k) break;
        res = max(res, sum + dp[k - i]);
        if (i == v[2].size()) break;
        sum += v[2][i];
    }
    cout << res << endl;
}
0