Selling Numbers
Contest: Codeforces Round 402 (Div. 1)
Difficulty rating: 3000
Tags: dp, sortings

Boris really likes numbers and even owns a small shop selling interesting numbers. He has n decimal numbers Bi. Cost of the number in his shop is equal to the sum of costs of its digits. You are given the values cd, where cd is the cost of the digit d. Of course, Boris is interested in that numbers he owns have the maximum cost possible.

Recently Boris got hold of the magical artifact A, which can allow him to increase the cost of his collection. Artifact is a string, consisting of digits and '?' symbols. To use the artifact, Boris must replace all '?' with digits to get a decimal number without leading zeros (it is also not allowed to get number 0). After that, the resulting number is added to all numbers Bi in Boris' collection. He uses the artifact exactly once.

What is the maximum cost of the collection Boris can achieve after using the artifact?

Input format:
First line contains artifact A, consisting of digits '0'–'9' and '?' symbols (1 ≤ |A| ≤ 1000). Next line contains n — the amount of numbers in Boris' collection (1 ≤ n ≤ 1000). Next n lines contain integers Bi (1 ≤ Bi < 101000). A doesn't start with '0'.

Last line contains ten integers — costs of digits c0, c1, ..., c9 (0 ≤ ci ≤ 1000).

Output format:
Output one integer — the maximum possible cost of the collection after using the artifact.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the second sample input, the optimal way is to compose the number 453. After adding this number, Boris will have numbers 2656, 5682, 729 and 6696. The total cost of all digits in them is equal to 18 + 15 + 11 + 18 = 62.

Example input:
42
3
89
1
958
0 0 1 1 2 2 3 3 4 4

Example output:
4

Example input:
?5?
4
2203
5229
276
6243
2 1 6 1 1 2 5 2 2 3

Example output:
62

Editorial:
Because the target value for this problem is calculated independently for all digits, we'll use the dynamic programming approach. Define dpk, C as the maximum possible cost of digits after we processed k least significant digits in A and C is the set of numbers having the carry in current digit. This information is sufficient to choose the digit in the current position in A and recalculate the C set and DP value for the next digit.
The key observation is that there are only n + 1 possible sets instead of 2n. Consider last k digits of A and Bi. Sort all the length-k suffixes of Bi in descending lexicographical order. Because all these suffixes will be increased by the same value, the property of having the carry is monotone. That means that all possible sets C are the prefixes of length m (0 ≤ m ≤ n) of this sorted list of suffixes. This fact allows us to reduce the number of DP states to O(n·|A|). Sorting all suffixes of Bi can be accomplished using the radix sort, appending the digits to the left and recalculating the order.
The only thing that's left is to make all DP transitions in O(1) time. To do that, maintain the total cost of all digits and the amount of numbers that have the carry. After adding one more number with carry in current digit, these two values can be easily recalculated. After processing all digits in A, we have to handle the remaining digits in Bi (if there are any) and take the best answer. Total running time is $${ \mathcal { O } } ( n \cdot \operatorname* { m a x } ( | A |, | B _ { i } | ) )$$.