結果

問題 No.2364 Knapsack Problem
ユーザー k1suxuk1suxu
提出日時 2023-06-30 23:11:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 3,000 ms
コード長 1,873 bytes
コンパイル時間 2,770 ms
コンパイル使用メモリ 254,592 KB
実行使用メモリ 68,448 KB
最終ジャッジ日時 2023-09-21 17:28:19
合計ジャッジ時間 5,082 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
5,364 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 13 ms
8,812 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 14 ms
10,496 KB
testcase_10 AC 6 ms
4,892 KB
testcase_11 AC 4 ms
4,720 KB
testcase_12 AC 138 ms
68,312 KB
testcase_13 AC 131 ms
68,196 KB
testcase_14 AC 136 ms
68,256 KB
testcase_15 AC 142 ms
68,304 KB
testcase_16 AC 135 ms
68,312 KB
testcase_17 AC 140 ms
68,248 KB
testcase_18 AC 133 ms
68,260 KB
testcase_19 AC 145 ms
68,308 KB
testcase_20 AC 144 ms
68,448 KB
testcase_21 AC 134 ms
68,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

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

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

void solve() {
    int n, m, w;
    cin >> n >> m >> w;
    vi a(n), b(n), c(m), d(m);
    FOR(n) cin >> a[i];
    FOR(n) cin >> b[i];
    FOR(m) cin >> c[i];
    FOR(m) cin >> d[i];

    vvvi dp(1<<n, vvi(1<<m, vi(w+1, -INF)));
    dp[0][0][0] = 0;

    rep(i, 1<<n) {
        rep(j, 1<<m) {
            rep(k, n) if(!(i>>k&1)) {
                rep(l, w+1) if(l+a[k] <= w) {
                    chmax(dp[i|(1<<k)][j][l+a[k]], dp[i][j][l] + b[k]);
                }
            }
            rep(k, m) if(!(j>>k&1)) {
                rep(l, w+1) if(l-c[k] >= 0) {
                    chmax(dp[i][j|(1<<k)][l-c[k]], dp[i][j][l] - d[k]);
                }
            }
        }
    }

    int ans = 0;
    rep(i, 1<<n) rep(j, 1<<m) rep(k, w+1) chmax(ans, dp[i][j][k]);
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0