結果

問題 No.2364 Knapsack Problem
ユーザー FplusFplusFFplusFplusF
提出日時 2023-06-30 21:56:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,229 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 207,448 KB
実行使用メモリ 68,292 KB
最終ジャッジ日時 2023-09-21 15:51:29
合計ジャッジ時間 9,109 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 16 ms
5,588 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 46 ms
8,628 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 53 ms
10,548 KB
testcase_10 AC 16 ms
4,836 KB
testcase_11 AC 9 ms
4,924 KB
testcase_12 AC 586 ms
68,240 KB
testcase_13 AC 591 ms
68,256 KB
testcase_14 AC 597 ms
68,292 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 593 ms
68,292 KB
testcase_18 WA -
testcase_19 AC 634 ms
68,200 KB
testcase_20 AC 604 ms
68,188 KB
testcase_21 AC 587 ms
68,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,w;
    cin >> n >> m >> w;
    vector<ll> a(n),b(n),c(m),d(m);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,m) cin >> c[i];
    rep(i,m) cin >> d[i];
    vector<vector<vector<ll>>> dp(1<<n,vector<vector<ll>>(1<<m,vector<ll>(w+1,-INF)));
    dp[0][0][0]=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                rep(l,n){
                    if(i&(1<<l)) continue;
                    if(k+a[l]<=w) chmax(dp[i|(1<<l)][j][k+a[l]],dp[i][j][k]+b[l]);
                    chmax(dp[i|(1<<l)][j][k],dp[i][j][k]);
                }
                rep(l,m){
                    if(i&(1<<l)) continue;
                    if(0<=k-c[l]) chmax(dp[i][j|(1<<l)][k-c[l]],dp[i][j][k]-d[l]);
                    chmax(dp[i][j|(1<<l)][k],dp[i][j][k]);
                }
            }
        }
    }
    ll ans=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                chmax(ans,dp[i][j][k]);
                dp[i][j][k]=-INF;
            }
        }
    }
    dp[0][0][0]=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                rep(l,m){
                    if(i&(1<<l)) continue;
                    if(0<=k-c[l]) chmax(dp[i][j|(1<<l)][k-c[l]],dp[i][j][k]-d[l]);
                    chmax(dp[i][j|(1<<l)][k],dp[i][j][k]);
                }
                rep(l,n){
                    if(i&(1<<l)) continue;
                    if(k+a[l]<=w) chmax(dp[i|(1<<l)][j][k+a[l]],dp[i][j][k]+b[l]);
                    chmax(dp[i|(1<<l)][j][k],dp[i][j][k]);
                }
            }
        }
    }
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                chmax(ans,dp[i][j][k]);
                dp[i][j][k]=-INF;
            }
        }
    }
    cout << ans << '\n';
}
0