結果

問題 No.15 カタログショッピング
ユーザー szkhtsszkhts
提出日時 2022-09-23 08:09:31
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 562 ms / 5,000 ms
コード長 828 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,068 KB
実行使用メモリ 36,996 KB
最終ジャッジ日時 2023-08-23 20:47:26
合計ジャッジ時間 3,903 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,840 KB
testcase_01 AC 16 ms
7,908 KB
testcase_02 AC 16 ms
7,852 KB
testcase_03 AC 17 ms
8,228 KB
testcase_04 AC 15 ms
7,800 KB
testcase_05 AC 561 ms
36,936 KB
testcase_06 AC 551 ms
36,940 KB
testcase_07 AC 551 ms
36,996 KB
testcase_08 AC 562 ms
36,960 KB
testcase_09 AC 557 ms
36,932 KB
権限があれば一括ダウンロードができます

ソースコード

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