結果

問題 No.15 カタログショッピング
ユーザー qibqib
提出日時 2023-03-18 14:33:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 998 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 81,808 KB
実行使用メモリ 91,264 KB
最終ジャッジ日時 2023-10-18 17:17:37
合計ジャッジ時間 1,514 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,552 KB
testcase_01 AC 35 ms
53,552 KB
testcase_02 AC 36 ms
53,552 KB
testcase_03 AC 43 ms
60,372 KB
testcase_04 AC 35 ms
53,552 KB
testcase_05 AC 107 ms
91,264 KB
testcase_06 AC 110 ms
91,264 KB
testcase_07 AC 110 ms
91,264 KB
testcase_08 AC 111 ms
90,556 KB
testcase_09 AC 107 ms
90,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, s = map(int, input().split())
p = [int(input()) for _ in range(n)]

left = {}
for mask in range(1 << (n // 2)):
  cost = 0
  for i in range(n // 2):
    if (mask >> i) & 1 != 0:
      cost += p[i]

  if cost > s:
    continue

  if left.get(cost, None) is None:
    left[cost] = []

  left[cost].append(mask)

right = {}
for mask in range(1 << (n - n // 2)):
  cost = 0
  for i in range(n // 2, n):
    if (mask >> (i - n // 2)) & 1 != 0:
      cost += p[i]

  if cost > s:
    continue

  if right.get(cost, None) is None:
    right[cost] = []

  right[cost].append(mask)

ans = []
for k, ls in left.items():
  if right.get(s - k, None) is None:
    continue

  for lm in ls:
    l = []
    for i in range(n // 2):
      if (lm >> i) & 1 != 0:
        l.append(i + 1)

    for rm in right[s - k]:
      r = []
      for i in range(n - n // 2):
        if (rm >> i) & 1 != 0:
          r.append(i + 1 + n // 2)

      ans.append(l + r)

ans.sort()

for xs in ans:
  print(" ".join(map(str, xs)))
0