Apple Exchange
Limits: 2 sec., 256 MiB
There are \(n\) people numbered from 1 to \(n\). They exchanged apples according to the following rules:
each person could give each other person at most one apple;
in addition, each person could keep at most one apple; such an apple counts as both given and received by that person.
It is known that person number \(i\) gave away exactly \(a_i\) apples and received exactly \(b_i\) apples. Determine whether such an exchange could have happened.
Input
The first line contains one integer \(n\) – the number of people.
The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) – how many apples each person gave away.
The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) – how many apples each person received.
Output
In the only line, print Yes if such an exchange could
have happened, or No otherwise.
Constraints
\(1 \le n \le 10^5\),
\(0 \le a_i, b_i \le n\).
The scoring consists of the following blocks:
1 point for each example from the statement,
15 points: \(n \le 4\),
20 points: all \(a_i \le 1\),
25 points: \(n \le 2000\),
38 points: no additional constraints.
You get the points for a block only if your program passes all tests of the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 2 2 1 1 2 2 | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 2 0 2 0 | No |
Notes
In the first example, the exchange could go like this: person 1 gave an apple to each of persons 2 and 3, person 2 kept one apple and gave one to person 3, and person 3 gave an apple to person 1. Then the people received 1, 2 and 2 apples, respectively.
In the second example, person 1 gave away 2 apples but can keep only one, so the second one must have been received by person 2. However, person 2 received no apples.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|