結果

問題 No.4 おもりと天秤
ユーザー cm-araicm-arai
提出日時 2020-12-24 15:11:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 86 ms / 5,000 ms
コード長 955 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 11,832 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2023-10-21 15:41:41
合計ジャッジ時間 2,009 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,100 KB
testcase_01 AC 30 ms
10,100 KB
testcase_02 AC 31 ms
10,100 KB
testcase_03 AC 29 ms
10,100 KB
testcase_04 AC 30 ms
10,100 KB
testcase_05 AC 65 ms
10,128 KB
testcase_06 AC 30 ms
10,100 KB
testcase_07 AC 64 ms
10,128 KB
testcase_08 AC 30 ms
10,100 KB
testcase_09 AC 54 ms
10,144 KB
testcase_10 AC 72 ms
10,128 KB
testcase_11 AC 31 ms
10,100 KB
testcase_12 AC 30 ms
10,100 KB
testcase_13 AC 32 ms
10,100 KB
testcase_14 AC 31 ms
10,100 KB
testcase_15 AC 30 ms
10,100 KB
testcase_16 AC 30 ms
10,100 KB
testcase_17 AC 30 ms
10,100 KB
testcase_18 AC 86 ms
10,124 KB
testcase_19 AC 59 ms
10,124 KB
testcase_20 AC 60 ms
10,124 KB
testcase_21 AC 58 ms
10,124 KB
testcase_22 AC 59 ms
10,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
W = list(map(int, input().split()))


# 最終的には片方の天秤に全体の半分の重さが乗る
sum_weight = sum(W) / 2
# 割り切れなければ(少数を含む)天秤は釣り合わない
if not sum_weight.is_integer():
    print("impossible")
    exit(0)
else:
    sum_weight = int(sum_weight)


# 方針: 合計値だけメモっといて、そこにsum_weightが入っていれば処理終了させる
# それ以外のやつは、一度処理したらスキップしたい
dp = [False] * (sum_weight + 1)
dp[0] = True
for i in range(len(W)):
    for j in range(len(dp) - 1, -1, -1):
        if not dp[j]:
            continue
        if W[i]+j <= sum_weight:
            # print(
            #     f"i={i}, j={j}, W[i]={W[i]}, dp[j]={dp[j]}, sum_weight={sum_weight}")
            dp[W[i] + j] = True
            if W[i] + j == sum_weight:
                print("possible")
                exit(0)
print("impossible")
0