結果

問題 No.4 おもりと天秤
ユーザー cm-arai
提出日時 2020-12-24 15:11:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 955 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-09-21 16:55:54
合計ジャッジ時間 1,678 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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