結果

問題 No.2364 Knapsack Problem
ユーザー taigatappuritaigatappuri
提出日時 2023-07-01 00:04:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 3,000 ms
コード長 3,351 bytes
コンパイル時間 2,394 ms
コンパイル使用メモリ 185,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 18:09:35
合計ジャッジ時間 3,400 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint998244353;
using Graph = vector<vector<long long>>;
#define all(v) v.begin(), v.end()
#define inf 2147483647
#define INF 9223372036854775807
#define gcd(a, b) __gcd((a), (b))
#define ll long long
#define fi first
#define se second
#define mod107 1000000007
#define mod998 998244353;
#define eps 1e-9
#define PI acos(-1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(x) std::clog << #x ": " << (x) << std::endl
template <class T>
using max_heap = priority_queue<T>;
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<>>;
template <class T>
istream &operator>>(istream &s, vector<T> &v)
{
    for (auto &e : v)
        s >> e;
    return s;
}
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v)
{
    for (auto &e : v)
        s << e << ' ';
    return s;
}
template <class T, class U>
ostream &operator<<(ostream &s, const pair<T, U> &p)
{
    s << p.first << ' ' << p.second;
    return s;
}

int dx[8] = {-1, 0, 1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

struct edge
{
    ll to, cost;
};
ll lcm(ll a, ll b)
{ // オーバーフローしない
    return a / __gcd(a, b) * b;
}



int main()
{   ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll N,M,W;
    cin >> N >> M >> W;
    vector<ll> A(N);
    vector<ll> B(N);
    vector<ll> C(M);
    vector<ll> D(M);

    cin >> A;
    cin >> B;
    cin >> C;
    cin >> D;

    ll ans = 0;
    ll nm = N + M;
    vector<ll> s(1 << nm,0);

    for(int i = 0;i < (1 << nm);i++){
        for(int j = 0;j < N;j++){
            if((i >> j) & 1 == 1){
                s[i] += A[j];
            }
        }
        for(int j = 0;j < M;j++){
            if((i >> (N + j) & 1) == 1){
                s[i] -= C[j];
            }
        }
    }
    vector<ll> dp(1 << nm,-inf);
    dp[0] = 0;
    // for (int i = 0; i < (1 << (N + M)); i++){
    //     if (0 <= s[i] && s[i] <= W){
    //         for (int j = 0; j < N + M; j++){
    //             if ((i >> j & 1) == 0){
    //                 int i2 = i | (1 << j);
    //                 if (0 <= s[i2] && s[i2] <= W){
    //                     if (j < N){
    //                     dp[i2] = max(dp[i2], dp[i] + B[j]);
    //                     } else {
    //                     dp[i2] = max(dp[i2], dp[i] - D[j - N]);
    //                     }
    //                 }
    //             }
    //         }
    //     }
    // } 
    for(int i = 0;i < (1 << nm);i++){
        if(0 <= s[i] && s[i] <= W){
            for(int j = 0;j < nm;j++){
                if((i >> j & 1) == 0){
                    int i2 = i | (1 << j);
                    if(0 <= s[i2] && s[i2] <= W){
                        if(j < N){
                            dp[i2] = max(dp[i2],dp[i] + B[j]);
                        }
                        else{
                            dp[i2] = max(dp[i2],dp[i] - D[j - N]);
                        }
                    }
                }
            }
        }
    }
    for(int i = 0;i < (1 << nm);i++){
        ans = max(ans,dp[i]);
    }
    cout << ans << endl;
}
0