def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 groups = [] for i in range(1, N+1): c_i = int(input[idx]) v_i = int(input[idx+1]) idx += 2 groups.append((i, c_i, v_i)) # Separate group 1 (i=1) group1 = groups[0] i1, c1, v1 = group1 assert i1 == 1 and c1 == N other_groups = groups[1:] # Initialize DP[x][s] = max value using x items from other groups, sum (i-1)*t_i = s INF = -1 << 60 dp = [[INF] * (N + 1) for _ in range(N + 1)] dp[0][0] = 0 for i, c_i, v_i in other_groups: weight = i value_diff = v_i - v1 excess_per = weight - 1 # Process this group # We need to consider taking t items from this group, t from 0 to c_i # For each possible t, update the DP # To handle this efficiently, we can use a temporary array and iterate backwards temp = [row[:] for row in dp] for t in range(1, c_i + 1): t_excess = t * excess_per t_value = t * value_diff for x in range(N, t - 1, -1): for s in range(N, t_excess - 1, -1): if temp[x - t][s - t_excess] != INF: if dp[x][s] < temp[x - t][s - t_excess] + t_value: dp[x][s] = temp[x - t][s - t_excess] + t_value # Precompute the maximum for each x and s # For each k, the answer is k*v1 + max(dp[x][s] where x <=k and s <= N -k) for k in range(1, N + 1): max_extra = INF for x in range(0, k + 1): max_s = N - k if max_s < 0: continue for s in range(0, max_s + 1): if dp[x][s] > max_extra: max_extra = dp[x][s] print(max_extra + k * v1) if __name__ == '__main__': main()