Network Coverage
Contest: Educational Codeforces Round 90 (Rated for Div. 2)
Difficulty rating: 2400
Tags: binary search, constructive algorithms, data structures, greedy

The government of Berland decided to improve network coverage in his country. Berland has a unique structure: the capital in the center and $$$n$$$ cities in a circle around the capital. The capital already has a good network coverage (so the government ignores it), but the $$$i$$$-th city contains $$$a_i$$$ households that require a connection.

The government designed a plan to build $$$n$$$ network stations between all pairs of neighboring cities which will maintain connections only for these cities. In other words, the $$$i$$$-th network station will provide service only for the $$$i$$$-th and the $$$(i + 1)$$$-th city (the $$$n$$$-th station is connected to the $$$n$$$-th and the $$$1$$$-st city).

All network stations have capacities: the $$$i$$$-th station can provide the connection to at most $$$b_i$$$ households.

Now the government asks you to check can the designed stations meet the needs of all cities or not — that is, is it possible to assign each household a network station so that each network station $$$i$$$ provides the connection to at most $$$b_i$$$ households.

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 the single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the number of cities and stations.

The second line of each test case contains $$$n$$$ integers ($$$1 \le a_i \le 10^9$$$) — the number of households in the $$$i$$$-th city.

The third line of each test case contains $$$n$$$ integers ($$$1 \le b_i \le 10^9$$$) — the capacities of the designed stations.

It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$10^6$$$.

Output format:
For each test case, print YES, if the designed stations can meet the needs of all cities, or NO otherwise (case insensitive).

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

Note:
In the first test case:

- the first network station can provide $$$2$$$ connections to the first city and $$$1$$$ connection to the second city;
- the second station can provide $$$2$$$ connections to the second city and $$$1$$$ connection to the third city;
- the third station can provide $$$3$$$ connections to the third city.

In the second test case:

- the $$$1$$$-st station can provide $$$2$$$ connections to the $$$1$$$-st city;
- the $$$2$$$-nd station can provide $$$3$$$ connections to the $$$2$$$-nd city;
- the $$$3$$$-rd station can provide $$$3$$$ connections to the $$$3$$$-rd city and $$$1$$$ connection to the $$$1$$$-st station.

In the third test case, the fourth city needs $$$5$$$ connections, but the third and the fourth station has $$$4$$$ connections in total.

Example input:
5
3
2 3 4
3 3 3
3
3 3 3
2 3 4
4
2 3 4 5
3 7 2 2
4
4 5 2 3
2 3 2 7
2
1 1
10 10

Example output:
YES
YES
NO
YES
YES

Editorial:
There are plenty of different solutions to this problem. Here is one that doesn't use Hall's theorem.
Let's look at pair $$$(a_i, b_i)$$$ as fuction $$$f_i(in) = out$$$: how many connections $$$out$$$ will be left for the $$$(i + 1)$$$-th city if we take $$$in$$$ connections from the $$$(i - 1)$$$-th station. This function has the following structure: there is a minimum required $$$in$$$ (let's name it $$$minx_i$$$) to meet the needs of the $$$i$$$-th city and with $$$minx_i$$$ borrowed connections there will be $$$f_i(minx_i) = mink_i$$$ free connections to the $$$(i + 1)$$$-th city. Increasing $$$minx_i$$$ by some $$$x$$$ we can get $$$f_i(minx_i + x) = mink_i + x$$$ free connections, but there is upper bound to number of free connections $$$maxk_i$$$. In other words, the function $$$f_i(minx_i + x) = \min(mink_i + x, maxk_i)$$$ where $$$x \ge 0$$$.
For example, let's calculate the corresponding coefficients for the $$$i$$$-th function:
• if $$$a_i \le b_i$$$ then $$$minx_i = 0$$$, $$$mink_i = b_i - a_i$$$ and $$$maxk_i = b_i$$$;
• if $$$a_i > b_i$$$ then $$$minx_i = a_i - b_i$$$, $$$mink_i = 0$$$ and $$$maxk_i = b_i$$$.
Why did we define such functions? If we can calculate result function $$$f_{[1,n]}(in) = f_n(f_{n - 1}(\dots f_2(f_1(in))\dots))$$$ then we can check the possibility of meeting all needs by checking that this fuction exists and $$$minx_{[1,n]} \le mink_{[1,n]}$$$, i. e. the minimum free $$$mink_{[1,n]}$$$ can be used as borrowed $$$minx_{[1,n]}$$$.
Fortunately, it turns out that the superposition $$$f_{i+1}(f_i(in))$$$ is either don't exists (if, for example, $$$maxk_i < minx_{i + 1}$$$) or it has the same structure as any function $$$f_i$$$. So we can calculate $$$f_{[1,n]}$$$ in one pass and find the answer.
We will skip the detailed formulas to calculate $$$f_{i+1}(f_i(in))$$$: you can either find them by yourself or look at function $$$merge()$$$ in author's solution.
The resulting complexity is $$$O(n)$$$.