Traveling in Berland
Contest: Educational Codeforces Round 145 (Rated for Div. 2)
Difficulty rating: 2500
Tags: binary search, data structures, graphs, greedy, implementation

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 format:
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 format:
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.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
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

Example output:
17 19 17 
13 12 12 12 14 
14 14 14 14 
8 8 8

Editorial:
The problem has a rather obvious naive solution in $$$O(n)$$$ for each starting city, but it's too slow. So we have to speed up this solution somehow. Binary lifting is one of the options, but here we have a problem that it is difficult to connect two consecutive groups of steps, because after the first group there is a certain amount of fuel left. Therefore, one of the solutions is to switch to such steps that $$$0$$$ liters of fuel remains after it.
Let's consider one of such "greedy" steps. Suppose we are in the city $$$i$$$ with $$$0$$$ fuel, then the following situations are possible:
• $$$b_i=2$$$, let's buy exactly $$$a_i$$$ liters of fuel to reach the next city, then the step length is $$$1$$$ and the cost is $$$2a_i$$$;
• $$$b_i=1$$$ and $$$cnt=0$$$ (where $$$cnt$$$ is the maximum number such that $$$b_{i+1}=2$$$, $$$b_{i+2}=2$$$, ..., $$$b_{i+cnt}=2$$$, i.e. the number of consecutive cities with the cost $$$2$$$), let's buy exactly $$$a_i$$$ liters of fuel to reach the next city, then the step length is $$$1$$$ and the cost is $$$a_i$$$;
• $$$b_i=1$$$ and $$$cnt>0$$$, let's find a minimum $$$j$$$ such that $$$sum = a_i + a_{i+1} + \dots + a_j \ge k$$$ and $$$j \le i + cnt$$$ (i.e. such $$$j$$$ that you can reach it by spending all $$$k$$$ of liters):   $$$sum \le k$$$, let's buy exactly $$$sum$$$ liters with the cost $$$1$$$ in the city $$$i$$$, then the step length is $$$j-i+1$$$ and the cost is $$$sum$$$;  $$$sum > k$$$, let's buy $$$k$$$ liters with the cost $$$1$$$ in the city $$$i$$$, and the remainder of $$$sum - k$$$ liters with the cost $$$2$$$ in the city $$$j$$$, then the step length is $$$j-i+1$$$ and the cost is $$$k + 2(sum-k)$$$.
• $$$sum \le k$$$, let's buy exactly $$$sum$$$ liters with the cost $$$1$$$ in the city $$$i$$$, then the step length is $$$j-i+1$$$ and the cost is $$$sum$$$;
• $$$sum > k$$$, let's buy $$$k$$$ liters with the cost $$$1$$$ in the city $$$i$$$, and the remainder of $$$sum - k$$$ liters with the cost $$$2$$$ in the city $$$j$$$, then the step length is $$$j-i+1$$$ and the cost is $$$k + 2(sum-k)$$$.
Now using these types of steps, we maintain an important invariant — after each step, the amount of fuel is $$$0$$$. So we can easily calculate the total distance and cost for several consecutive steps. Which leads us to a solution using binary lifting: for each city $$$i$$$ calculate the length and cost of the path with $$$2^{pw}$$$ (for all $$$pw$$$ up to $$$\log{n}$$$) greedy steps. And then, using this data, we can calculate the answer for each starting city in $$$O(\log{n})$$$.