結果

問題 No.2364 Knapsack Problem
ユーザー rulerruler
提出日時 2023-07-01 16:23:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 3,000 ms
コード長 1,327 bytes
コンパイル時間 3,678 ms
コンパイル使用メモリ 249,156 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-22 10:36:39
合計ジャッジ時間 4,272 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,388 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 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::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) {
    long dw = i < n ? a[i] : -c[i - n];
    long dv = i < n ? b[i] : -d[i - n];
    rep(s, 1 << i) {
      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;
}
0