Glass Half Spilled (Codeforces 1459D)
Tags: DP, brute force

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: 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: 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.

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.

Sample Inputs:
3
6 5
6 5
10 2

Sample Outputs:
7.0000000000 11.0000000000 12.0000000000

Solution (Python):
n = int(input())
lst = [list(map(int, input().split())) for i in range(n)]
t_c = 0
cu = 0
for i in range(n):
    t_c = t_c + lst[i][0]
for i in range(n):
    cu = cu + lst[i][1]
tc = 0
dp = [[-1] * (t_c + 1) for i in range(n + 1)]
dp[0][0] = 0
for (i, [a, b]) in enumerate(lst):
    for k in range(i, -1, -1):
        for t in range(tc + 1):
            if dp[k][t] != -1:
                dp[k + 1][t + a] = max(dp[k + 1][t + a], dp[k][t] + b)
    tc = tc + a
for i in range(1, n + 1):
    for j in range(1, t_c + 1):
        if dp[i][j] != -1:
            dp[i][j] = min(j, dp[i][j] + (cu - dp[i][j]) / 2)
for i in range(1, n + 1):
    print(max(dp[i]))

Solution (Python):
n = int(input())
ma = [[int(i) for i in input().split()] for j in range(n)]
mc = 0
s = 0
for i in range(n):
    mc = mc + ma[i][0]
    s = s + ma[i][1]
dp = [[-1] * (mc + 1) for i in range(n + 1)]
dp[0][0] = 0
mc = 0
for (i, [a, b]) in enumerate(ma):
    for k in range(i, -1, -1):
        for j in range(mc + 1):
            if dp[k][j] != -1:
                dp[k + 1][j + a] = max(dp[k + 1][j + a], dp[k][j] + b)
    mc = mc + a
for i in range(1, n + 1):
    for j in range(1, mc + 1):
        if dp[i][j] != -1:
            dp[i][j] = min(j, dp[i][j] + (s - dp[i][j]) / 2)
for i in range(1, n + 1):
    print(max(dp[i]))

Solution (Python):
n = int(input())
glass = [tuple(map(int, input().split())) for _ in range(n)]
dp = [[-1] * 10001 for _ in range(n + 1)]
dp[0][0] = 0
(A_tot, B_tot) = (0, 0)
for (i, (ai, bi)) in enumerate(glass):
    for k in range(i, -1, -1):
        for A in range(A_tot + 1):
            if dp[k][A] != -1:
                dp[k + 1][A + ai] = max(dp[k + 1][A + ai], dp[k][A] + bi)
    A_tot += ai
    B_tot += bi
print(*[max((min(A, dp[k][A] / 2 + B_tot / 2) for A in range(A_tot + 1) if dp[k][A] > -1)) for k in range(1, n + 1)])