結果

問題 No.2364 Knapsack Problem
ユーザー FplusFplusFFplusFplusF
提出日時 2023-06-30 21:55:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,010 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 207,488 KB
実行使用メモリ 68,500 KB
最終ジャッジ日時 2023-09-21 15:50:57
合計ジャッジ時間 10,161 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 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,436 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 53 ms
8,616 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 62 ms
10,500 KB
testcase_10 AC 18 ms
4,780 KB
testcase_11 AC 11 ms
4,852 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 660 ms
68,292 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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]);
                }
            }
        }
    }
    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]);
                }
            }
        }
    }
    ll ans=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                chmax(ans,dp[i][j][k]);
            }
        }
    }
    cout << ans << '\n';
}
0