Kalila and Dimna in the Logging Industry (Codeforces 319C)
Tags: DP, geometry

Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a1, a2, ..., an. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height ai in the beginning), then the cost of charging the chain saw would be bi. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, ai < aj and bi > bj and also bn = 0 and a1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you?

Input: The first line of input contains an integer n (1 ≤ n ≤ 105). The second line of input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line of input contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 109).It's guaranteed that a1 = 1, bn = 0, a1 < a2 < ... < an and b1 > b2 > ... > bn.

Output: The only line of output must contain the minimum cost of cutting all the trees completely.Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.


Sample Inputs:
5
1 2 3 4 5
5 4 3 2 0

6
1 2 3 10 20 30
6 5 4 3 2 0

Sample Outputs:
25

138

Solution (Python):
N = 100100

def main():
    n = int(input())
    d = [0] * N
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    (m, c) = ([0] * N, [0] * N)
    m[0] = b[0]
    c[0] = d[0]
    z = 1
    p = 0
    for i in range(1, n):
        p = min(p, z)
        while p + 1 < z and m[p + 1] * a[i] + c[p + 1] <= m[p] * a[i] + c[p]:
            p = p + 1
        d[i] = m[p] * a[i] + c[p]
        while z >= 2 and (c[z - 2] - c[z - 1]) * (b[i] - m[z - 1]) >= (c[z - 1] - d[i]) * (m[z - 1] - m[z - 2]):
            z = z - 1
        (m[z], c[z]) = (b[i], d[i])
        z = z + 1
    print(d[n - 1])
main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
stk = [0]
for i in range(1, n):
    while len(stk) > 1 and c[stk[1]] - c[stk[0]] <= a[i] * (b[stk[0]] - b[stk[1]]):
        del stk[0]
    c[i] = c[stk[0]] + a[i] * b[stk[0]]
    while len(stk) > 1 and (c[stk[-1]] - c[stk[-2]]) * (b[stk[-1]] - b[i]) > (b[stk[-2]] - b[stk[-1]]) * (c[i] - c[stk[-1]]):
        del stk[-1]
    stk.append(i)
print(c[n - 1])

Solution (Python):
def dot(a, b, c):
    return a + c * b

def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    b = list(map(int, input().split(' ')))
    dp = [0 for _ in range(n)]
    st = [0 for _ in range(n)]
    (i, j) = (0, 0)
    for k in range(1, n):
        while j - i > 0 and dot(dp[st[i]], b[st[i]], a[k]) >= dot(dp[st[i + 1]], b[st[i + 1]], a[k]):
            i += 1
        dp[k] = a[k] * b[st[i]] + dp[st[i]]
        while j - i > 0 and (b[st[j]] - b[k]) * (dp[st[j]] - dp[st[j - 1]]) > (dp[k] - dp[st[j]]) * (b[st[j - 1]] - b[st[j]]):
            j -= 1
        j += 1
        st[j] = k
    print(dp[-1])
if __name__ == '__main__':
    main()