結果

問題 No.4 おもりと天秤
ユーザー nanaenanae
提出日時 2017-02-09 20:27:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 110 ms / 5,000 ms
コード長 846 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 10,780 KB
実行使用メモリ 24,928 KB
最終ジャッジ日時 2023-09-08 17:18:52
合計ジャッジ時間 2,068 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,660 KB
testcase_01 AC 18 ms
8,684 KB
testcase_02 AC 20 ms
8,880 KB
testcase_03 AC 19 ms
8,612 KB
testcase_04 AC 19 ms
8,732 KB
testcase_05 AC 64 ms
17,044 KB
testcase_06 AC 19 ms
8,536 KB
testcase_07 AC 58 ms
16,268 KB
testcase_08 AC 19 ms
8,708 KB
testcase_09 AC 21 ms
8,952 KB
testcase_10 AC 69 ms
17,876 KB
testcase_11 AC 19 ms
8,800 KB
testcase_12 AC 19 ms
8,604 KB
testcase_13 AC 19 ms
8,600 KB
testcase_14 AC 19 ms
8,796 KB
testcase_15 AC 19 ms
8,728 KB
testcase_16 AC 19 ms
8,724 KB
testcase_17 AC 19 ms
8,536 KB
testcase_18 AC 110 ms
24,928 KB
testcase_19 AC 56 ms
16,456 KB
testcase_20 AC 56 ms
16,328 KB
testcase_21 AC 57 ms
16,352 KB
testcase_22 AC 54 ms
16,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    @lru_cache(maxsize=None)
    def rec(m, weight):
        if weight == 0:
            return True
        elif m == 0:
            if Ws[0] == weight:
                return True
            else:
                return False
        else:
            return rec(m - 1, weight) or rec(m - 1, weight - Ws[m])
    
    N = int(input())
    Ws = [int(i) for i in input().split()]

    half, rem = divmod(sum(Ws), 2)

    if rem == 1:
        print('impossible')
        return

    if rec(N - 1, half):
        print('possible')
    else:
        print('impossible')
    pass

if __name__ == '__main__':
    solve()
0