結果

問題 No.4 おもりと天秤
ユーザー mitsushinomitsushino
提出日時 2017-12-22 16:52:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 225 ms / 5,000 ms
コード長 1,107 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 16,520 KB
最終ジャッジ日時 2023-09-08 17:37:38
合計ジャッジ時間 2,478 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
7,796 KB
testcase_01 AC 16 ms
7,880 KB
testcase_02 AC 18 ms
8,268 KB
testcase_03 AC 16 ms
8,272 KB
testcase_04 AC 18 ms
8,320 KB
testcase_05 AC 119 ms
12,824 KB
testcase_06 AC 16 ms
7,932 KB
testcase_07 AC 119 ms
12,940 KB
testcase_08 AC 15 ms
7,856 KB
testcase_09 AC 225 ms
16,520 KB
testcase_10 AC 130 ms
13,304 KB
testcase_11 AC 16 ms
7,832 KB
testcase_12 AC 15 ms
7,832 KB
testcase_13 AC 15 ms
7,924 KB
testcase_14 AC 16 ms
7,884 KB
testcase_15 AC 16 ms
7,800 KB
testcase_16 AC 16 ms
7,844 KB
testcase_17 AC 16 ms
7,924 KB
testcase_18 AC 115 ms
12,364 KB
testcase_19 AC 107 ms
12,264 KB
testcase_20 AC 108 ms
12,340 KB
testcase_21 AC 101 ms
11,932 KB
testcase_22 AC 103 ms
12,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-


def make_dp_table(N, sum_weight_list):
    dp = [[False for _ in range(sum_weight_list + 1)] for _ in range(N)]
    for i in range(N):
        if i == 0:
            for j in range(sum_weight_list + 1):
                if j == weight_list[i]:
                    dp[i][j] = True
                    break
        else:
            for j in range(1, sum_weight_list + 1):
                if j == weight_list[i]:
                    dp[i][j] = True
                elif j < weight_list[i]:
                    dp[i][j] = dp[i - 1][j]
                else:
                    dp[i][j] = dp[i - 1][j] or dp[i - 1][j - weight_list[i]]
    return dp


if __name__ == '__main__':
    N = int(input())
    weight_list = list(map(int, input().split()))
    weight_list.sort()
    sum_weight_list = sum(weight_list)
    if sum_weight_list % 2 != 0:
        print('impossible')
    else:
        dp = make_dp_table(N, sum_weight_list)
        target_val = int(sum_weight_list / 2)
        if dp[N - 1][target_val]:
            print('possible')
        else:
            print('impossible')
0