Under construction

Still being built. Things may change

M. S. I. S.

Time limit0.5sMemory limit2048 MB

Summary
Given a 2 by n matrix, reorder its columns to maximize the sum of the best increasing subsequence sums in the two rows.
Level

Hard9 of 10

Topics
Dynamic programming, Graph, Sorting
Solved
No attempts yet

Problem

We are given a 2×n2 \times n matrix MM of positive integers. No row of MM contains a duplicate number. For the ii-th row rir_i (i=1,2i = 1, 2), let sis_i be the maximum sum of an increasing subsequence of rir_i. For example, in the matrix below, s1s_1 is 1+2+3+4+5+61 + 2 + 3 + 4 + 5 + 6 and s2s_2 is 2+3+52 + 3 + 5.

[123456623541]\begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} & \color{red}{5} & \color{red}{6}\\ 6 & \color{red}{2} & \color{red}{3} & \color{red}{5} & 4 & 1 \end{bmatrix}

We call s1+s2s_1 + s_2 the maximum sum of increasing subsequences (MSIS). Permuting the columns of MM can change the MSIS. In the matrix below, the columns [c1,c2,c3,c4,c5,c6][c_1, c_2, c_3, c_4, c_5, c_6] are rearranged to [c2,c3,c4,c5,c6,c1][c_2, c_3, c_4, c_5, c_6, c_1], and the MSIS becomes 36.

[234561235416]\begin{bmatrix} \color{red}{2} & \color{red}{3} & \color{red}{4} & \color{red}{5} & \color{red}{6} & 1\\ \color{red}{2} & \color{red}{3} & \color{red}{5} & 4 & 1 & \color{red}{6} \end{bmatrix}

Given MM, find the maximum MSIS over all permutations of its columns.

Input

The first line contains an integer nn (1≤n≤100001 \le n \le 10000), the number of columns of MM. The next two lines each contain nn integers, the entries of row 1 and row 2. All integers are between 1 and 50000, and no row contains a duplicate number.

Output

Print one line containing the maximum MSIS over all permutations of the columns of MM.

Examples2

  1. Example 1

    Input
    6
    1 2 3 4 5 6
    6 2 3 5 4 1
    
    Expected output
    36
    
  2. Example 2

    Input
    5
    50 40 3 2 1
    1 2 3 100 200
    
    Expected output
    396