Glass Half Spilled
Contest: Codeforces Round 691 (Div. 1)
Difficulty rating: 2000
Tags: dp

There are $$$n$$$ glasses on the table numbered $$$1, \ldots, n$$$. The glass $$$i$$$ can hold up to $$$a_i$$$ units of water, and currently contains $$$b_i$$$ units of water.

You would like to choose $$$k$$$ glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.

Formally, suppose a glass $$$i$$$ currently contains $$$c_i$$$ units of water, and a glass $$$j$$$ contains $$$c_j$$$ units of water. Suppose you try to transfer $$$x$$$ units from glass $$$i$$$ to glass $$$j$$$ (naturally, $$$x$$$ can not exceed $$$c_i$$$). Then, $$$x / 2$$$ units is spilled on the floor. After the transfer is done, the glass $$$i$$$ will contain $$$c_i - x$$$ units, and the glass $$$j$$$ will contain $$$\min(a_j, c_j + x / 2)$$$ units (excess water that doesn't fit in the glass is also spilled).

Each time you transfer water, you can arbitrarlly choose from which glass $$$i$$$ to which glass $$$j$$$ to pour, and also the amount $$$x$$$ transferred can be any positive real number.

For each $$$k = 1, \ldots, n$$$, determine the largest possible total amount of water that can be collected in arbitrarily chosen $$$k$$$ glasses after transferring water between glasses zero or more times.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of glasses.

The following $$$n$$$ lines describe the glasses. The $$$i$$$-th of these lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq b_i \leq a_i \leq 100$$$, $$$a_i > 0$$$) — capacity, and water amount currently contained for the glass $$$i$$$, respectively.

Output format:
Print $$$n$$$ real numbers — the largest amount of water that can be collected in $$$1, \ldots, n$$$ glasses respectively. Your answer will be accepted if each number is within $$$10^{-9}$$$ absolute or relative tolerance of the precise answer.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the sample case, you can act as follows:

- for $$$k = 1$$$, transfer water from the first two glasses to the third one, spilling $$$(5 + 5) / 2 = 5$$$ units and securing $$$2 + (5 + 5) / 2 = 7$$$ units;
- for $$$k = 2$$$, transfer water from the third glass to any of the first two, spilling $$$2 / 2 = 1$$$ unit and securing $$$5 + 5 + 2 / 2 = 11$$$ units;
- for $$$k = 3$$$, do nothing. All $$$5 + 5 + 2 = 12$$$ units are secured.

Example input:
3
6 5
6 5
10 2

Example output:
7.0000000000 11.0000000000 12.0000000000

Editorial:
Suppose that we want to collect water in a certain set of chosen glasses $$$S$$$. Let $$$A_S$$$ be the total capacity of chosen glasses, and $$$B_S$$$ be the total amount of water currently contained in chosen glasses. Also, let $$$B$$$ be the total amount of water in all glasses.
Clearly, the optimal way is to directly transfer water from non-chosen glasses to chosen ones. Then, we already secured $$$B_S$$$ units, and we can transfer up to $$$B - B_S$$$ units, thus the largest possible amount is $$$B_S + (B - B_S) / 2 = B / 2 + B_S / 2$$$. But all this water may not fit into the chosen glasses, so the actual amount we collect is $$$\min(A_S, B / 2 + B_s / 2)$$$.
To find the optimum answer for each $$$k$$$, let's use dynamic programming: define $$$dp[i][k][A]$$$ as the largest possible $$$B_S$$$ for a subset of $$$k$$$ glasses among $$$1, \ldots, i$$$ such that the total capacity in the subset is $$$A$$$. We can recalculate this with transition $$$dp[i][k][A] = \max(dp[i - 1][k][A], dp[i - 1][k - 1][A - a[i]] + b[i])$$$. Then, $$$ans[k] = \max_A \min(A, dp[n][k][A] / 2 + B / 2)$$$. Time complexity is $$$O(n^3 \max a_i)$$$, where $$$\max a_i$$$ is the largest capacity of a glass. We can also get rid of storing the first dimension $$$i$$$ by overwriting DP values as $$$i$$$ increases, and obtain $$$O(n^2 \max a_i)$$$ memory.