Stringer

Interview

Time limit1sMemory limit128 MB

Summary
Given fixed counts of each of N letters, find the K-th string in alphabetical order among all arrangements, without listing them.
Level

Medium6 of 10

Topics
Combinatorics, Math, Greedy, String matching
Solved
No attempts yet

Problem

Consider all strings built from the first NN letters of the alphabet in which the number of a's, the number of b's, and so on are each fixed to a predetermined (though possibly different) value. List every such string in alphabetical order and number the entries starting from 00. Given an index KK, output the KK-th string in this list.

For example, take the strings over N=2N = 2 letters (a and b) that use exactly 22 a's and 33 b's. Sorted alphabetically they are:

IndexStringIndexString
0aabbb5babab
1ababb6babba
2abbab7bbaab
3abbba8bbaba
4baabb9bbbaa

If K=5K = 5, the answer is babab.

Input

The input consists of several datasets. Each dataset spans two lines.

The first line holds two integers NN and KK (1≤N≤201 \le N \le 20, 0≤K<m0 \le K < m), where NN is the number of alphabet letters used, KK is the index of the wanted list entry, and mm is the total number of strings in the list. The value mm is not given explicitly in the input.

mm may be extremely large — far too large to build the whole list — but the input is chosen so that both mm and KK fit in a signed 32-bit integer.

The second line holds NN non-negative integers: the required number of a's, of b's, and so on. Their sum is at least 11 and at most 5050.

The input ends with a line containing two zeros.

Output

For each dataset, print the answer string on its own line. Do not print any extra whitespace, and do not print blank lines between answers.

Examples3

  1. Example 1

    Input
    2 5
    2 3
    3 0
    2 3 1
    0 0
    
    Expected output
    babab
    aabbbc
    
  2. Example 2

    Input
    1 0
    5
    0 0
    
    Expected output
    aaaaa
    
  3. Example 3

    Input
    2 9
    2 3
    0 0
    
    Expected output
    bbbaa