Submission #2896985


Source Code Expand

def d_simple_knapsack(N, W, Baggage):
    from collections import defaultdict
    # dp[w]:ナップサックに重さがwになるように荷物を入れたときの価値の最大値
    dp = defaultdict(int)
    dp[0] = 0  # 重さ0で達成できる価値は0とする
    for w, v in Baggage:
        # dpの各要素(現時点におけるナップサック内の荷物の重さ)に対して、
        # 注目した荷物を入れても重さがWを超えなければ、
        # ナップサックに荷物を入れて価値の最大値を更新する
        tmp = defaultdict(int)  # 価値の最大値を更新できた荷物の重さの情報
        for kw, kv in dp.items():
            if kw + w <= W:
                if kw + w in dp:
                    tmp[kw + w] = max(dp[kw + w], kv + v)
                else:
                    tmp[kw + w] = kv + v
        for key, value in tmp.items():
            dp[key] = value
    ans = max(dp.values())
    return ans

N, W = [int(i) for i in input().split()]
Baggage = [[int(i) for i in input().split()] for j in range(N)]
print(d_simple_knapsack(N, W, Baggage))

Submission Info

Submission Time
Task D - Simple Knapsack
User kenseiQ
Language Python (3.4.3)
Score 400
Code Size 1149 Byte
Status AC
Exec Time 78 ms
Memory 3996 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 24 ms 3316 KB
antigreedy1 AC 24 ms 3316 KB
antigreedy2 AC 25 ms 3316 KB
example0 AC 21 ms 3316 KB
example1 AC 21 ms 3316 KB
example2 AC 21 ms 3316 KB
example3 AC 21 ms 3316 KB
quarter0 AC 78 ms 3996 KB
quarter1 AC 65 ms 3884 KB
quarter2 AC 35 ms 3500 KB
rand0 AC 21 ms 3316 KB
rand1 AC 21 ms 3316 KB
rand2 AC 21 ms 3316 KB
smallw0 AC 21 ms 3316 KB
smallw1 AC 21 ms 3316 KB
smallw2 AC 26 ms 3372 KB