CF510D.Fox And Jumping
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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__i and cost c__i. If she pays c__i dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length l__i, i. e. from cell x to cell (x - l__i) or cell (x + l__i).
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.
小狐狸 Ciel 正在玩一个游戏。游戏中有一条无限长的纸带,纸带上的格子用整数(正数、负数和零)编号。游戏开始时,她位于编号为 0 的格子上。
此外还有 n 张卡片,每张卡片有两个属性:长度 li 和费用 ci。若她支付 ci 美元,则可以使用第 i 张卡片。使用第 i 张卡片后,她便能进行长度为 li 的跳跃,即可以从格子 x 跳到格子 (x−li) 或格子 (x+li)。
她希望能够在纸带上跳到任意一个格子(途中可能经过一些中间格子)。为达成这一目标,她打算购买若干张卡片,并希望所付总金额尽可能少。
如果该目标可以实现,请计算所需的最小费用。
输入格式
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers l__i (1 ≤ l__i ≤ 109), the jump lengths of cards.
The third line contains n numbers c__i (1 ≤ c__i ≤ 105), the costs of cards.
第一行包含一个整数 n(1≤n≤300),表示卡片的数量。
第二行包含 n 个整数 li(1≤li≤109),表示各卡片的跳跃长度。
第三行包含 n 个整数 ci(1≤ci≤105),表示各卡片的花费。
输出格式
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.
如果无法通过购买若干张卡片使得能够跳到任意一个格子,则输出 -1;否则,输出购买这样一组卡片的最小花费。
输入输出样例
输入#1
3 100 99 9900 1 1 1
输出#1
2
输入#2
5 10 20 30 40 50 1 1 1 1 1
输出#2
-1
输入#3
7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10
输出#3
6
输入#4
8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026
输出#4
7237
说明/提示
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.
在第一个样例测试中,仅购买一张卡片是不够的:例如,如果你购买一张长度为 100 的卡片,则你无法跳转到任意索引不是 100 的倍数的格子。最优方案是购买第一张和第二张卡片,这样你便能够跳转到任意格子。
在第二个样例测试中,即使你购买了所有卡片,你也无法跳转到任意索引不是 10 的倍数的格子,因此你应该输出 -1。
输入解题思路,AI测评打分。不知道怎么写?