Jumping Minho
Time limit2sMemory limit512 MB
Buy a cheapest subset of jump lengths so every integer on the line is reachable from the start; print -1 if impossible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Number theory, Math
- Solved
- No attempts yet
Problem
There is an infinitely long one-dimensional coordinate line whose points are the negative integers, , and the positive integers. Minho stands on one point of this line.
In front of Minho there is a shop that sells cards. Card is labeled with a length and a price . If Minho pays won for that card, he can jump from any point to or to .
At the start Minho owns no card, so he cannot leave the point he stands on. Once he spends money on cards, he can jump to other points.
Minho wants to buy some cards so that every point of the line becomes reachable. If such a purchase exists, he wants to spend as little money as possible.
Write a program that decides whether some set of cards makes every point reachable and, when it does, computes the smallest possible cost.
Input
The first line contains the number of cards ().
The second line contains the lengths of the cards, ().
The third line contains the prices of the cards, ().
Output
If no set of cards makes every point reachable, print .
Otherwise, print the smallest cost among the purchases that make every point reachable.