結果

問題 No.4 おもりと天秤
ユーザー nanaenanae
提出日時 2017-02-09 20:42:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 129 ms / 5,000 ms
コード長 923 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 12,696 KB
最終ジャッジ日時 2023-09-08 17:18:55
合計ジャッジ時間 2,146 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,632 KB
testcase_01 AC 19 ms
8,724 KB
testcase_02 AC 21 ms
8,720 KB
testcase_03 AC 20 ms
8,668 KB
testcase_04 AC 20 ms
8,824 KB
testcase_05 AC 60 ms
10,936 KB
testcase_06 AC 20 ms
8,820 KB
testcase_07 AC 58 ms
10,740 KB
testcase_08 AC 20 ms
8,624 KB
testcase_09 AC 129 ms
12,696 KB
testcase_10 AC 63 ms
11,124 KB
testcase_11 AC 20 ms
8,588 KB
testcase_12 AC 20 ms
8,768 KB
testcase_13 AC 19 ms
8,788 KB
testcase_14 AC 19 ms
8,588 KB
testcase_15 AC 19 ms
8,740 KB
testcase_16 AC 20 ms
8,676 KB
testcase_17 AC 20 ms
8,644 KB
testcase_18 AC 66 ms
10,800 KB
testcase_19 AC 55 ms
10,684 KB
testcase_20 AC 54 ms
10,576 KB
testcase_21 AC 53 ms
10,452 KB
testcase_22 AC 53 ms
10,484 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():
    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