結果

問題 No.4 おもりと天秤
ユーザー nanaenanae
提出日時 2017-03-30 23:28:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 663 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 12,028 KB
最終ジャッジ日時 2023-09-08 17:23:43
合計ジャッジ時間 2,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,844 KB
testcase_01 AC 16 ms
7,808 KB
testcase_02 AC 17 ms
7,808 KB
testcase_03 AC 16 ms
7,876 KB
testcase_04 AC 17 ms
8,264 KB
testcase_05 AC 123 ms
10,344 KB
testcase_06 AC 16 ms
7,804 KB
testcase_07 AC 124 ms
10,408 KB
testcase_08 AC 16 ms
7,788 KB
testcase_09 AC 23 ms
12,028 KB
testcase_10 AC 136 ms
10,612 KB
testcase_11 AC 15 ms
7,940 KB
testcase_12 AC 16 ms
7,844 KB
testcase_13 AC 16 ms
7,876 KB
testcase_14 AC 15 ms
7,776 KB
testcase_15 AC 15 ms
7,928 KB
testcase_16 AC 15 ms
7,936 KB
testcase_17 AC 16 ms
7,884 KB
testcase_18 AC 69 ms
10,176 KB
testcase_19 AC 115 ms
9,992 KB
testcase_20 AC 115 ms
10,112 KB
testcase_21 AC 106 ms
9,864 KB
testcase_22 AC 107 ms
9,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    def rec(m, weight, dp):
        if dp[m][weight] is None:
            dp[m][weight] = rec(m - 1, weight, dp)
            if weight - W[m - 1] >= 0:
                dp[m][weight] |= rec(m - 1, weight - W[m - 1], dp)

        return dp[m][weight]

    N = int(input())
    W = [int(i) for i in input().split()]
    Wt = sum(W)

    if Wt & 1:
        print('impossible')
        return

    Wh = Wt // 2

    dp = [[None]*(Wh + 1) for i in range(N + 1)]

    dp[0][0] = True
    for i in range(1, Wh + 1):
        dp[0][i] = False
    judge = rec(N, Wh, dp)

    print('possible' if judge else 'impossible')

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