結果

問題 No.2866 yuusaan's Knapsack
ユーザー tassei903tassei903
提出日時 2024-08-30 21:31:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 851 ms / 2,000 ms
コード長 1,334 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 82,508 KB
実行使用メモリ 258,880 KB
最終ジャッジ日時 2024-08-31 01:27:18
合計ジャッジ時間 14,455 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
80,432 KB
testcase_01 AC 58 ms
72,208 KB
testcase_02 AC 91 ms
87,388 KB
testcase_03 AC 51 ms
67,848 KB
testcase_04 AC 55 ms
67,304 KB
testcase_05 AC 50 ms
67,784 KB
testcase_06 AC 622 ms
252,388 KB
testcase_07 AC 573 ms
255,120 KB
testcase_08 AC 766 ms
257,416 KB
testcase_09 AC 558 ms
252,636 KB
testcase_10 AC 589 ms
252,484 KB
testcase_11 AC 538 ms
256,340 KB
testcase_12 AC 601 ms
255,044 KB
testcase_13 AC 578 ms
254,484 KB
testcase_14 AC 611 ms
256,224 KB
testcase_15 AC 665 ms
253,140 KB
testcase_16 AC 528 ms
257,932 KB
testcase_17 AC 755 ms
258,844 KB
testcase_18 AC 427 ms
257,900 KB
testcase_19 AC 741 ms
257,368 KB
testcase_20 AC 559 ms
258,220 KB
testcase_21 AC 548 ms
256,684 KB
testcase_22 AC 586 ms
256,756 KB
testcase_23 AC 851 ms
258,188 KB
testcase_24 AC 497 ms
257,212 KB
testcase_25 AC 777 ms
258,880 KB
testcase_26 AC 496 ms
247,640 KB
testcase_27 AC 84 ms
83,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
n, W = na()
v,w = zip(*[na() for _ in range(n)])
m = 10000
M = 2 * 10 ** 5
inf = 10**18
mod = 998244353
dp = [-inf] * (M + 1)
dp[m] = 0
dpcnt = [0] * (M + 1)
dpcnt[m] = 1

for i in range(n):
    ndp = [-inf] * (M + 1)
    ndpcnt = [0] * (M + 1)
    dp, ndp = ndp, dp
    dpcnt, ndpcnt = ndpcnt, dpcnt
    for j in range(M+1):
        if 0 <= j + w[i] <= M:
            if dp[j+w[i]] < ndp[j] + v[i]:
                dp[j+w[i]] = ndp[j] + v[i]
                dpcnt[j+w[i]] = ndpcnt[j]
            elif dp[j+w[i]] == ndp[j] + v[i]:
                dpcnt[j+w[i]] += ndpcnt[j]
                dpcnt[j+w[i]] %= mod
        if dp[j] < ndp[j]:
            dp[j] = ndp[j]
            dpcnt[j] = ndpcnt[j]
        elif dp[j] == ndp[j]:
            dpcnt[j] += ndpcnt[j]
            dpcnt[j] %= mod
    #print(dp)
    #print(dpcnt)

ans = max(dp[:m + W + 1])
ans_cnt = 0
for i in range(m + W + 1):
    if dp[i] == ans:
        ans_cnt += dpcnt[i]
        ans_cnt %= mod

print(ans, ans_cnt)
0