結果
問題 | No.2364 Knapsack Problem |
ユーザー |
|
提出日時 | 2023-07-01 16:23:04 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 3,000 ms |
コード長 | 1,309 bytes |
コンパイル時間 | 2,523 ms |
コンパイル使用メモリ | 251,264 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-08 02:30:34 |
合計ジャッジ時間 | 3,247 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h>using namespace std;template <typename T> bool is_kth_bit_set(T n, int k) { return n >> k & 1; }#define rep(i, n) for (int i = 0; i < n; i++)template <typename T>std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {int n = a.size();rep(i, n) { os << a[i] << " \n"[i == n - 1]; }return os;}template <typename T>std::istream &operator>>(std::istream &is, std::vector<T> &a) {int n = a.size();rep(i, n) { is >> a[i]; }return is;}template <typename T> void chmax(T &a, T b) {if (b > a)a = b;}template <typename T> void chmin(T &a, T b) {if (b < a)a = b;}auto main() -> int {int n, m, w;cin >> n >> m >> w;vector<int> a(n), b(n), c(m), d(m);cin >> a >> b >> c >> d;int l = n + m;int l2 = 1 << l;vector<long> v(l2), wt(l2);rep(i, l) rep(s, 1 << i) {long dw = i < n ? a[i] : -c[i - n];long dv = i < n ? b[i] : -d[i - n];int u = s | 1 << i;v[u] = v[s] + dv;wt[u] = wt[s] + dw;}vector<bool> ok(l2);ok[0] = true;long mx = 0;rep(s, l2) {if (!ok[s])continue;chmax(mx, v[s]);rep(i, l) {if (s >> i & 1)continue;int u = s | 1 << i;if (0 <= wt[u] && wt[u] <= w)ok[u] = true;}}cout << mx << endl;}