結果

問題 No.2866 yuusaan's Knapsack
ユーザー 寝癖寝癖
提出日時 2024-08-18 19:35:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 1,048 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 82,012 KB
実行使用メモリ 111,084 KB
最終ジャッジ日時 2024-08-31 01:27:10
合計ジャッジ時間 8,316 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
75,272 KB
testcase_01 AC 78 ms
74,708 KB
testcase_02 AC 76 ms
75,076 KB
testcase_03 AC 77 ms
75,468 KB
testcase_04 AC 75 ms
74,976 KB
testcase_05 AC 77 ms
74,928 KB
testcase_06 AC 339 ms
103,448 KB
testcase_07 AC 286 ms
99,488 KB
testcase_08 AC 352 ms
109,368 KB
testcase_09 AC 322 ms
106,304 KB
testcase_10 AC 292 ms
95,272 KB
testcase_11 AC 253 ms
96,500 KB
testcase_12 AC 298 ms
102,508 KB
testcase_13 AC 271 ms
102,788 KB
testcase_14 AC 293 ms
99,888 KB
testcase_15 AC 320 ms
101,008 KB
testcase_16 AC 307 ms
99,840 KB
testcase_17 AC 382 ms
110,380 KB
testcase_18 AC 270 ms
95,844 KB
testcase_19 AC 383 ms
105,020 KB
testcase_20 AC 331 ms
105,940 KB
testcase_21 AC 352 ms
107,228 KB
testcase_22 AC 314 ms
101,528 KB
testcase_23 AC 395 ms
111,084 KB
testcase_24 AC 291 ms
95,428 KB
testcase_25 AC 354 ms
109,324 KB
testcase_26 AC 102 ms
80,484 KB
testcase_27 AC 78 ms
75,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from dataclasses import dataclass
from collections import defaultdict

N, W = map(int, input().split())
v, w = map(list, zip(*[map(int, input().split()) for _ in range(N)]))

# 重さが小さい順にソート
vw = sorted(zip(v, w), key=lambda x: x[1])
v, w = map(list, zip(*vw))

@dataclass
class Data:
    max: int
    cnt: int
    def __add__(self, other):
        if self.max < other.max:
            return other
        elif self.max > other.max:
            return self
        else:
            return Data(self.max, (self.cnt + other.cnt)%998244353)
    def __repr__(self) -> str:
        return f"({self.max}, {self.cnt})"

M = 20001
inf = 10**18
now = defaultdict(lambda: Data(-inf, 0))
now[0] = Data(0, 1)

for i in range(N):
    nxt = defaultdict(lambda: Data(-inf, 0))
    for j in now.keys():
        # 使う場合
        if j+w[i] <= W:
            nxt[j+w[i]] += Data(now[j].max+v[i], now[j].cnt)
        # 使わない場合
        nxt[j] += now[j]
    now = nxt

ans = sum(now.values(), Data(-inf, 0))
print(ans.max, ans.cnt)
0