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

#GD0000178. Đặt quân trên bàn cờ (Chips on the Board)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đặt quân trên bàn cờ (Chips on the Board)

Chips on the Board

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an n×nn\times n board and two arrays of positive integers a1,…,ana_1,\ldots,a_n and b1,…,bnb_1,\ldots,b_n. You may place chips into distinct cells. Placing a chip in row ii, column jj costs ai+bja_i+b_j.

For every cell (i,j)(i,j), there must be at least one chip in row ii or in column jj. A chip directly at (i,j)(i,j) also counts. Not every cell needs to contain a chip.

Find the minimum total cost of a placement satisfying the condition.

Input

Line 1 contains integer nn, the number of rows and columns.

Line 2 contains exactly nn integers a1,…,ana_1,\ldots,a_n, the row cost components.

Line 3 contains exactly nn integers b1,…,bnb_1,\ldots,b_n, the column cost components.

Output

Print one integer: the minimum total cost of any valid chip placement.

Subtasks

  • Subtask 1 (20 points): 1≤n≤61\le n\le6; 1≤ai,bi≤1091\le a_i,b_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤ai,bi≤1091\le a_i,b_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤3⋅1051\le n\le3\cdot10^5; 1≤ai,bi≤1091\le a_i,b_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

3
1 4 1
3 2 2

Output

10

Explanation

There are 33 rows and columns. Place chips at (1,1)(1,1), (1,2)(1,2) and (1,3)(1,3). Their costs are 1+3=41+3=4, 1+2=31+2=3, and 1+2=31+2=3, totaling 1010. Every column then has a chip, so all cells are covered. Placing one chip in each row in the cheapest column would cost (1+4+1)+3⋅2=12(1+4+1)+3\cdot2=12. The minimum is therefore 1010.