Producing Snow
Contest: VK Cup 2018 - Round 1
Difficulty rating: 1600
Tags: binary search, data structures

Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.

Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.

Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.

You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.

Input format:
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.

The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.

The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.

Output format:
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.

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

Note:
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.

Example input:
3
10 10 5
5 7 2

Example output:
5 12 4

Example input:
5
30 25 20 15 10
9 10 12 4 13

Example output:
9 20 35 11 25

Editorial:
We can directly simulate the process, but it takes $${\mathcal{O}}(N^{2})$$ time, which is too slow. There are multiple approaches how to make this simulation faster. We present two of them.
1. In the first solution, instead of calculating the total volume of the snow melted, we first calculate two quantities: F[i] — the number of piles left after day i, and M[i] — the total volume of piles that disappear on day i. The answer will then be F[i] * T[i] + M[i].Calculate prefix sums of the temperatures. This way, when a snow pile is formed on day i, we can use binary search to determine on which day it will disappear completely. Denote this day by j and put j = N + 1 if the pile survives. We can note that on every day k between i and j - 1 inclusive, this pile will lose T[k] of its volume, which corresponds to increasing F[k] by one. Furthermore, we add the remaining volume to M[j].To calculate all F[i]'s fast, we can again use prefix sums — adding 1 to interval can then be done by two additions.
2. The second solution can handle queries online. For each pile, we calculate how big it would be if it was created on the first day: $$V'[i] = V[i] + \sum_{j=1}^{i-1} T[j]$$. We maintain all existing piles in a multiset. When a day i starts, we add V'[i] into the multiset. Then we remove all piles with $$V'[k] \leq \sum_{j=1}^{i} T[j]$$ — those are the piles that disappear on day i — and easily calculate the total volume of melted snow in them. All the piles left in the multiset contribute exactly $$T^{[i]}\cdot\text{size of multiset}$$.As the multiset is sorted, and each pile is added and removed only once, the total complexity is $${ \mathcal { O } } ( N \log N )$$.