結果
| 問題 |
No.2364 Knapsack Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-01 16:40:29 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 3,000 ms |
| コード長 | 1,221 bytes |
| コンパイル時間 | 3,070 ms |
| コンパイル使用メモリ | 252,232 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-08 02:47:13 |
| 合計ジャッジ時間 | 3,944 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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();
vector<U> b(1 << n);
rep(i, n) rep(s, 1 << i) { b[s | 1 << i] = 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;
}