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

#TP0000045. Tích vô hướng nhỏ nhất (Minimum Scalar Product)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tích vô hướng nhỏ nhất (Minimum Scalar Product)

Minimum Scalar Product

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given two integer vectors A=(a1,…,an)A=(a_1,\ldots,a_n) and B=(b1,…,bn)B=(b_1,\ldots,b_n), you may independently permute the coordinates of each vector. Their scalar product is a1b1+⋯+anbna_1b_1+\cdots+a_nb_n. Find the minimum possible scalar product. The PHOJ version processes one vector pair and prints only the number, without the multiple-case Case #...: prefix.

Input

  • The first line contains nn, the dimension of each vector.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n.
  • The third line contains nn integers b1,…,bnb_1,\ldots,b_n.

Output

Print one integer: the minimum possible scalar product after permutations.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; −100000≤ai,bi≤100000-100000\le a_i,b_i\le100000 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤1001\le n\le100; −100000≤ai,bi≤100000-100000\le a_i,b_i\le100000 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤8001\le n\le800; −100000≤ai,bi≤100000-100000\le a_i,b_i\le100000 for all 1≤i≤n1\le i\le n.

Examples

Input

3
1 3 -5
-2 4 1

Output

-25

Explanation

Sort the first vector ascending to [−5,1,3][-5,1,3] and the second descending to [4,1,−2][4,1,-2]. Their scalar product becomes (−5)⋅4+1⋅1+3⋅(−2)=−20+1−6=−25(-5)\cdot4+1\cdot1+3\cdot(-2)=-20+1-6=-25, the minimum possible.