結果

問題 No.4 おもりと天秤
ユーザー nanae
提出日時 2017-02-09 20:27:03
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 846 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 27,004 KB
最終ジャッジ日時 2024-06-26 10:09:19
合計ジャッジ時間 1,980 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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