結果

問題 No.15 カタログショッピング
ユーザー szkhts
提出日時 2022-09-23 08:09:31
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 651 ms / 5,000 ms
コード長 828 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 39,628 KB
最終ジャッジ日時 2024-12-22 05:12:47
合計ジャッジ時間 4,136 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

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

first = prices[:n // 2]
second = prices[n // 2:]

F = {}
S = {}
len_f = len(first)
for l in range(1 << len_f):
    t = []
    sum = 0
    for i in range(len_f):
        if l >> i & 1:
            t.append(i + 1)
            sum += first[i]

    if sum not in F:
        F[sum] = []
    F[sum].append(t[:])

len_s = len(second)
for l in range(1 << len_s):
    t = []
    sum = 0
    for i in range(len_s):
        if l >> i & 1:
            t.append(i + len_f + 1)
            sum += second[i]

    if sum not in S:
        S[sum] = []
    S[sum].append(t[:])

ans = []
for p in F:
    if s - p in S:
        for p1 in F[p]:
            for p2 in S[s - p]:
                ans.append(p1 + p2)

ans.sort()
for a in ans:
    print(" ".join(map(str, a)))

0