結果

問題 No.2866 yuusaan's Knapsack
コンテスト
ユーザー 寝癖
提出日時 2024-08-18 19:35:14
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 1,048 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 173 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 115,324 KB
最終ジャッジ日時 2026-04-13 17:08:51
合計ジャッジ時間 6,338 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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