gadfadar3

Time limit: 2s Memory limit: 256MB Input: gadfadar3.in Output: gadfadar3.out

When you can guess the solution, it's a shame to think at it.

After helping Don identify the intruders, he has put his trust in you. Therefore, to prevent the appearance of other intruders, Don has given you the number NN and two sequences a1,a2,aNa_1, a_2, \cdots a_N and b1,b2,bNb_1, b_2, \cdots b_N of natural numbers.

We denote the absolute value of xx by x|x|.

For a sequence x1,x2,xkx_1, x_2, \cdots x_k of integers, we define f(x)=f(x) = the minimum value of the sum x1v+x2v+...+xkv|x_1 - v| + |x_2 - v| + ... + |x_k - v| for an integer vv.

You can form any sequence c1,c2,cNc_1, c_2, \cdots c_N of natural numbers with the property that ci=aic_i = a_i or ci=bic_i = b_i for each ii from 11 to NN.

Task

To keep your job as an advisor to the organization, you need to find the minimum value of f(c)f(c) for all the sequences cc that you can form.

Input data

The first line of the input file gadfadar3.in contains a non-zero natural number NN. The second line contains NN natural numbers, separated by spaces, representing the sequence a1,a2aNa_1, a_2 \cdots a_N. The third line contains NN natural numbers, separated by spaces, representing the sequence b1,b2,bNb_1, b_2, \cdots b_N.

Output data

The first line of the output file gadfadar3.out will contain a single integer, representing the minimum value of f(c)f(c) among all the sequences cc that you can form.

Constraints and clarifications

  • 1N300 0001 \leq N \leq 300\ 000
  • 0ai,bi1090 \leq a_i, b_i \leq 10^9
# Score Constraints
1 5 1N121 \leq N \leq 12 and 0ai,bi1 0000 \leq a_i, b_i \leq 1\ 000
2 9 1N121 \leq N \leq 12
3 11 12<N2 00012 < N \leq 2\ 000 and 0ai,bi1 0000 \leq a_i, b_i \leq 1\ 000
4 17 12<N2 00012 < N \leq 2\ 000
5 22 200 000<N300 000200\ 000 < N \leq 300\ 000 and 0ai,bi100 0000 \leq a_i, b_i \leq 100\ 000
6 36 No other constraints

Example 1

gadfadar3.in

5
2 6 10 2 5
3 9 3 1 2 

gadfadar3.out

5

Explanation

We can form the sequence c=[2,6,3,2,2]c = [2, 6, 3, 2, 2], and f(c)=5f(c) = 5 if we choose v=2v = 2.

Example 2

gadfadar3.in

7
1 2 5 0 9 30 7
11 30 30 5 8 0 7

gadfadar3.out

17

Explanation

We can form the sequence c=[1,2,5,5,8,0,7]c = [1, 2, 5, 5, 8, 0, 7], and f(c)=17f(c) = 17 if we choose v=5v = 5.

Log in or sign up to be able to send submissions!