結果

問題 No.4 おもりと天秤
ユーザー nanae
提出日時 2017-02-09 20:42:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 153 ms / 5,000 ms
コード長 923 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-06-26 10:09:21
合計ジャッジ時間 1,973 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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():
    N = int(input())
    Ws = [int(i) for i in input().split()]

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

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

    dp = [[False] * (half + 1) for i in range(N + 1)]

    for i in range(N + 1):
        for j in range(half + 1):
            if j == 0:
                dp[i][j] = True
            elif i == 0:
                dp[i][j] = False
            elif dp[i - 1][j] == True:
                dp[i][j] = True
            elif j - Ws[i - 1] >= 0 and dp[i - 1][j - Ws[i - 1]] == True:
                dp[i][j] = True

    if dp[N][half]:
        print('possible')
    else:
        print('impossible')

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