Vasya And The Mushrooms (Codeforces 1016C)
Tags: Implement, dp

Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.Help Vasya! Calculate the maximum total weight of mushrooms he can collect.

Input: The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.

Output: Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.

Note: In the first test case, the optimal route is as follows:    Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.In the second test case, the optimal route is as follows:    Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.

Sample Inputs:
3
1 2 3
6 5 4

3
1 1000 10000
10 100 100000

Sample Outputs:
70

543210

Solution (Python):
n = int(input())
F = list(map(int, input().split()))
S = list(map(int, input().split()))
Sum = [0] * n
Sum[n - 1] = F[n - 1] + S[n - 1]
for i in range(n - 2, -1, -1):
    Sum[i] = Sum[i + 1] + F[i] + S[i]
ans1 = 0
for i in range(n):
    ans1 += F[i] * i
    ans1 += S[i] * (2 * n - i - 1)
ans2 = 0
for i in range(n):
    ans2 += S[i] * (i + 1)
for i in range(1, n):
    ans2 += F[i] * (2 * n - i)
x = ans1
y = ans2
ans = [x, y]
for i in range(0, max(0, n - 2), 2):
    x = x + F[i + 1] * 2 - S[i] * (2 * n - 2 - 2 * i) - S[i + 1] * (2 * n - 4 - 2 * i) + Sum[i + 2] * 2
    y = y - F[i + 1] * (2 * n - 2 * i - 4) - F[i + 2] * (2 * n - 2 * i - 4) + Sum[i + 2] * 2
    ans.append(x)
    ans.append(y)
print(max(ans))

Solution (Python):
def main():
    [n] = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    b = list(map(int, input().split(' ')))
    sums = []
    for i in range(n):
        if len(sums) == 0:
            sums.append(a[-1] + b[-1])
        else:
            sums.append(sums[-1] + a[-i - 1] + b[-i - 1])
    bottom_to_top = []
    top_to_bottom = []
    for i in range(n):
        if i == 0:
            bottom_to_top.append(a[-1])
            top_to_bottom.append(b[-1])
        else:
            bottom_to_top.append(bottom_to_top[-1] + sums[i - 1] + a[-i - 1] * (2 * i + 1))
            top_to_bottom.append(top_to_bottom[-1] + sums[i - 1] + b[-i - 1] * (2 * i + 1))
    best_t = []
    best_b = []
    for i in range(n):
        if i == 0:
            best_t.append(top_to_bottom[0])
            best_b.append(bottom_to_top[0])
        else:
            x = b[-i - 1] + 2 * sums[i - 1] + best_b[i - 1]
            y = a[-i - 1] + 2 * sums[i - 1] + best_t[i - 1]
            best_t.append(max([x, top_to_bottom[i]]))
            best_b.append(max([y, bottom_to_top[i]]))
    print(best_t[-1])
main()

Solution (Python):
def main():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    roundup = []
    rounddown = []
    sumsofar = []
    for (i, (a, b)) in enumerate(zip(reversed(A), reversed(B))):
        ssf = sumsofar[-1] if sumsofar else 0
        t = roundup[-1] if roundup else 0
        roundup.append(t + ssf + b * (1 + 2 * i))
        t = rounddown[-1] if rounddown else 0
        rounddown.append(t + ssf + a * (1 + 2 * i))
        sumsofar.append(ssf + a + b)
    roundup = list(reversed(roundup))
    rounddown = list(reversed(rounddown))
    sumsofar = list(reversed(sumsofar))
    bestres = 0
    sofar = 0
    for (i, (a, b)) in enumerate(zip(A, B)):
        (rup, rdo) = (roundup, rounddown)
        if i % 2:
            (a, b) = (b, a)
            (rup, rdo) = (rdo, rup)
        bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i)
        sofar += a * 2 * i
        sofar += b * (2 * i + 1)
        bestres = max(bestres, sofar)
    print(bestres)
if __name__ == '__main__':
    main()