Devil's Share

Time limit: 0.4s Memory limit: 256MB Input: Output:

You are given a number, XX. The devil wants his share of the number. He will take the largest subnumber with KK digits. Minimize the devil’s share by reordering the digits in number XX.

Task

Formally, you have at your disposal SS digits between 11 and 99, inclusively. Given an integer KK, you are to create a number XX using all the digits at your disposal, such that the largest length KK substring of XX is as small as possible.

Input data

The first line of input contains one integer TT - the number of test scenarios to analyse.

The description of TT test scenarios follows. Each test scenario consists of two lines:

  • The first line contains one integer KK - the length of all the substrings to consider.
  • The second line contains 99 space-separated integers: D1,D2,,D9D_1, D_2, \dots, D_9, where DiD_i represents the number of digits ii at your disposal.

Output data

For each test scenario, print XX - the number you created, on a separate line.

If there are several numbers XX with the same smallest possible length KK substring you can output any of them.

Constraints and clarifications

  • 1T1 000 0001 \leq T \leq 1 \ 000 \ 000
  • 1S100 0001 \leq S \leq 100 \ 000
  • 1KS1 \leq K \leq S
  • 0Di, D1+D2++D9=S0 \leq D_i, \ D_1 + D_2 + \dots + D_9 = S
  • A length KK substring of XX is a base 1010 integer comprising of KK consecutive digits of XX in the very same order. There are SK+1S - K + 1 such substrings in number XX.
# Points Restrictions
1 13 0D1,D2,D3,D43, D5=D6=D7=D8=D9=0, 1T15360 \leq D_1, D_2, D_3, D_4 \leq 3, \ D_5 = D_6 = D_7 = D_8 = D_9 = 0, \ 1 \leq T \leq 1536, scenarios will not repeat
2 14 K=2K = 2
3 29 D3=D4==D9=0D_3 = D_4 = \dots = D_9 = 0
5 44 No further restrictions.

Example

stdin

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

stdout

2313
62616236261623778899
623616236162361778899

Explanation

There are three test scenarios to consider in the example.

In the first scenario K=2K = 2 and you have to arrange digits 12331233.
One optimal XX is 23132313, with the following length 22 substrings: 23,3123, 31 and 1313, the largest being 3131. No other XX has a smaller largest length 22 substring.
Another optimal XX would be 31233123, since its largest length 22 substring is also 3131.

In the second scenario K=7K = 7 and you have to arrange digits 1122223366666677889911222233666666778899.
One optimal XX is 6261623626162377889962616236261623778899 with the largest length 77 substring 62616236261623.

In the third scenario K=7K = 7 and you have to arrange digits 111222333666666778899111222333666666778899.
One optimal XX is 623616236162361778899623616236162361778899 with the largest length 77 substring 62361776236177.

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