D - Fox And Jumping

                      Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 li and cost ci. If she pays ci dollars then she can apply i-th card. After applyingi-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).

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 ≤ n ≤ 300), number of cards.

The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.

The third line contains n numbers ci (1 ≤ ci ≤ 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 Input

Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237

区间dp
 //2016.8.6
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm> using namespace std; int l[], c[];
map<int, int>dp1, dp2; int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int main()
{
int n;
while(cin>>n)
{
dp1.clear();
for(int i = ; i <= n; i++)
scanf("%d", &l[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
dp1[] = ;
for(int i = ; i <= n; i++)
{
map<int, int>::iterator it;
for(it = dp1.begin(); it != dp1.end(); it++)
{
int g = gcd(it->first, l[i]);
if(dp1.count(g))
dp1[g] = min(it->second+c[i], dp1[g]);
else dp1[g] = it->second+c[i];
}
}
if(dp1.count())cout<<dp1[]<<endl;
else cout<<-<<endl;
} return ;
}

CodeForces 512B(区间dp)的更多相关文章

  1. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. Timetable CodeForces - 946D (区间dp)

    大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间. 每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天 ...

  3. Codeforces 1114D(区间DP)

    题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...

  4. CodeForces - 1107E 区间DP

    和紫书上的Blocks UVA - 10559几乎是同一道题,只不过是得分计算不同 不过看了半天紫书上的题才会的,当时理解不够深刻啊 不过这是一道很好区间DP题 细节看代码 #include<c ...

  5. CodeForces 149D 区间DP Coloring Brackets

    染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...

  6. Zuma CodeForces - 607B (区间DP)

    大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完. #include <iostream> #include <cstdio> #define REP(i,a,n ...

  7. Recovering BST CodeForces - 1025D (区间dp, gcd)

    大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...

  8. Codeforces 940 区间DP单调队列优化

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  9. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

随机推荐

  1. 去掉谷歌input记住账号或密码时默认出现的黄色背景

    在谷歌浏览器会默认记住账号,而记住账号之后其input的背景会变成黄色,解决的办法如下: 方法一:直接用css的内阴影来覆盖黄色,代码如下: input:-webkit-autofill { -web ...

  2. ios开发证书,描述文件,bundle ID的关系

    苹果为了控制应用的开发与发布流程,制定了一套非常复杂的机制.这里面的关键词有:个人开发者账号,企业开发者账号,bundle ID,开发证书,发布证书(又叫"生产证书"),开发描述文 ...

  3. 打印等腰三角形javascript

    <html> <head> <script type="text/javascript"> var n=4;//层数 for(var i=1; ...

  4. 如何高效的用判断用js判断ie6

    用js判断ie6的方法有很多,如: 1. var isIE=!!window.ActiveXObject; var isIE6=isIE&&!window.XMLHttpRequest ...

  5. iOS之UILabel自适应大小

    //初始化一个label self.label=[[UILabel alloc] init]; //设置自动行数与字符换行 [self.label setNumberOfLines:0]; //给la ...

  6. 路过Haxe

    刚才在看Nape的时候,看到Haxe的代码,意外的感觉到亲切. 因为之前写过as2代码,最近学习了python,所以对haxe看起来很亲切,于是路过一下写了个HelloWorld. 另外,估计很长时间 ...

  7. php中使用mysql_fetch_object向页面输出结果,总结!

    public function selectResultByThird() { if ((!empty ($_REQUEST["bigname"])) && (!e ...

  8. CodeForces 622D Optimal Number Permutation

    是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就 ...

  9. mysql连接字符集default

    用mysql客户端工具输入中文数据报错,或乱码问题解决 数据库字符集为latin1时 用mysql工具,然后选择连接>l连接属性>高级>字符集选择default

  10. LPC1768定时器普通定时

    //其他通道的基本定时功能都能在这里实现 void Time0Mr0Init(u32 arr,u32 psc) { LPC_SC->PCONP |= (1<<1);          ...