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

#GL0000129. ƯCLN lớn nhất và tổng lớn nhất (Maximum Gcd and Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

ƯCLN lớn nhất và tổng lớn nhất (Maximum Gcd and Sum)

Maximum Gcd and Sum

Source: HackerRank

Version: Phuoc Hung OJ Extended

Problem Statement

Given two arrays AA and BB, each of length nn, select xx from AA and yy from BB. First maximize gcd⁡(x,y)\gcd(x,y) among all possible pairs. Among pairs with this maximum GCD, maximize x+yx+y. Output the resulting sum.

Input

The first line contains nn. The second contains AA; the third contains BB, each with nn numbers.

Output

Print the sum x+yx+y satisfying the two priority criteria.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤Ai,Bi≤1001\le A_i,B_i\le 100.
  • Subtask 2 (30 points): 1≤n≤5 0001\le n\le 5\,000; 1≤Ai,Bi≤10 0001\le A_i,B_i\le 10\,000.
  • Subtask 3 (50 points): 1≤n≤500 0001\le n\le 500\,000; 1≤Ai,Bi≤1 000 0001\le A_i,B_i\le 1\,000\,000.

Examples

Input

3
6 10 15
9 12 20

Output

30

Explanation

The pair (10,20) has GCD 10 and sum 30. Every other cross-array pair has a smaller GCD, so the answer is 30.