Fox And Jumping

CodeForces - 510D

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 applying i-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.

Examples

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

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 should output -1.

sol:首先容易发现题目就是让我们凑出一个1来,但是直接凑感觉很 蛋疼

然后有一个引理就是若干个数a,b,c...能凑出的最小数字就是gcd(a,b,c...),证明就不用了,自己XJByy一下就可以了,其实很容易证明,这样就可以轻松dp辣

Ps:STL真好用

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
struct Kapian
{
int Len,Cost;
}Card[N];
map<int,int>dp;
inline int gcd(int a,int b)
{
return (!b)?a:(gcd(b,a%b));
}
int main()
{
int i;
map<int,int>::iterator it;
R(n);
for(i=;i<=n;i++) R(Card[i].Len);
for(i=;i<=n;i++) R(Card[i].Cost);
dp.clear();
dp[]=;
for(i=;i<=n;i++)
{
for(it=dp.begin();it!=dp.end();it++)
{
int oo=it->first;
int tmp=gcd(Card[i].Len,oo),CC=Card[i].Cost+it->second;
if(dp[tmp]&&dp[tmp]<CC) continue;
dp[tmp]=CC;
}
}
if(!dp[]) puts("-1");
else Wl(dp[]);
return ;
}
/*
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
*/

codeforces510D的更多相关文章

  1. CodeForces-510D

    https://vjudge.net/problem/CodeForces-510D题目可以转化为花最小代价选一些数,然后这些数可以经过加减运算得到1或-1,不然1你就凑不出来,一旦凑出来1,其他的都 ...

随机推荐

  1. 【大数据安全】基于Kerberos的大数据安全验证方案

    1.背景 互联网从来就不是一个安全的地方.很多时候我们过分依赖防火墙来解决安全的问题,不幸的是,防火墙是假设"坏人"是来自外部的,而真正具有破坏性的攻击事件都是往往都是来自于内部的 ...

  2. 利用 Blob 处理 node 层返回的二进制文件流字符串并下载文件

    博客地址:https://ainyi.com/65 解释 | 背景 看到标题有点懵逼,哈哈,实际上是后端将文件处理成二进制流,返回到前端,前端处理这个二进制字符串,输出文件或下载 最近公司有个需求是用 ...

  3. 创建索引CreateIndex

    nuget引用NEST new一个客户端 源码可查ElasticClient.cs new一个ElasticClient有多种方式 第一种 ES地址是http://localhost:9200,可以直 ...

  4. WPF Geometry 添加Path数据

    当图片转svg,svg转Xaml后,根据数据加载显示图片 DrawingImage: <DrawingImage x:Key="Image.Search"> <D ...

  5. InnoSetup 以命令行自动编译打包

    本章介绍,在新建Innosetup的编译脚本iss文件后,在不运行Innosetup软件的基础上,以bat自动编译运行打包. 一.添加Innosetup软件包 下载InnoSetup软件包并解压到bu ...

  6. Excel的读取和保存(POI)

    示例 Excel文件: 数据读取: 保存路径: Jar包准备 下载地址: 链接:https://pan.baidu.com/s/1RZAwEsFwjKMlnYYGwHMfaA 提取码:h9mj 文件上 ...

  7. jquery之冒泡事件介绍以及阻止冒泡

    什么是事件冒泡 <div style="width: 200px;height: 200px;background: red;margin: 200px auto;" onc ...

  8. Java中单例模式的几种实现

    目录 懒汉式单例 简单版本 synchronized版本 双重检查(Double-Check)版本 volatile 饿汉式单例 实现1 其他实现方式 静态内部类-Effective Java 枚举- ...

  9. WordCount项目基本功能

    一.项目源代码地址 本人Gitee项目地址:https://gitee.com/yuliu10/WordCount 二.PSP表格 psp阶段 预估耗时 (分钟) 实际耗时 (分钟) 计划 30 10 ...

  10. 导入虚拟机vmware,此主机支持Intel VT-x,但Intel VT-x处于禁用状态和黑屏

    解决方法:进入BIOS(按什么键进入bios,需要看你用什么电脑),把Intel Virtualization Technology         设置enabled 然后是黑屏解决方法:管理员模式 ...