Jumping Minho

Buy a cheapest subset of jump lengths so every integer on the line is reachable from the start; print -1 if impossible.

Medium6Dynamic programmingNumber theoryMathNo attempts yetTime limit2sMemory limit512 MB

Problem

There is an infinitely long one-dimensional coordinate line whose points are the negative integers, 00, and the positive integers. Minho stands on one point of this line.

In front of Minho there is a shop that sells nn cards. Card ii is labeled with a length lil_i and a price cic_i. If Minho pays cic_i won for that card, he can jump from any point xx to xlix - l_i or to x+lix + l_i.

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 nn (1n3001 \le n \le 300).

The second line contains the lengths of the nn cards, l1,l2,,lnl_1, l_2, \ldots, l_n (1li1091 \le l_i \le 10^9).

The third line contains the prices of the nn cards, c1,c2,,cnc_1, c_2, \ldots, c_n (1ci1051 \le c_i \le 10^5).

Output

If no set of cards makes every point reachable, print 1-1.

Otherwise, print the smallest cost among the purchases that make every point reachable.