結果
問題 | No.2364 Knapsack Problem |
ユーザー |
|
提出日時 | 2023-07-01 16:38:17 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 5 ms / 3,000 ms |
コード長 | 1,245 bytes |
コンパイル時間 | 3,464 ms |
コンパイル使用メモリ | 250,656 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-08 02:44:46 |
合計ジャッジ時間 | 3,844 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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::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, typename U = T> vector<U> sums(vector<T> &a) {int n = a.size();int n2 = 1 << n;vector<T> b(n2);rep(s, n2) rep(i, n) {if (s >> i & 1)b[s] += a[i];}return 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> dw(l), dv(l);rep(i, l) {dw[i] = i < n ? a[i] : -c[i - n];dv[i] = i < n ? b[i] : -d[i - n];}vector<long> v = sums(dv);vector<long> wt = sums(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;}