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

Source: Internet
Author: User

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 alsoNCards, each card has 2 attributes: lengthLIAnd 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 integerN(1 digit ≤ DigitNSegment ≤ limit 300), number of cards.

The second line containsNNumbersLI(1 digit ≤ DigitLILimit ≤ limit 109), the jump lengths of cards.

The third line containsNNumbersCI(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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.