Codeforces Round #290 (Div. 1) B. Fox And Jumping,

B. Fox And Jumpingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.

There are also*N*Cards, each card has 2 attributes: length*L*_{IAnd costCI. If she paysCIDollars then she can applyI-Th card. After applyingI-Th card she becomes able to make jumps of lengthLI, I. e. from cellXTo cell (XAccept-Encoding-LI) Or cell (XRegion + RegionLI).}

She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells ). for achieving this goal, she wants to buy some cards, paying as little money as possible.

If this is possible, calculate the minimal cost.

Input

The first line contains an integer*N*(1 digit ≤ Digit*N*Segment ≤ limit 300), number of cards.

The second line contains*N*Numbers*L*_{I(1 digit ≤ DigitLILimit ≤ limit 109), the jump lengths of cards.}

The third line contains*N*Numbers*C*_{I(1 digit ≤ DigitCILimit ≤ limit 105), the costs of cards.}

Output

If it is impossible to buy some cards and become able to jump to any cell, output-1. Otherwise output the minimal cost of buying such set of cards.

Sample test (s) Input

3100 99 99001 1 1

Output

2

Input

510 20 30 40 501 1 1 1 1

Output

-1

Input

715015 10010 6006 4290 2730 2310 11 1 1 1 1 1 10

Output

6

Input

84264 4921 6321 6984 2316 8432 6120 10264264 4921 6321 6984 2316 8432 6120 1026

Output

7237

Note

In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. the best way is to buy first and second card, that will make you be able to jump to any cell.

In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you shoshould output-1.

After observation, we can find that if there are two cards a and B, we can jump to gcd (a, B) every time)

Gcd = 1

So the problem is converted to get the k number, and their gcd = 1 is the minimum cost.

Dp [I] indicates the minimum cost of I.

The data is a little big. Just use map to transfer the data.

/*************************************** * *********************************> File Name: cf289b. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: tuesday, February 03, 2015 ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstrin G >#include <iostream >#include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; map <int, int> dp; const int N = 330; int l [N], c [N]; int gcd (int a, int B) {return B = 0? A: gcd (B, a % B);} int main () {int n; while (~ Scanf ("% d", & n) {map <int, int >:: iterator it; for (int I = 1; I <= n; ++ I) {scanf ("% d", & l [I]) ;}for (int I = 1; I <= n; ++ I) {scanf ("% d ", & c [I]);} dp. clear (); dp [0] = 0; for (int I = 1; I <= n; ++ I) {for (it = dp. begin (); it! = Dp. end (); ++ it) {int t = gcd (l [I], it-> first); if (dp. count (t) {dp [t] = min (dp [t], it-> second + c [I]);} else {dp [t] = it-> second + c [I] ;}} if (! Dp. count (1) {printf ("-1 \ n") ;}else {printf ("% d \ n", dp [1]) ;}} return 0 ;}