結果

問題 No.2364 Knapsack Problem
ユーザー momoyuumomoyuu
提出日時 2023-10-25 15:25:54
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,304 bytes
コンパイル時間 3,213 ms
コンパイル使用メモリ 255,188 KB
実行使用メモリ 584,552 KB
最終ジャッジ日時 2023-10-25 15:26:20
合計ジャッジ時間 25,580 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 3 ms
4,372 KB
testcase_02 AC 13 ms
9,824 KB
testcase_03 AC 3 ms
4,372 KB
testcase_04 AC 20 ms
12,200 KB
testcase_05 AC 51 ms
25,664 KB
testcase_06 AC 21 ms
12,200 KB
testcase_07 AC 861 ms
266,696 KB
testcase_08 AC 5 ms
4,840 KB
testcase_09 AC 203 ms
76,616 KB
testcase_10 AC 387 ms
134,960 KB
testcase_11 AC 45 ms
20,872 KB
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false); 
   
    int n,m,w;
    cin>>n>>m>>w;
    vector<ll> a(n),b(n),c(m),d(m);
    for(int i = 0;i<n;i++) cin>>a[i];
    for(int i = 0;i<n;i++) cin>>b[i];
    for(int i = 0;i<m;i++) cin>>c[i];
    for(int i = 0;i<m;i++) cin>>d[i];

    vector<vector<vector<ll>>> dp(1<<n,vector<vector<ll>>(1<<m,vector<ll>(w+1+4000,-1e18)));
    dp[0][0][4000] = 0;

    ll ans = -1e18;
    for(int i = 0;i<1<<n;i++){
        for(int j = 0;j<1<<m;j++){
            for(int k = 0;k<=w+4000;k++){
                ans = max(ans,dp[i][j][k]);
                for(int l = 0;l<n;l++){
                    if(i>>l&1) continue;
                    int ni = i | (1<<l);
                    int nxt = k + a[l];
                    if(nxt>w+4000) continue;
                    dp[ni][j][nxt] = max(dp[ni][j][nxt],dp[i][j][k]+b[l]);
                }
                for(int l = 0;l<m;l++){
                    if(j>>l&1) continue;
                    int ni = j | (1<<l);
                    int nxt = k - c[l];
                    if(nxt<4000) continue;
                    dp[i][ni][nxt] = max(dp[i][ni][nxt],dp[i][j][k]-d[l]);
                }
            }
        }
    }
    cout<<ans<<endl;
}
0