This page is still under construction.

Parts of this page are still being built. What you see may change.

Jumping Minho

Time limit2sMemory limit512 MB

Summary
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, 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 x−lix - 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 (1≤n≤3001 \le n \le 300).

The second line contains the lengths of the nn cards, l1,l2,…,lnl_1, l_2, \ldots, l_n (1≤li≤1091 \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 (1≤ci≤1051 \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.

Examples4

  1. Example 1

    Input
    3
    100 99 9900
    1 1 1
    
    Expected output
    2
    
  2. Example 2

    Input
    5
    10 20 30 40 50
    1 1 1 1 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    7
    15015 10010 6006 4290 2730 2310 1
    1 1 1 1 1 1 10
    
    Expected output
    6
    
  4. Example 4

    Input
    8
    4264 4921 6321 6984 2316 8432 6120 1026
    4264 4921 6321 6984 2316 8432 6120 1026
    
    Expected output
    7237