B - Uniform Sum Editorial /

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

数列 A=(A1,,AN)A=(A_1,\dots,A_N), B=(B1,,BN)B=(B_1,\dots,B_N) があります。これらに対して、以下の 33 種類の操作を、任意の順番で任意の回数だけ行うことができます。

  • Ai=1A_i = -1 を満たす ii を選び、AiA_i を任意の非負整数に置き換える。
  • Bi=1B_i = -1 を満たす ii を選び、BiB_i を任意の非負整数に置き換える。
  • 数列 AA の要素を任意の順番に並び替える。

操作の結果、AA, BB の全ての要素が非負であり、なおかつ A1+B1=A2+B2==AN+BNA_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N となるようにできるかを判定してください。

制約

  • 2N20002 \leq N \leq 2000
  • 1Ai109-1 \leq A_i \leq 10^9
  • 1Bi109-1 \leq B_i \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 \ldots ANA_N
B1B_1 B2B_2 \ldots BNB_N

出力

操作の結果、AA, BB の全ての要素が非負かつ A1+B1=A2+B2==AN+BNA_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N となるようにできる場合は Yes、そうでない場合は No を出力せよ。


入力例 1Copy

Copy
4
2 0 -1 3
3 -1 4 2

出力例 1Copy

Copy
Yes

以下の操作を行うことを考えます。

  • A3A_311 に置き換える。
  • B2B_211 に置き換える。
  • AA を並び替えて (1,3,0,2)(1,3,0,2) にする。

操作の結果、A=(1,3,0,2)A = (1,3,0,2), B=(3,1,4,2)B = (3,1,4,2) となり、AA, BB の全ての要素が非負かつ A1+B1=A2+B2=A3+B3=A4+B4=4A_1+B_1 = A_2+B_2 = A_3+B_3 = A_4+B_4 = 4 が満たされます。


入力例 2Copy

Copy
3
1 2 3
1 2 4

出力例 2Copy

Copy
No

どのように操作を行っても、A1+B1=A2+B2=A3+B3A_1+B_1 = A_2+B_2 = A_3+B_3 を満たすようにはできません。


入力例 3Copy

Copy
3
1 2 -1
1 2 4

出力例 3Copy

Copy
No

Score : 500500 points

Problem Statement

There are two sequences A=(A1,,AN)A=(A_1,\dots,A_N) and B=(B1,,BN)B=(B_1,\dots,B_N). You can perform the following three types of operations any number of times in any order:

  • Choose an index ii such that Ai=1A_i = -1, and replace AiA_i with any non-negative integer.
  • Choose an index ii such that Bi=1B_i = -1, and replace BiB_i with any non-negative integer.
  • Rearrange the elements of sequence AA in any order.

Determine whether it is possible, after these operations, for all elements of AA and BB to be non-negative and satisfy A1+B1=A2+B2==AN+BNA_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N.

Constraints

  • 2N20002 \leq N \leq 2000
  • 1Ai109-1 \leq A_i \leq 10^9
  • 1Bi109-1 \leq B_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \ldots ANA_N
B1B_1 B2B_2 \ldots BNB_N

Output

If it is possible, after the operations, for all elements of AA and BB to be non-negative and satisfy A1+B1=A2+B2==AN+BNA_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N, print Yes. Otherwise, print No.


Sample Input 1Copy

Copy
4
2 0 -1 3
3 -1 4 2

Sample Output 1Copy

Copy
Yes

Consider the following operations:

  • Replace A3A_3 with 11.
  • Replace B2B_2 with 11.
  • Rearrange AA to (1,3,0,2)(1,3,0,2).

After these operations, A=(1,3,0,2)A = (1,3,0,2) and B=(3,1,4,2)B = (3,1,4,2): all elements of AA and BB are non-negative, and A1+B1=A2+B2=A3+B3=A4+B4=4A_1+B_1 = A_2+B_2 = A_3+B_3 = A_4+B_4 = 4 is satisfied.


Sample Input 2Copy

Copy
3
1 2 3
1 2 4

Sample Output 2Copy

Copy
No

No matter how you perform the operations, it is impossible to satisfy A1+B1=A2+B2=A3+B3A_1+B_1 = A_2+B_2 = A_3+B_3.


Sample Input 3Copy

Copy
3
1 2 -1
1 2 4

Sample Output 3Copy

Copy
No


2025-04-01 (Tue)
16:05:44 +00:00