All problems

Problem 656

Minimum Cost For Tickets

Medium · 20 pts · Dynamic Programming

Problem

You will travel on certain days of the year, numbered from 1 to 365. Train passes come in three kinds: a 1-day pass, a 7-day pass and a 30-day pass, with given prices. A pass bought on day d covers d and the following days of its length (a 7-day pass bought on day 2 covers days 2 to 8). Find the minimum total cost to cover every travel day. Let cost[d] be the cheapest cover of all travel days up to d; on a travel day, try each pass ending at d.

Input

The first line contains N. The second line contains the N travel days in increasing order. The third line contains the prices of the 1-day, 7-day and 30-day passes.

Output

Print the minimum total cost.

Constraints

1 <= N <= 365; 1 <= day <= 365; days strictly increasing; 1 <= price <= 1000

Example input

6
1 4 6 7 8 20
2 7 15

Example output

11

Explanation

For days 1 4 6 7 8 20 with prices 2 7 15, buy a 1-day pass on day 1, a 7-day pass on day 4 (covering 4 to 10) and a 1-day pass on day 20. The total is 2 + 7 + 2 = 11.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Strange Printer Next: Number of Ways to Stay in the Same Place