結果

問題 No.2364 Knapsack Problem
ユーザー tnakao0123tnakao0123
提出日時 2023-07-05 16:17:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 3,000 ms
コード長 1,532 bytes
コンパイル時間 408 ms
コンパイル使用メモリ 43,632 KB
実行使用メモリ 35,104 KB
最終ジャッジ日時 2023-09-26 16:24:32
合計ジャッジ時間 2,277 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 10 ms
19,340 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 6 ms
7,100 KB
testcase_10 AC 6 ms
11,132 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 42 ms
35,084 KB
testcase_13 AC 38 ms
35,008 KB
testcase_14 AC 42 ms
35,028 KB
testcase_15 AC 48 ms
35,032 KB
testcase_16 AC 42 ms
35,088 KB
testcase_17 AC 43 ms
35,060 KB
testcase_18 AC 38 ms
34,952 KB
testcase_19 AC 48 ms
35,096 KB
testcase_20 AC 47 ms
35,036 KB
testcase_21 AC 40 ms
35,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2364.cc:  No.2364 Knapsack Problem - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 7;
const int MAX_M = 7;
const int MAX_K = MAX_N + MAX_M;
const int KBITS = 1 << MAX_K;
const int MAX_W = 500;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N], cs[MAX_M], ds[MAX_M];
int es[MAX_K], fs[MAX_K], dp[KBITS][MAX_W + 1];

/* subroutines */

void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, m, w;
  scanf("%d%d%d", &n, &m, &w);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  for (int i = 0; i < n; i++) scanf("%d", bs + i);
  for (int i = 0; i < m; i++) scanf("%d", cs + i);
  for (int i = 0; i < m; i++) scanf("%d", ds + i);

  int k = n + m;
  copy(as, as + n, es);
  for (int i = 0; i < m; i++) es[i + n] = -cs[i];
  copy(bs, bs + n, fs);
  for (int i = 0; i < m; i++) fs[i + n] = -ds[i];

  int kbits = 1 << k;
  for (int bits = 0; bits < kbits; bits++)
    fill(dp[bits], dp[bits] + w + 1, -1);
  dp[0][0] = 0;

  for (int bits = 0; bits < kbits; bits++)
    for (int i = 0, bi = 1; i < k; i++, bi <<= 1)
      if (! (bits & bi)) {
	int minj = max(0, -es[i]), maxj = min(w, w - es[i]);
	for (int j = minj; j <= maxj; j++)
	  if (dp[bits][j] >= 0)
	    setmax(dp[bits | bi][j + es[i]], dp[bits][j] + fs[i]);
      }

  int maxd = 0;
  for (int bits = 0; bits < kbits; bits++)
    for (int j = 0; j <= w; j++)
      maxd = max(maxd, dp[bits][j]);

  printf("%d\n", maxd);

  return 0;
}
0