PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000038. Sắp xếp bằng phép đổi chỗ có điều kiện (pSort)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sắp xếp bằng phép đổi chỗ có điều kiện (pSort)

pSort

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn positions numbered from 11 to nn. Initially, position ii contains value ii, so the starting array is 1,2,…,n1,2,\ldots,n. Every position ii has a fixed preferred distance did_i.

In one operation, choose positions i,ji,j and swap the values currently stored in them, provided ∣i−j∣=di|i-j|=d_i. This condition depends on the position indices and the fixed distance did_i, not on the values currently stored there. Operations may be repeated in any order.

Given a target permutation pp, determine whether the initial array can be transformed into exactly p1,p2,…,pnp_1,p_2,\ldots,p_n using only allowed swaps.

Input

The first line contains nn. The second contains the permutation p1,…,pnp_1,\ldots,p_n. The third contains d1,…,dnd_1,\ldots,d_n.

Output

Print YES if reachable, otherwise NO.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; pp is a permutation of 1,2,…,n1,2,\ldots,n; 1≤di≤n1\le d_i\le n.
  • Subtask 2 (30 points): 1≤n≤401\le n\le40; pp is a permutation of 1,2,…,n1,2,\ldots,n; 1≤di≤n1\le d_i\le n.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le100; pp is a permutation of 1,2,…,n1,2,\ldots,n; 1≤di≤n1\le d_i\le n.

Examples

Input

5
5 4 3 2 1
1 1 1 1 1

Output

YES

Explanation

Since every did_i equals 11, the values at any two adjacent positions can be swapped. For example, value 55 can be moved successively left from position 55 to position 11, and the other values can be arranged similarly. Hence the target permutation is reachable, so print YES.