結果

問題 No.2866 yuusaan's Knapsack
ユーザー detteiuudetteiuu
提出日時 2024-08-30 22:23:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 995 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,024 KB
実行使用メモリ 104,644 KB
最終ジャッジ日時 2024-08-31 01:28:29
合計ジャッジ時間 4,148 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
62,388 KB
testcase_01 AC 44 ms
62,188 KB
testcase_02 AC 47 ms
62,716 KB
testcase_03 AC 41 ms
61,588 KB
testcase_04 AC 42 ms
61,500 KB
testcase_05 AC 41 ms
60,476 KB
testcase_06 AC 168 ms
102,568 KB
testcase_07 AC 127 ms
96,376 KB
testcase_08 AC 149 ms
102,492 KB
testcase_09 AC 137 ms
97,524 KB
testcase_10 AC 147 ms
100,748 KB
testcase_11 AC 113 ms
90,200 KB
testcase_12 AC 130 ms
95,984 KB
testcase_13 AC 125 ms
95,176 KB
testcase_14 AC 129 ms
96,580 KB
testcase_15 AC 149 ms
101,200 KB
testcase_16 AC 124 ms
93,840 KB
testcase_17 AC 144 ms
98,840 KB
testcase_18 AC 112 ms
89,168 KB
testcase_19 AC 159 ms
104,644 KB
testcase_20 AC 131 ms
97,052 KB
testcase_21 AC 139 ms
98,332 KB
testcase_22 AC 135 ms
96,272 KB
testcase_23 AC 156 ms
102,672 KB
testcase_24 AC 122 ms
94,360 KB
testcase_25 AC 143 ms
98,772 KB
testcase_26 AC 92 ms
97,768 KB
testcase_27 AC 44 ms
61,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

MOD = 998244353
INF = 10**18

dp = [[-INF]*20001 for _ in range(N+1)]
dp[0][10000] = 0
for i in range(N):
    v, w = A[i]
    for j in range(20001):
        if dp[i][j] == -INF:
            continue
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j+w <= 20000:
            dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j]+v)
MAX = max(dp[-1][:10000+W+1])
cnt = [[0]*20001 for _ in range(N+1)]
cnt[0][10000] = 1
for i in range(N):
    v, w = A[i]
    for j in range(20001):
        if dp[i][j] == -INF:
            continue
        if dp[i][j] == dp[i+1][j]:
            cnt[i+1][j] += cnt[i][j]
            cnt[i+1][j] %= MOD
        if j+w <= 20000 and dp[i][j]+v == dp[i+1][j+w]:
            cnt[i+1][j+w] += cnt[i][j]
            cnt[i+1][j+w] %= MOD

ans = 0
for i in range(10000+W+1):
    if dp[-1][i] == MAX:
        ans += cnt[-1][i]
        ans %= MOD

print(MAX, ans)
0