結果
| 問題 |
No.2364 Knapsack Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-27 10:16:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 17 ms / 3,000 ms |
| コード長 | 3,183 bytes |
| コンパイル時間 | 3,473 ms |
| コンパイル使用メモリ | 252,188 KB |
| 最終ジャッジ日時 | 2025-02-15 19:28:15 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
#pragma region Macros
#include <bits/stdc++.h>
#include <atcoder/all>
#if defined(LOCAL) || defined(_DEBUG)
#include "debug.hpp"
#else
#define O(...)
#define START()
#define STOP()
#define MEMORY()
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T, class U>bool chmax(T &a, const U &b) { if (a<(T)b) { a=(T)b; return 1; } return 0; }
template<class T, class U>bool chmin(T &a, const U &b) { if (b<(T)a) { a=(T)b; return 1; } return 0; }
#define vec vector
#define umap unordered_map
#define uset unordered_set
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Tup = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
template<class T> istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T, class U> istream &operator>>(istream &stream, pair<T, U>& o){cin >> o.fi >> o.se; return stream;}
namespace myinput {
void input() {}
template<class T, class... Ts> void input(T&& o, Ts&&... args){cin >> o;input(forward<Ts>(args)...);}
void assign_vl(size_t siz) {};
template<class T, class... Ts> void assign_vl(size_t siz, T&& o, Ts&&... args){o.resize(siz);assign_vl(siz, forward<Ts>(args)...);}
void input_horizon_sub(size_t index) {};
template<class T, class... Ts> void input_horizon_sub(size_t index, T&& o, Ts&&... args) {cin>>o[index];input_horizon_sub(index, forward<Ts>(args)...);}
template<class... Ts> void input_horizon(size_t siz, Ts&&... args){REP(i, siz) input_horizon_sub(i, forward<Ts>(args)...);}
}
#define _I(T, ...) ;myinput::input(__VA_ARGS__);
#define I(T, ...) ;T __VA_ARGS__;_I(T, __VA_ARGS__);
#define _Iv(T, siz, ...) ;myinput::assign_vl(siz, __VA_ARGS__);myinput::input(__VA_ARGS__);
#define Iv(T, siz, ...) ;vec<T> __VA_ARGS__;_Iv(T, siz, __VA_ARGS__);
#define _Ih(T, siz, ...) ;myinput::assign_vl(siz, __VA_ARGS__);myinput::input_horizon(siz, __VA_ARGS__);
#define Ih(T, siz, ...) ;vec<T> __VA_ARGS__;_Ih(T, siz, __VA_ARGS__);
#pragma endregion
void Main();
int main(){
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
MEMORY();
return 0;
}
ll n, m, w;
vl a, b, c, d;
ll dp[500+1][128][128];
ll rec(ll noww, ll usedn, ll usedm) {
if(noww < 0 || noww > w) return -INF;
ll &res = dp[noww][usedn][usedm];
if(~res) return res;
res = 0;
REP(i, n) {
ll msk = 1 << i;
if((usedn & msk) == 0) {
chmax(res, rec(noww+a[i], usedn | msk, usedm)+b[i]);
}
}
REP(i, m) {
ll msk = 1 << i;
if((usedm & msk) == 0) {
chmax(res, rec(noww-c[i], usedn, usedm | msk)-d[i]);
}
}
return res;
}
void Main(){
memset(dp, -1, sizeof(dp));
_I(ll, n, m, w);
_Iv(ll, n, a, b);
_Iv(ll, m, c, d);
cout << rec(0, 0, 0) << el;
}