Minimum Grid Path (Codeforces 1499C)
Tags: Math, data structures

Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$.You can move only in two directions:   to the right, i. e. horizontally and in the direction that increase your $$$x$$$ coordinate,  or up, i. e. vertically and in the direction that increase your $$$y$$$ coordinate. In other words, your path will have the following structure:   initially, you choose to go to the right or up;  then you go some positive integer distance in the chosen direction (distances can be chosen independently);  after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $$$n - 1$$$ direction changes.As a result, your path will be a polygonal chain from $$$(0, 0)$$$ to $$$(n, n)$$$, consisting of at most $$$n$$$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.Not all paths are equal. You have $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the cost of the $$$i$$$-th segment.Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $$$k$$$ segments ($$$k \le n$$$), then the cost of the path is equal to $$$\sum\limits_{i=1}^{k}{c_i \cdot length_i}$$$ (segments are numbered from $$$1$$$ to $$$k$$$ in the order they are in the path).Find the path of the minimum cost and print its cost.

Input: The first line contains the single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.The first line of each test case contains the single integer $$$n$$$ ($$$2 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^9$$$) — the costs of each segment.It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$.

Output: For each test case, print the minimum possible cost of the path from $$$(0, 0)$$$ to $$$(n, n)$$$ consisting of at most $$$n$$$ alternating segments.

Note: In the first test case, to reach $$$(2, 2)$$$ you need to make at least one turn, so your path will consist of exactly $$$2$$$ segments: one horizontal of length $$$2$$$ and one vertical of length $$$2$$$. The cost of the path will be equal to $$$2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$$$.In the second test case, one of the optimal paths consists of $$$3$$$ segments: the first segment of length $$$1$$$, the second segment of length $$$3$$$ and the third segment of length $$$2$$$.The cost of the path is $$$1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$$$.In the third test case, one of the optimal paths consists of $$$4$$$ segments: the first segment of length $$$1$$$, the second one — $$$1$$$, the third one — $$$4$$$, the fourth one — $$$4$$$. The cost of the path is $$$1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$$$.

Sample Inputs:
3
2
13 88
3
2 3 1
5
4 3 2 1 4

Sample Outputs:
202
13
19

Solution (Python):
def get_cost(h, v, min_h, min_v, n, cnt_h, cnt_v):
    return h - min_h + min_h * (n - cnt_h) + (v - min_v) + min_v * (n - cnt_v)

def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    min_h = a[0]
    min_v = a[1]
    h = a[0]
    v = a[1]
    cnt_h = 0
    cnt_v = 0
    ans = min_h * n + min_v * n
    for i in range(2, n):
        if i % 2 == 0:
            cnt_h += 1
            min_h = min(min_h, a[i])
            h += a[i]
            ans = min(ans, get_cost(h, v, min_h, min_v, n, cnt_h, cnt_v))
        else:
            cnt_v += 1
            min_v = min(min_v, a[i])
            v += a[i]
            ans = min(ans, get_cost(h, v, min_h, min_v, n, cnt_h, cnt_v))
    print(ans)
t = int(input())
for _ in range(t):
    solve()

Solution (Python):
def ints():
    return list(map(int, input().split()))

def case():
    (n,) = ints()
    c = ints()
    cx = c[::2]
    cy = c[1::2]
    cy.append(cy[-1])
    res = cx[0] * n + cy[0] * n
    mincx = cx[0]
    sumcx = cx[0]
    mincy = cy[0]
    sumcy = cy[0]
    for i in range(1, (n + 1) // 2):
        if cx[i] < mincx:
            mincx = cx[i]
        sumcx += cx[i]
        cost = sumcx + mincx * (n - i - 1) + sumcy + mincy * (n - i)
        if cost < res:
            res = cost
        if cy[i] < mincy:
            mincy = cy[i]
        sumcy += cy[i]
        cost = sumcx + mincx * (n - i - 1) + sumcy + mincy * (n - i - 1)
        if cost < res:
            res = cost
    return res
(t,) = ints()
for i in range(t):
    print(case())

Solution (Python):
for _ in range(int(input())):
    x = int(input())
    zoo = list(map(int, input().split()))
    ans = zoo[0] * x + zoo[1] * x
    sum1 = zoo[0]
    sum2 = zoo[1]
    min1 = zoo[0]
    min2 = zoo[1]
    count1 = 1
    count2 = 1
    p2 = zoo[1] * x
    for i in range(2, x):
        if i % 2 == 0:
            sum1 += zoo[i]
            count1 += 1
            min1 = min(min1, zoo[i])
            p1 = sum1 + min1 * (x - count1)
        else:
            sum2 += zoo[i]
            count2 += 1
            min2 = min(min2, zoo[i])
            p2 = sum2 + min2 * (x - count2)
        ans = min(ans, p1 + p2)
    print(ans)