結果

問題 No.4 おもりと天秤
ユーザー strn5strn5
提出日時 2019-07-25 02:02:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 265 ms / 5,000 ms
コード長 638 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 10,744 KB
実行使用メモリ 12,280 KB
最終ジャッジ日時 2023-09-11 02:27:36
合計ジャッジ時間 3,023 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,896 KB
testcase_01 AC 16 ms
8,148 KB
testcase_02 AC 18 ms
8,168 KB
testcase_03 AC 17 ms
7,892 KB
testcase_04 AC 19 ms
8,304 KB
testcase_05 AC 139 ms
10,384 KB
testcase_06 AC 16 ms
7,944 KB
testcase_07 AC 138 ms
10,460 KB
testcase_08 AC 16 ms
8,244 KB
testcase_09 AC 265 ms
12,280 KB
testcase_10 AC 153 ms
10,688 KB
testcase_11 AC 17 ms
7,836 KB
testcase_12 AC 16 ms
7,784 KB
testcase_13 AC 16 ms
7,820 KB
testcase_14 AC 17 ms
7,804 KB
testcase_15 AC 17 ms
7,832 KB
testcase_16 AC 17 ms
7,944 KB
testcase_17 AC 17 ms
7,924 KB
testcase_18 AC 136 ms
10,408 KB
testcase_19 AC 121 ms
10,272 KB
testcase_20 AC 123 ms
10,164 KB
testcase_21 AC 116 ms
9,976 KB
testcase_22 AC 122 ms
10,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
N = int(input())
W = list(map(int, input().split()))
sum = 0
for i in range(len(W)):
    sum += W[i]

#合計が偶数でない時に終了
if sum % 2 != 0:
    print("impossible")
    sys.exit()
    
hsum = int(sum / 2)

DP = [[0 for i in range(hsum + 1)] for j in range(N + 1)]
DP[0][0] = 1
for i in range(1, N + 1):
    for j in range(0, hsum + 1):
        if j < W[i-1]:
            DP[i][j] = DP[i-1][j]
        else:
            if DP[i-1][j-W[i-1]] == 1:
                DP[i][j] = 1
            elif DP[i-1][j] == 1:
                DP[i][j] = 1

if DP[N][hsum] == 1:
    print("possible")
else:
    print("impossible")
0