from typing import List INF = 1 << 60 class RangeMaxSegmentTree: _n : int _dat : List[int] def __init__(self, dat: List[int]) -> None: self._n = len(dat) self._dat = [0] * self._n + dat for i in reversed(range(1, self._n)): self._dat[i] = max(self._dat[2 * i], self._dat[2 * i + 1]) def max(self, l : int, r : int) -> int: l += self._n r += self._n res = -INF while l < r: if l & 1: res = max(res, self._dat[l]) l += 1 if r & 1: r -= 1 res = max(res, self._dat[r]) l >>= 1 r >>= 1 return res N = int(input()) items = [(int(0), int(0))] for _ in range(N): c, v = map(int, input().split()) items.append((c, v)) dp = [[-INF] * (N + 1) for i in range(N + 1)] dp[0][0] = 0 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) seg = RangeMaxSegmentTree([dp[num + p][sum + i * p] - (num + p) * v for p in range(max_p + 1)]) for p in range(max_p + 1): dp[num + p][sum + i * p] = seg.max(max(0, p - c), p + 1) + (num + p) * v for k in range(1, N + 1): print(max(dp[k]))