結果

問題 No.2364 Knapsack Problem
ユーザー t98slidert98slider
提出日時 2023-06-30 21:35:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 3,000 ms
コード長 1,250 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 170,720 KB
実行使用メモリ 67,920 KB
最終ジャッジ日時 2023-09-21 15:26:54
合計ジャッジ時間 3,250 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,808 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 7 ms
8,604 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 9 ms
10,292 KB
testcase_10 AC 3 ms
4,844 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 64 ms
67,792 KB
testcase_13 AC 65 ms
67,792 KB
testcase_14 AC 65 ms
67,796 KB
testcase_15 AC 65 ms
67,648 KB
testcase_16 AC 66 ms
67,792 KB
testcase_17 AC 64 ms
67,668 KB
testcase_18 AC 66 ms
67,640 KB
testcase_19 AC 66 ms
67,920 KB
testcase_20 AC 67 ms
67,656 KB
testcase_21 AC 64 ms
67,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, w;
    cin >> n >> m >> w;
    vector<ll> a(n), b(n), c(m), d(m);
    cin >> a >> b >> c >> d;
    const ll INF = (1ll << 60);
    ll ans = 0;
    vector<vector<ll>> dp(1 << (n + m), vector<ll>(w + 1, -INF));
    dp[0][0] = 0;
    for(int i = 0; i < (1 << (n + m)); i++){
        for(int j = 0; j <= w; j++){
            if(dp[i][j] == -INF) continue;
            for(int k = 0; k < (n + m); k++){
                if(i >> k & 1) continue;
                int nw = j + (k < n ? a.at(k) : -c.at(k - n));
                if(nw < 0 || nw > w) continue;
                ll nv = dp[i][j] + (k < n ? b[k] : -d[k - n]);
                dp[i | (1 << k)][nw] = max(dp[i | (1 << k)][nw], nv);
                ans = max(ans, nv);
            }
        }
    }
    cout << ans << '\n';
}
0