結果

問題 No.4 おもりと天秤
ユーザー cm-kojimatcm-kojimat
提出日時 2020-11-12 14:46:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 96 ms / 5,000 ms
コード長 693 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 10,808 KB
実行使用メモリ 13,564 KB
最終ジャッジ日時 2023-09-30 01:14:13
合計ジャッジ時間 2,154 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
9,680 KB
testcase_01 AC 33 ms
9,716 KB
testcase_02 AC 33 ms
9,876 KB
testcase_03 AC 32 ms
9,684 KB
testcase_04 AC 33 ms
9,704 KB
testcase_05 AC 68 ms
11,760 KB
testcase_06 AC 33 ms
9,832 KB
testcase_07 AC 64 ms
11,764 KB
testcase_08 AC 32 ms
9,812 KB
testcase_09 AC 96 ms
13,564 KB
testcase_10 AC 68 ms
12,096 KB
testcase_11 AC 33 ms
9,688 KB
testcase_12 AC 31 ms
9,680 KB
testcase_13 AC 32 ms
9,672 KB
testcase_14 AC 32 ms
9,656 KB
testcase_15 AC 32 ms
9,708 KB
testcase_16 AC 33 ms
9,828 KB
testcase_17 AC 32 ms
9,712 KB
testcase_18 AC 63 ms
11,660 KB
testcase_19 AC 60 ms
11,520 KB
testcase_20 AC 60 ms
11,620 KB
testcase_21 AC 58 ms
11,460 KB
testcase_22 AC 59 ms
11,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List


def f(n: int, ws: List[int]) -> bool:
    max_w = sum(ws)
    if max_w % 2 != 0:
        return False

    half_w = int(max_w / 2)
    dp = [[False] * (half_w + 1) for _ in range(0, n + 1)]
    dp[0][0] = True

    for i in range(0, n):
        for w in range(0, half_w + 1):
            dp[i + 1][w] = (
                (dp[i][w - ws[i]] or dp[i][w]) if w >= ws[i] else dp[i][w]
            )

    return dp[n][half_w]


# assert f(3, [1, 2, 3])
# assert not f(5, [1, 2, 3, 4, 5])
# assert not f(15, [62, 8, 90, 2, 24, 62, 38, 64, 76, 60, 30, 76, 80, 74, 72])

n = int(input())
ws = list(map(int, input().split(" ")))
print("possible" if f(n, ws) else "impossible")
0