In order to make some extra money, Stefan decided to open a brand new pancake house for the upcoming summer season, because after all, who wouldn’t buy some wonderful pancakes during the summer holiday?
The house is going to sell different types of pancakes, each labeled with a certain number, based on its type. Now, in order to find the price of a pancake, Stefan decided to use his imagination and come up with a really interesting algorithm for deciding the price of a pancake.
Namely, because prime numbers are his favorite, the price is going to depend on how prime a pancake is.
Therefore:
- The base price of a pancake is euro, where is given in the input.
- If the label of the pancake is prime, the price increases by label euro, where label is the value of the label and is given in the input.
- For each prime digit of the label (), the price will increase by euro, where values are given in the input.
- If the sum of the digits of the label is prime, the price increases by euro, where is the sum of digits of the label.
- If the product of the digits of the label is prime, the price will increase by euro, where is the product of the digits of the label.
Obviously, this may not lead to enough profit and now it’s time for you to step in and help Stefan get the highest profit by changing at most one digit from each label in order to maximize the sum of prices of all the pancake labels.
However, you can’t make the most significant digit become and thus getting a number with one less digit, even though it might be the most lucrative option.
Input
The first line of the input will have an integer (), representing the pancakes which are initially available in Stefan’s pancake house.
The second line of the input will have integers, representing the initial labels of the pancakes ().
The third line of the input will have two values, and (), explained in the statement.
The fourth line of the input will have four values, , , , ().
For tests worth points, ().
Output
The output will contain a single line, representing the maximum possible profit Stefan can get by changing conveniently some of the pancake labels according to the rule from the statement.
Example
stdin
4
12 89 941 101
5 7
8 5 3 9
stdout
1907
Explanation
We should change the first value from 12 to 17 and that would lead us to a value of 45.
It is optimal to keep the second value to be 89 for a value of 118.
We should change the third value from 941 to 991 and that would lead us to a value of 1022.
We should change the fourth value from 101 to 701 and that would lead us to a value of 722.
The sum of values will become 1907.