結果

問題 No.2866 yuusaan's Knapsack
ユーザー 寝癖寝癖
提出日時 2024-07-18 23:24:26
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,036 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 82,060 KB
実行使用メモリ 110,960 KB
最終ジャッジ日時 2024-08-18 19:23:46
合計ジャッジ時間 8,878 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
75,300 KB
testcase_01 AC 72 ms
74,996 KB
testcase_02 AC 72 ms
74,864 KB
testcase_03 AC 72 ms
75,172 KB
testcase_04 AC 70 ms
75,476 KB
testcase_05 AC 70 ms
75,280 KB
testcase_06 AC 306 ms
102,968 KB
testcase_07 AC 251 ms
99,612 KB
testcase_08 AC 301 ms
109,428 KB
testcase_09 AC 281 ms
106,068 KB
testcase_10 AC 257 ms
95,724 KB
testcase_11 AC 218 ms
96,116 KB
testcase_12 AC 253 ms
102,236 KB
testcase_13 AC 242 ms
102,372 KB
testcase_14 AC 270 ms
99,880 KB
testcase_15 AC 275 ms
100,980 KB
testcase_16 AC 260 ms
99,844 KB
testcase_17 AC 334 ms
110,516 KB
testcase_18 AC 233 ms
95,940 KB
testcase_19 AC 321 ms
105,012 KB
testcase_20 AC 292 ms
105,004 KB
testcase_21 AC 308 ms
107,232 KB
testcase_22 AC 272 ms
101,660 KB
testcase_23 AC 335 ms
110,960 KB
testcase_24 AC 249 ms
95,044 KB
testcase_25 AC 306 ms
109,344 KB
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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)
    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