Traveling in Berland (Codeforces 1809F)
Tags: Greedy, graphs

There are $$$n$$$ cities in Berland, arranged in a circle and numbered from $$$1$$$ to $$$n$$$ in clockwise order.You want to travel all over Berland, starting in some city, visiting all the other cities and returning to the starting city. Unfortunately, you can only drive along the Berland Ring Highway, which connects all $$$n$$$ cities. The road was designed by a very titled and respectable minister, so it is one-directional — it can only be traversed clockwise, only from the city $$$i$$$ to the city $$$(i \bmod n) + 1$$$ (i.e. from $$$1$$$ to $$$2$$$, from $$$2$$$ in $$$3$$$, ..., from $$$n$$$ to $$$1$$$).The fuel tank of your car holds up to $$$k$$$ liters of fuel. To drive from the $$$i$$$-th city to the next one, $$$a_i$$$ liters of fuel are needed (and are consumed in the process).Every city has a fuel station; a liter of fuel in the $$$i$$$-th city costs $$$b_i$$$ burles. Refueling between cities is not allowed; if fuel has run out between cities, then your journey is considered incomplete.For each city, calculate the minimum cost of the journey if you start and finish it in that city.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^9$$$) — the number of cities and the volume of fuel tank, respectively.The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$).The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 2$$$).The sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print $$$n$$$ integers, where the $$$i$$$-th of them is equal to the minimum cost of the journey if you start and finish in the $$$i$$$-th city.


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

Sample Outputs:
17 19 17 
13 12 12 12 14 
14 14 14 14 
8 8 8

Solution (Python):
import sys
Z = sys.stdin.readline
Y = lambda : map(int, Z().split())

def f(v, k):
    return v if v <= k else v + v - k

def solve(n, k, a, b):
    if 1 not in b:
        return solve(n, k << 1, [i << 1 for i in a], [1] * n)
    if b[-1] != 1:
        i = b.index(1) + 1
        A = solve(n, k, a[i:] + a[:i], b[i:] + b[:i])
        return A[-i:] + A[:-i]
    f1 = [0]
    f2 = [0]
    s = v = 0
    p = n - 1
    d = [0] * n
    for i in range(n):
        if b[i] == 1:
            f1.append(0)
            s += f(v + a[i - 1], k)
            v = 0
            p = i
        else:
            f1.append(f1[-1] + a[i - 1])
            v += a[i - 1]
            d[i] = p
    for i in range(n - 2, -2, -1):
        if b[i] == 1:
            f2.append(0)
            d[i] = f(v + a[i], k)
            v = 0
        else:
            f2.append(f2[-1] + (a[i] << 1))
            v += a[i]
    f1 = f1[1:]
    f2 = f2[-2::-1]
    A = []
    for i in range(n):
        if b[i] == 1:
            A.append(s)
        else:
            A.append(s - d[d[i]] + f(f1[i], k) + f2[i])
    return A
for _ in range(*Y()):
    (n, k) = Y()
    a = [*Y()]
    b = [*Y()]
    print(*solve(n, k, a, b))

Solution (Python):
import sys
Z = sys.stdin.readline
Y = lambda : map(int, Z().split())

def f(v, k):
    return v if v <= k else v + v - k

def solve(n, k, a, b):
    if 1 not in b:
        return solve(n, k << 1, [i << 1 for i in a], [1] * n)
    if b[-1] != 1:
        i = b.index(1) + 1
        A = solve(n, k, a[i:] + a[:i], b[i:] + b[:i])
        return A[-i:] + A[:-i]
    f1 = [0]
    f2 = [0]
    s = v = 0
    p = n - 1
    d = [0] * n
    for i in range(n):
        if b[i] == 1:
            f1.append(0)
            s += f(v + a[i - 1], k)
            v = 0
            p = i
        else:
            f1.append(f1[-1] + a[i - 1])
            v += a[i - 1]
            d[i] = p
    for i in range(n - 2, -2, -1):
        if b[i] == 1:
            f2.append(0)
            d[i] = f(v + a[i], k)
            v = 0
        else:
            f2.append(f2[-1] + (a[i] << 1))
            v += a[i]
    f1 = f1[1:]
    f2 = f2[-2::-1]
    A = []
    for i in range(n):
        if b[i] == 1:
            A.append(s)
        else:
            A.append(s - d[d[i]] + f(f1[i], k) + f2[i])
    return A
for _ in range(*Y()):
    (n, k) = Y()
    a = [*Y()]
    b = [*Y()]
    print(*solve(n, k, a, b))

Solution (Python):
import sys
Z = sys.stdin.readline
Y = lambda : map(int, Z().split())

def f(v, k):
    return v if v <= k else v + v - k

def solve(n, k, a, b):
    if 1 not in b:
        return solve(n, k << 1, [i << 1 for i in a], [1] * n)
    if b[-1] != 1:
        i = b.index(1) + 1
        A = solve(n, k, a[i:] + a[:i], b[i:] + b[:i])
        return A[-i:] + A[:-i]
    f1 = [0]
    f2 = [0]
    s = v = 0
    p = n - 1
    d = [0] * n
    for i in range(n):
        if b[i] == 1:
            f1.append(0)
            s += f(v + a[i - 1], k)
            v = 0
            p = i
        else:
            f1.append(f1[-1] + a[i - 1])
            v += a[i - 1]
            d[i] = p
    for i in range(n - 2, -2, -1):
        if b[i] == 1:
            f2.append(0)
            d[i] = f(v + a[i], k)
            v = 0
        else:
            f2.append(f2[-1] + (a[i] << 1))
            v += a[i]
    f1 = f1[1:]
    f2 = f2[-2::-1]
    A = []
    for i in range(n):
        if b[i] == 1:
            A.append(s)
        else:
            A.append(s - d[d[i]] + f(f1[i], k) + f2[i])
    return A
for _ in range(*Y()):
    (n, k) = Y()
    a = [*Y()]
    b = [*Y()]
    print(*solve(n, k, a, b))