JOI '19 Open P2 - Remittance

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem type

There exist N houses around Beaver Lake in JOI Kingdom, numbered from 1 to N counterclockwisely.

Each house can send money to the left adjoint house in a view from the lake (for the House i (1 \le i \le N-1), it is the House i+1 and for the House N, it is the House 1.) by using the remittance service. However, it costs money which is the same amount as the sent money. Money must be sent in units of 1 yen. When you send money, you must pay the fee, so the sum of sent money and fee cannot exceed the amount of money in the house.

Currently, the House i (1 \le i \le N) has A_i yen. On the other hand, it is desired in the point of view of tax measures that the amount of money in the House i is equal to B_i yen. By using the remittance service, you want to make the amount of money in the House i equal to B_i yen. You cannot spend money on the other thing than fee or send money by any other way than the remittance service.

Write a program which, given the current amounts of money and the desired amounts of money for all houses, decide if you can make the amounts of money equal to the desired amounts of money for all houses by using the remittance service.

Input Specification

Read the following data from the standard input.

N

A_1\ B_1

\vdots

A_N\ B_N

Output Specification

Output Yes if you can make the amounts of money equal to the desired amounts of money for all houses by using the remittance service, No if it is impossible.

Constraints

  • 2 \le N \le 1\,000\,000.
  • 0 \le A_i \le 1\,000\,000\,000 (1 \le i \le N).
  • 0 \le B_i \le 1\,000\,000\,000 (1 \le i \le N).

Subtasks

  1. (15 points) N \le 7, A_i \le 5, B_i \le 5 (1 \le i \le N).
  2. (40 points) N \le 20.
  3. (45 points) No additional constraints.

Sample Input 1

5
0 0
1 0
2 3
3 3
4 0

Sample Output 1

Yes

Explanation for Sample 1

For example, by using the remittance service in the following way, you can make the amounts of money equal to the desired amounts of money for all houses.

  1. 2 yen is sent from the House 5 to the House 1. It costs 2 yen.
  2. 1 yen is sent from the House 1 to the House 2. It costs 1 yen.
  3. 1 yen is sent from the House 2 to the House 3. It costs 1 yen.

Sample Input 2

5
0 0
1 2
2 4
3 2
4 0

Sample Output 2

No

Explanation for Sample 2

You cannot make the amounts of money equal to the desired amounts of money for all houses by using remittance service in any way.

Sample Input 3

2
1 1
2 1

Sample Output 3

No

Explanation for Sample 3

Note that money must be sent in units of 1 yen.

Sample Input 4

2
1 1
2 2

Sample Output 4

Yes

Explanation for Sample 4

You do not need to use the remittance service.


Comments

There are no comments at the moment.