atcoder#ABC257E. [ABC257E] Addition and Multiplication 2

[ABC257E] Addition and Multiplication 2

Score : 500500 points

Problem Statement

Takahashi has an integer xx. Initially, x=0x=0.

Takahashi may do the following operation any number of times.

  • Choose an integer i (1i9)i\ (1\leq i \leq 9). Pay CiC_i yen (the currency in Japan) to replace xx with 10x+i10x + i.

Takahashi has a budget of NN yen. Find the maximum possible value of the final xx resulting from operations without exceeding the budget.

Constraints

  • 1N1061 \leq N \leq 10^6
  • 1CiN1 \leq C_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

C1C_1 C2C_2 \ldots C9C_9

Output

Print the answer.

5
5 4 3 3 2 5 3 5 3
95

For example, the operations where i=9i = 9 and i=5i=5 in this order change xx as:

09950 \rightarrow 9 \rightarrow 95.

The amount of money required for these operations is C9+C5=3+2=5C_9 + C_5 = 3 + 2 = 5 yen, which does not exceed the budget. Since we can prove that we cannot make an integer greater than or equal to 9696 without exceeding the budget, the answer is 9595.

20
1 1 1 1 1 1 1 1 1
99999999999999999999

Note that the answer may not fit into a 6464-bit integer type.