結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー chineristACchineristAC
提出日時 2022-04-24 13:48:51
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,561 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 150,132 KB
最終ジャッジ日時 2023-09-29 04:03:35
合計ジャッジ時間 69,462 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
92,284 KB
testcase_01 AC 274 ms
92,204 KB
testcase_02 AC 271 ms
92,208 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 273 ms
93,516 KB
testcase_06 AC 327 ms
95,788 KB
testcase_07 RE -
testcase_08 AC 292 ms
95,112 KB
testcase_09 AC 329 ms
95,564 KB
testcase_10 AC 313 ms
96,196 KB
testcase_11 AC 329 ms
96,020 KB
testcase_12 AC 268 ms
92,532 KB
testcase_13 AC 332 ms
95,676 KB
testcase_14 AC 317 ms
95,900 KB
testcase_15 AC 267 ms
92,332 KB
testcase_16 RE -
testcase_17 AC 318 ms
96,080 KB
testcase_18 AC 271 ms
92,592 KB
testcase_19 AC 318 ms
96,148 KB
testcase_20 AC 270 ms
92,680 KB
testcase_21 AC 322 ms
96,228 KB
testcase_22 AC 307 ms
95,328 KB
testcase_23 RE -
testcase_24 AC 272 ms
92,676 KB
testcase_25 AC 274 ms
92,576 KB
testcase_26 AC 322 ms
95,940 KB
testcase_27 RE -
testcase_28 AC 523 ms
97,964 KB
testcase_29 AC 525 ms
98,332 KB
testcase_30 AC 524 ms
98,048 KB
testcase_31 AC 538 ms
97,876 KB
testcase_32 AC 484 ms
97,880 KB
testcase_33 AC 536 ms
98,360 KB
testcase_34 AC 515 ms
98,276 KB
testcase_35 AC 484 ms
97,552 KB
testcase_36 AC 507 ms
98,240 KB
testcase_37 AC 509 ms
97,904 KB
testcase_38 AC 6,550 ms
146,656 KB
testcase_39 AC 6,970 ms
150,132 KB
testcase_40 AC 6,899 ms
148,412 KB
testcase_41 AC 6,852 ms
149,124 KB
testcase_42 AC 6,801 ms
147,560 KB
testcase_43 AC 7,057 ms
147,708 KB
testcase_44 AC 6,908 ms
149,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd
from fractions import Fraction


input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

local_test = False

def random_test(N):
    item = [(N,random.randint(-10**2,10**2))]
    for i in range(1,N):
        c = random.randint(1,2)
        v = random.randint(-10**2,10**2)
        item.append((c,v))
    return item

def solve_slow(N,item):
    dp = [[-INF]*(N+1) for w in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        c,v = item[i]
        for w in range(N+1)[::-1]:
            for k in range(N+1)[::-1]:
                for use in range(c+1):
                    if w+use*(i+1) <= N and k+use <= N:
                        dp[w+use*(i+1)][k+use] = max(dp[w+use*(i+1)][k+use],dp[w][k]+use*v)
    
    return [max(dp[w][k] for w in range(N+1)) for k in range(1,N+1)]

def solve(N,item):
    dp = [[-INF]*(N+1) for i in range(N+1)]
    dp[0][0] = 0
    for i in range(N)[::-1]:
        c,v = item[i]
        k_max = N//(i+1)
        for w_start in range(i+1):
            for k_start in range(k_max+1):
                deq = deque()
                for k in range(k_start,k_max+1):
                
                    w = w_start + (k-k_start) * (i+1)
                    if N < w:
                        break
                    if deq and deq[0][1] < k-c:
                        deq.popleft()
                    tmp = dp[w][k] - k * v
                    while deq and deq[-1][0] <= tmp:
                        deq.pop()
                    deq.append((tmp,k))

                    dp[w][k] = v*k + deq[0][0]
                    

        for w_start in range(i+1,N+1):
            for k_start in range(1):
                deq = deque()
                for k in range(k_start,k_max+1):
                    w = w_start + (k-k_start) * (i+1)
                    if N < w:
                        break
                    if deq and deq[0][1] < k-c:
                        deq.popleft()
                    tmp = dp[w][k] - k * v
                    while deq and deq[-1][0] <= tmp:
                        deq.pop()
                    deq.append((tmp,k))

                    dp[w][k] = v*k + deq[0][0]
                    

    res = [max(dp[w][k] for w in range(N+1)) for k in range(1,N+1)]
    return res
    
    
INF = 10**17


N = int(input())
item = [tuple(mi()) for i in range(N)]
print(*solve(N,item),sep="\n")
0