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, M, W = map(int, input().split()) items = [(int(0), int(0))] for _ in range(N): c, v = map(int, input().split()) items.append((c, v)) INF = 1 << 60 dp = [[-INF] * (W + 1) for i in range(M + 1)] dp[0][0] = 0 sw = SlidingWindowMaximum() for i in reversed(range(1, N + 1)): max_num = min(M, W // i) max_sum = W 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) if max_p <= c: max_val = -INF for p in range(max_p + 1): max_val = max(max_val, dp[num + p][sum + i * p] - (num + p) * v) dp[num + p][sum + i * p] = max_val + (num + p) * v else: sw.clear() for p in range(c + 1): sw.push_back(dp[num + p][sum + i * p] - (num + p) * v) dp[num + p][sum + i * p] = sw.query() + (num + p) * v for p in range(c + 1, max_p + 1): sw.push_back(dp[num + p][sum + i * p] - (num + p) * v) sw.pop_front() dp[num + p][sum + i * p] = sw.query() + (num + p) * v ans = [0] * (M + 1) for k in range(1, M + 1): ans[k] = max(dp[k]) if ans[k] < -INF // 2: ans[k] = -INF print(ans[k])