QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109033 | #6194. Knapsack Problem | LG_Monkey | TL | 79ms | 128508kb | C++14 | 2.2kb | 2023-05-27 10:07:14 | 2023-05-27 10:07:17 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define pb push_back
#define se second
#define deb(x) cerr << #x << "=" << (x) << "; "
#define ll long long
int k, c[100], a[100];
const int blo = 12;
ll dp[33][17][blo + 1][blo + 1][blo + 1][blo + 1];
signed main() {
int T;
cin >> T;
while (T--) {
memset(dp, 0xcf, sizeof dp);
cin >> k;
for (int i = 1; i < (1ll << k); i++) cin >> c[i];
for (int i = 0; i < k; i++) cin >> a[i];
dp[0][1][0][0][0][0] = 0;
for (int i = 0; i <= 31; i++) {
for (int j = 1; j < (1ll << k); j++) {
for (int a1 = 0; a1 <= blo; a1++) {
for (int a2 = 0; a2 <= blo; a2++) {
for (int a3 = 0; a3 <= blo; a3++) {
for (int a4 = 0; a4 <= blo; a4++) {
// x[j][i] = 0
dp[i][j + 1][a1][a2][a3][a4] = max(dp[i][j + 1][a1][a2][a3][a4], dp[i][j][a1][a2][a3][a4]);
// x[j][i] = 1
int b1 = a1, b2 = a2, b3 = a3, b4 = a4;
if (j & 1) b1 += 2;
if (j & 2) b2 += 2;
if (j & 4) b3 += 2;
if (j & 8) b4 += 2;
if (b1 <= blo && b2 <= blo && b3 <= blo && b4 <= blo) dp[i][j + 1][b1][b2][b3][b4] = max(dp[i][j + 1][b1][b2][b3][b4], dp[i][j][a1][a2][a3][a4] + c[j] * (1ll << i));
}
}
}
}
}
for (int a1 = 0; a1 <= blo; a1++) {
for (int a2 = 0; a2 <= blo; a2++) {
for (int a3 = 0; a3 <= blo; a3++) {
for (int a4 = 0; a4 <= blo; a4++) {
bool flag = 1;
if (k >= 1) {
flag &= (bool)(a1 & 2) == (bool)(a[0] & (1ll << i));
}
if (k >= 2) {
flag &= (bool)(a2 & 2) == (bool)(a[1] & (1ll << i));
}
if (k >= 3) {
flag &= (bool)(a3 & 2) == (bool)(a[2] & (1ll << i));
}
if (k >= 4) {
flag &= (bool)(a4 & 2) == (bool)(a[3] & (1ll << i));
}
if (flag) {
ll x = dp[i][1 << k][a1][a2][a3][a4];
dp[i + 1][1][a1 / 2][a2 / 2][a3 / 2][a4 / 2] = max(dp[i + 1][1][a1 / 2][a2 / 2][a3 / 2][a4 / 2], x);
}
}
}
}
}
}
cout << dp[32][1][0][0][0][0] << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 79ms
memory: 128508kb
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
Time Limit Exceeded
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 34577176834491128 9058894962996375 21525085254465501 6657186892229297 16750628911275484 11217846865301187 12674726744043780 33873234230125448 3890237279573366 319038768614465...