Submission #8518414


Source Code Expand

from collections import defaultdict
from itertools import product, accumulate

N, W = map(int, input().split())
items = defaultdict(list)
for _ in range(N):
    w, v = map(int, input().split())
    items[w].append(v)
weights = list(items.keys())
nums = {w: len(items[w]) for w in weights}
cumsum = {w: None for w in weights}
for i, w in enumerate(weights):
    items[w].sort(reverse=True)
    cumsum[w] = [0] + list(accumulate(items[w]))
ans = 0
for tup in product(*[range(nums[w] + 1) for w in weights]):
    temp_weight = sum(w * t for w, t in zip(weights, tup))
    if temp_weight > W: continue
    temp_value = sum(cumsum[w][t] for w, t in zip(weights, tup))
    ans = max(temp_value, ans)
print(ans)

Submission Info

Submission Time
Task D - Simple Knapsack
User taq225
Language Python (3.4.3)
Score 400
Code Size 724 Byte
Status AC
Exec Time 708 ms
Memory 3316 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 16
Set Name Test Cases
Sample example0, example1, example2, example3
All antigreedy0, antigreedy1, antigreedy2, example0, example1, example2, example3, quarter0, quarter1, quarter2, rand0, rand1, rand2, smallw0, smallw1, smallw2
Case Name Status Exec Time Memory
antigreedy0 AC 21 ms 3316 KB
antigreedy1 AC 21 ms 3316 KB
antigreedy2 AC 21 ms 3316 KB
example0 AC 20 ms 3316 KB
example1 AC 20 ms 3316 KB
example2 AC 20 ms 3316 KB
example3 AC 21 ms 3316 KB
quarter0 AC 708 ms 3316 KB
quarter1 AC 612 ms 3316 KB
quarter2 AC 489 ms 3316 KB
rand0 AC 21 ms 3316 KB
rand1 AC 53 ms 3316 KB
rand2 AC 22 ms 3316 KB
smallw0 AC 484 ms 3316 KB
smallw1 AC 483 ms 3316 KB
smallw2 AC 445 ms 3316 KB