結果

問題 No.2364 Knapsack Problem
ユーザー logxlogx
提出日時 2021-06-27 17:41:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 3,000 ms
コード長 2,797 bytes
コンパイル時間 2,388 ms
コンパイル使用メモリ 203,200 KB
実行使用メモリ 36,048 KB
最終ジャッジ日時 2023-09-21 14:35:13
合計ジャッジ時間 5,633 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 29 ms
6,008 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 22 ms
7,232 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 238 ms
35,804 KB
testcase_13 AC 231 ms
35,704 KB
testcase_14 AC 238 ms
35,996 KB
testcase_15 AC 250 ms
35,740 KB
testcase_16 AC 243 ms
35,804 KB
testcase_17 AC 241 ms
36,048 KB
testcase_18 AC 236 ms
35,820 KB
testcase_19 AC 250 ms
35,808 KB
testcase_20 AC 247 ms
35,824 KB
testcase_21 AC 236 ms
35,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define unless(x) if(!(x))
#define until(x) while(!(x))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}



int main(){
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/
    
    int N,M,W;
    cin >> N >> M >> W;
    vector<int> weight(N+M);
    vector<int> value(N+M);
    for(int i=0;i<N;i++)cin >> weight[i];
    for(int i=0;i<N;i++)cin >> value[i];
    for(int i=N;i<N+M;i++)cin >> weight[i],weight[i]*=-1;
    for(int i=N;i<N+M;i++)cin >> value[i],value[i]*=-1;
    N+=M;
    vector dp(1<<N,vector<int>(W+1,-1000000000));
    dp[0][0]=0;
    int ans=0;
    for(int S=0;S<(1<<N);S++)for(int now=0;now<=W;now++){
        for(int i=0;i<N;i++)if(!(S>>i & 1)){
            if(0<=now+weight[i] && now+weight[i]<=W){
                chmax(dp[S^(1<<i)][now+weight[i]], dp[S][now]+value[i]);
            }
        }
        chmax(ans,dp[S][now]);
    }
    cout << ans << newl;
}
0