Tiny Types

Time limit: 0.3s Memory limit: 64MB Input: Output:

A little hero is a 11 year old prodigy and he just learned about numbers and programming. While he was doing some programming, he saw that numbers can be represented using various data types and he started thinking at a game:

Given nn integers and kk data types, we want to be able to store the various values using a single variable (after all, our hero is just 1) which can change its datatype if we pay a certain cost. The initialization of the variable is free, however, so we can start with whichever data type we want.

Now the little hero wants to compute the minimum cost of changing the data types so that he's able to represent the various integers he will have to use while processing the input without any kind of overflow.

We can use the data type ii in order to store integers from 00 to 2i12^i - 1. There are no negative integers in the input.

However, he can't use too big data types either, so in order to avoid doing that, there is also a penalty for using data types bigger than the minimal size we need to use for a certain number.

For example, if he needs to represent 55, the smallest data type necessary for storing 55 is data type 33, so if he uses data type 55, the penalty we have is equal to penalty2penalty_2, since we used a data type two tiers higher than what we needed 55 - 33 = 22.

Input data

The first line of the input contains nn, the number of integers the little hero needs to represent and kk, the number of data types.

The next line contains the nn integers the hero needs to use, which are represented using the array vv.

The next kk lines contain kk integers each, representing the cost of changing the data type from type ii to type jj.

The next line contains kk integers, representing the penalty array. It is guaranteed that the penalty array is non-decreasing.

Output data

The output file contains a single integer, representing the minimum cost of processing the data types given the rules described in the statement.

Constraints and clarifications

  • 1n1051 \leq n \leq 10^5
  • 1k301 \leq k \leq 30
  • 0vi<2k0 \leq v_i < 2^k
  • 0costij1000 \leq cost_{ij} \leq 100. It is guaranteed that costii=0cost_{ii} = 0 for all values ii.
  • 0penaltyi1000 \leq penalty_i \leq 100

Example 1

stdin

5 3
4 2 6 3 1
0 3 2
3 0 1
4 2 0
1 4 7

stdout

4

Log in or sign up to be able to send submissions!