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

#DSU0000082. Sắp xếp các chai rượu (Bottle Arrangement)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sắp xếp các chai rượu (Bottle Arrangement)

Bottle Arrangement

Source: Codeforces 2041J – 2024 ICPC Asia Taichung Regional Contest

Version: Phuoc Hung OJ Extended

Problem Statement

A wine display rack contains two parallel rows, each with exactly nn positions numbered from left to right.

The rear row is already filled. Its bottle at position ii has height aia_i. Neither the positions nor the heights of the rear-row bottles may be changed.

You are given nn additional bottles to arrange in the front row, with original heights b1,b2,…,bnb_1,b_2,\ldots,b_n. All bib_i are distinct. You may permute these bottles arbitrarily, but every bottle must be used exactly once and every front-row position must be occupied.

An arrangement of final front-row heights h1,h2,…,hnh_1,h_2,\ldots,h_n is valid only if both conditions hold:

1. The rear row remains visible. At every position ii, the front bottle must be strictly shorter than the corresponding rear bottle:

hi<ai.h_i<a_i.

2. The front row forms a mountain. There must exist a position pp such that the heights are non-decreasing up to pp and non-increasing afterwards:

$$h_1\le h_2\le\cdots\le h_p\ge h_{p+1}\ge\cdots\ge h_n.$$

Equal adjacent heights are allowed, and the peak may occur at either endpoint.

If the original bottle heights do not admit a valid arrangement, you may remove the cap of some front-row bottles. Each bottle has at most one removable cap, and removing it decreases that bottle's height by exactly one. You may either remove the cap or leave it in place for each bottle; no bottle may be shortened more than once. The rear row may not be modified.

For example, a bottle whose original height is 55 can be placed at height 55 or 44, and at no other height. Removing a cap does not remove the bottle itself.

Find the minimum number of caps that must be removed to obtain a valid arrangement. If no valid arrangement exists even after choosing caps freely, output -1.

Input

  • The first line contains a single integer nn, the number of bottles in each row.
  • The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, the fixed heights in the rear row.
  • The third line contains nn distinct integers b1,b2,…,bnb_1,b_2,\ldots,b_n, the original heights of the front-row bottles.

Output

Print one integer: the minimum number of caps to remove, or -1 if no valid arrangement exists.

Subtask

  • Subtask 1 (20 points): 1≤n≤351\le n\le35; 1≤ai,bi≤1091\le a_i,b_i\le10^9; all bib_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000; 1≤ai,bi≤1091\le a_i,b_i\le10^9; all bib_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤5⋅1051\le n\le5\cdot10^5; 1≤ai,bi≤1091\le a_i,b_i\le10^9; all bib_i are pairwise distinct.

Examples

Example 1

Input

5
2 4 6 5 4
1 2 3 4 5

Output

0

Explanation: One valid front-row arrangement has heights [1,3,5,4,2][1,3,5,4,2]. This sequence first increases and then decreases, and every front bottle is strictly shorter than its corresponding rear bottle. No cap needs to be removed.

Example 2

Input

5
6 2 6 6 6
1 2 3 4 5

Output

1

Explanation: The second rear bottle has height 22, forcing the front bottle at that position to have height less than 22. Remove the cap from the bottle originally of height 22, reducing it to 11, and arrange the front row as [1,1,5,4,3][1,1,5,4,3]. This forms a mountain and satisfies every strict rear-height bound. Keeping all caps in place is not sufficient.

Example 3

Input

5
7 2 7 7 7
1 3 4 5 6

Output

-1

Explanation: Only the originally shortest bottle can be made short enough for position 22. The other bottles cannot be arranged to form a mountain with this position constrained to be that low, so no valid arrangement exists.