Birds (Codeforces 922E)
Tags: DP, dp

Apart from plush toys, Imp is a huge fan of little yellow birds!  To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest. Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?

Input: The first line contains four integers n, W, B, X (1 ≤ n ≤ 103, 0 ≤ W, B, X ≤ 109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ 104) — where ci is the number of birds living in the i-th nest. It is guaranteed that .The third line contains n integers cost1, cost2, ..., costn (0 ≤ costi ≤ 109), where costi is the mana cost to summon a bird from the i-th nest.

Output: Print a single integer — the maximum number of birds Imp can summon.

Note: In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6.In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.

Sample Inputs:
2 12 0 4
3 4
4 2

4 1000 10 35
1 2 4 5
1000 500 250 200

2 10 7 11
2 10
6 1

Sample Outputs:
6

5

11

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, w, b, x) = map(int, input().split())
c = list(map(int, input().split()))
cost = list(map(float, input().split()))
(w, b) = (float(w), float(b))
dp0 = [w]
s = 0
for (i, u) in zip(c, cost):
    s += i
    dp = [-1] * (s + 1)
    v = b - u
    (q, k) = ([], 0)
    for j in range(s + 1):
        if j < len(dp0):
            dp0[j] = min(dp0[j] + x, w + j * b)
            y = dp0[j]
            while len(q) ^ k and dp0[q[-1]] - u * (j - q[-1]) <= y:
                q.pop()
            q.append(j)
        while len(q) ^ k and q[k] + i < j:
            k += 1
        if len(q) ^ k:
            dp[j] = min(dp0[q[k]] - u * (j - q[k]), w + j * b)
    dp0 = dp
    while dp0[-1] < 0:
        dp0.pop()
        s -= 1
ans = len(dp) - 1
print(ans)

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, w, b, x) = map(int, input().split())
c = list(map(int, input().split()))
cost = list(map(int, input().split()))
dp0 = [w]
s = 0
for (i, u) in zip(c, cost):
    s += i
    dp = [-1] * (s + 1)
    v = b - u
    (q, k) = ([], 0)
    for j in range(s + 1):
        if j < len(dp0):
            dp0[j] = min(dp0[j] + x, w + j * b)
            y = dp0[j]
            while len(q) ^ k and dp0[q[-1]] - u * (j - q[-1]) <= y:
                q.pop()
            q.append(j)
        while len(q) ^ k and q[k] + i < j:
            k += 1
        if len(q) ^ k:
            dp[j] = min(dp0[q[k]] - u * (j - q[k]), w + j * b)
    dp0 = dp
    while dp0[-1] < 0:
        dp0.pop()
        s -= 1
ans = len(dp) - 1
print(ans)

Solution (Python):
(n, w, b, x) = map(int, input().split())
nums = list(map(int, input().split()))
costs = list(map(int, input().split()))
m = sum(nums)
dp = [[-1] * (m + 2) for _ in range(2)]
dp[0][0] = w
dq = [0] * (m + 1)
for i in range(n):
    l = r = 0
    for bird in range(m + 1):
        while l < r and dp[i % 2][dq[r - 1]] + costs[i] * dq[r - 1] < dp[i % 2][bird] + costs[i] * bird:
            r -= 1
        if l < r and dq[l] + nums[i] < bird:
            l += 1
        dq[r] = bird
        r += 1
        v = dp[i % 2][dq[l]] - (bird - dq[l]) * costs[i]
        if v == -1:
            break
        dp[(i + 1) % 2][bird] = min(v + x, w + bird * b)
print(dp[n % 2].index(-1) - 1)