QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#559522#6194. Knapsack Problempandapythoner#WA 2ms3636kbC++231.5kb2024-09-11 22:49:472024-09-11 22:49:47

Judging History

你现在查看的是最新测评结果

  • [2024-09-11 22:49:47]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3636kb
  • [2024-09-11 22:49:47]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;

using ll = long long;
using ld = long double;

#define len(a) ((int)(a).size())
#define rep(i, n) for(int i = 0; i < (n); i += 1)

const ll INF = 1'000'000'000'000'000'000;

mt19937 rnd(234);
vector<ll> cs, as;
int k;

ll rec(ll it) {
    if (it == 0) {
        bool flag = true;
        for (int i = 0; i < k; ++i) flag &= (as[i] == 0);
        if (flag) return 0;
        return -INF;
    }
    ll bst = -INF;
    for (int i = 1; i < cs.size(); ++i) {
        ll mn = INF;
        for (int j = 0; j < k; ++j) {
            if ((i >> j) & 1) {
                mn = min(mn, as[j]);
            }
        }

        assert(mn != INF);
        if (mn != 0) {
            for (int j = 0; j < k; ++j) {
                if ((i >> j) & 1) as[j] -= mn;
            }
            bst = max(bst, rec(it - 1) + cs[i] * mn);
            for (int j = 0; j < k; ++j) {
                if ((i >> j) & 1) as[j] += mn;
            }
        }
    }

    return bst;
}

void solve() {
    cin >> k;
    cs.resize((1 << k) - 1);
    as.resize(k);
    for (auto& c : cs) cin >> c;
    cs.insert(cs.begin(), 0);
    for (auto& c : as) cin >> c;

    ll ans = rec(k);

    cout << ans << "\n";
}

int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }

    int t;
    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

3
2
1 2 4
4 5
3
3226252 19270256 2430652 1187613 12496062 10286165 17494834
24 85 34
4
901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555
529321239 218214127 92942310 207467810

output:

18
1949753378
7832404777567179

result:

ok 3 number(s): "18 1949753378 7832404777567179"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3564kb

input:

100
4
4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493
200477197 258791439 590664017 982949628
4
5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...

output:

16156444320083421
24565535429631234
29418802996321901
30685727712782520
21026118053726857
27272339796311010
28831028447377015
34577176478351368
9058894962996375
21525085254465501
6657186892229297
16750628911275484
11217846865301187
12674726744043780
33873234230125448
3890237279573366
319038768614465...

result:

wrong answer 8th numbers differ - expected: '34577176834491128', found: '34577176478351368'