結果

問題 No.2364 Knapsack Problem
ユーザー rulerruler
提出日時 2023-07-01 16:40:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 3,000 ms
コード長 1,221 bytes
コンパイル時間 2,736 ms
コンパイル使用メモリ 248,200 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 10:54:38
合計ジャッジ時間 3,705 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0