結果

問題 No.2364 Knapsack Problem
ユーザー 👑 hitonanodehitonanode
提出日時 2023-06-30 23:12:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 3,000 ms
コード長 1,339 bytes
コンパイル時間 989 ms
コンパイル使用メモリ 98,388 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-21 17:29:47
合計ジャッジ時間 2,350 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,500 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using lint = long long;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; }


int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);

    int N, M, W;
    cin >> N >> M >> W;
    vector<int> A(N), B(N), C(M), D(M);
    cin >> A >> B >> C >> D;

    constexpr lint inf = 1LL << 60;
    vector<lint> dp(1 << (N + M), -inf);
    vector<lint> weight(1 << (N + M), 0);
    dp.front() = 0;

    FOR(S, 1, 1 << (N + M)) {
        int i = floor_lg(S);
        weight.at(S) = weight.at(S - (1 << i)) + (i < N ? A.at(i) : -C.at(i - N));
    }

    REP(S, 1 << (N + M)) {
        REP(i, N + M) {
            if ((S >> i) & 1) continue;
            if (weight.at(S | (1 << i)) > W) continue;
            if (weight.at(S | (1 << i)) < 0) continue;
            chmax(dp.at(S | (1 << i)), dp.at(S) + (i < N ? B.at(i) : -D.at(i - N)));
        }
    }

    cout << *max_element(dp.cbegin(), dp.cend()) << "\n";
}
0