#P11393. Money Transfer

    ID: 13470 Type: Default 1000ms 256MiB

Money Transfer

Money Transfer

Problem Statement

In the Kingdom of JOI, there are N houses arranged around Beaver Lake and numbered 1 through N in counter‐clockwise order. From any house, you can send money to its immediate left neighbor. Specifically, for house i (1 ≤ i ≤ N−1) the left neighbor is house i+1, and for house N the left neighbor is house 1.

When a house sends a remittance, the transfer amount must be an integer, and a fee equal to the transfer amount is charged. In other words, if a house sends y units, it must pay a total of 2y units. At the moment of transfer, the sending house must have at least 2y units available. (Note that the fee is paid out of the house’s money and is not received by the destination house.)

You are given that initially house i has Ai units, and the desired amount is Bi units. Using only these transfers, determine whether it is possible to achieve the desired amounts in all houses. No other spending or depositing of money is allowed.

Modeling the Process

When a house i sends yi units (where 0 ≤ yi is an integer) to its left neighbor, its money decreases by 2yi, and the left neighbor (house i+1, with house N+1 interpreted as house 1) receives yi units. Therefore, after all transfers are complete, the money in house i becomes:

Bi = Ai - 2yi + yi-1

where indices are taken modulo N (with y0 = yN). Equivalently, the system of equations can be written as:

yi-1 - 2yi = Bi - Ai, for i = 1,2,…,N (with y0 = yN).

It can be shown that a necessary and sufficient condition for a solution in nonnegative integers y1, …, yN is that

\[ y_1 = \frac{\sum_{j=1}^{N}2^{N-j}(A_j - B_j)}{2^N-1} \]

is a nonnegative integer and, when computing subsequent values by

\[ y_{i+1} = \frac{y_i - (B_{i+1}-A_{i+1})}{2} \quad (1 \le i \le N-1), \]

each computed yi is a nonnegative integer and the cyclic condition

\[ y_N - 2y_1 = B_1-A_1 \]

holds. (For the case N = 1, the equation simply gives y1 = A1 - B1 and a solution is possible if and only if A1 ≥ B1.)

Input and Output

Your task is to determine whether it is possible to achieve the desired distribution B by a sequence of valid transfers.

inputFormat

The input is given in the following format:

N
A1 A2 ... AN
B1 B2 ... BN

Here, 1 ≤ N and all Ai and Bi are nonnegative integers.

outputFormat

Output a single line containing YES if it is possible to achieve the desired distribution, and NO otherwise.

sample

1
10
5
YES