from collections import deque from typing import Deque, Tuple class SlidingWindowMaximum: l : int r : int dq : Deque[Tuple[int, int]] def __init__(self) -> None: self.clear() def clear(self) -> None: self.l = 0 self.r = 0 self.dq = deque() def push_back(self, v : int) -> None: while self.dq and self.dq[-1][0] < v: self.dq.pop() self.dq.append((v, self.r)) self.r += 1 def pop_front(self) -> None: assert self.size() if self.dq[0][1] == self.l: self.dq.popleft() self.l += 1 def query(self) -> int: return self.dq[0][0] def size(self) -> int: return self.r - self.l N = int(input()) items = [(int(0), int(0))] for _ in range(N): c, v = map(int, input().split()) items.append((c, v)) INF = 1 << 60 dp = [[-INF] * (N + 1) for i in range(N + 1)] dp[0][0] = 0 sw = SlidingWindowMaximum() for i in reversed(range(1, N + 1)): max_num = min(N, N // i) max_sum = N c, v = items[i] # dp[num][sum] = max{ pd[num-p][sum-p*i]-(num-p)*v[i] | 0<=p<=c[i] } ⋃ {-∞} + num*v[i] for num in range(max_num + 1): for sum in range(i if num else max_sum + 1): max_p = min(max_num - num, (max_sum - sum) // i) sw.clear() for p in range(max_p + 1): sw.push_back(dp[num + p][sum + i * p] - (num + p) * v) if p > c: sw.pop_front() dp[num + p][sum + i * p] = sw.query() + (num + p) * v for k in range(1, N + 1): print(max(dp[k]))