結果

問題 No.2866 yuusaan's Knapsack
ユーザー H3PO4H3PO4
提出日時 2024-09-07 14:25:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 98,868 KB
最終ジャッジ日時 2024-09-07 14:25:46
合計ジャッジ時間 4,266 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
63,620 KB
testcase_01 AC 49 ms
62,068 KB
testcase_02 AC 56 ms
66,676 KB
testcase_03 AC 47 ms
61,220 KB
testcase_04 AC 44 ms
60,380 KB
testcase_05 AC 44 ms
60,384 KB
testcase_06 AC 113 ms
96,868 KB
testcase_07 AC 100 ms
91,704 KB
testcase_08 AC 109 ms
95,776 KB
testcase_09 AC 105 ms
93,768 KB
testcase_10 AC 107 ms
94,804 KB
testcase_11 AC 91 ms
85,912 KB
testcase_12 AC 102 ms
91,156 KB
testcase_13 AC 95 ms
88,676 KB
testcase_14 AC 101 ms
93,176 KB
testcase_15 AC 109 ms
95,240 KB
testcase_16 AC 99 ms
89,104 KB
testcase_17 AC 111 ms
95,972 KB
testcase_18 AC 96 ms
86,868 KB
testcase_19 AC 126 ms
98,868 KB
testcase_20 AC 107 ms
91,972 KB
testcase_21 AC 110 ms
94,060 KB
testcase_22 AC 107 ms
93,188 KB
testcase_23 AC 120 ms
97,840 KB
testcase_24 AC 100 ms
91,136 KB
testcase_25 AC 109 ms
94,968 KB
testcase_26 AC 99 ms
96,320 KB
testcase_27 AC 55 ms
66,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

# get max value
MAX = 10001
MOD = 998244353

dp_val = [-(10**9)] * (2 * MAX + 1)
dp_val[MAX] = 0
dp_ct = [0] * (2 * MAX + 1)
dp_ct[MAX] = 1

goods = list(tuple(map(int, input().split())) for _ in range(N))
goods.sort(key=lambda x: x[1])

for v, w in goods:
    new_dp_val = dp_val.copy()
    new_dp_ct = [0] * (2 * MAX + 1)
    for i in range(2 * MAX + 1):
        if i - w >= 0 and i - w <= 2 * MAX:
            if dp_val[i] <= dp_val[i - w] + v:
                new_dp_ct[i] += dp_ct[i - w]
                new_dp_val[i] = dp_val[i - w] + v
            if dp_val[i] >= dp_val[i - w] + v:
                new_dp_ct[i] += dp_ct[i]
            new_dp_ct[i] %= MOD
    dp_val = new_dp_val
    dp_ct = new_dp_ct

max_value = -(10**9)
ct = 0
for i in range(MAX + W + 1):
    if max_value < dp_val[i]:
        max_value = dp_val[i]
        ct = dp_ct[i]
    elif max_value == dp_val[i]:
        ct += dp_ct[i]

ct %= MOD
print(max_value, ct)
0