This page is still under construction.

Parts of this page are still being built. What you see may change.

Finding a string with enough inversions

Time limit2sMemory limit512 MB

Summary
Find the lexicographically smallest permutation of the first N lowercase letters with at least V inversions that is not smaller than the given string S.
Level

Medium7 of 10

Topics
Backtracking, Combinatorics, Greedy
Solved
No attempts yet

Problem

For a string SS of length NN, the number of inversions in SS is the number of pairs (i,j)(i, j) with 0≤i<j<N0 \le i < j < N and S[i]>S[j]S[i] > S[j]. The first character of a string is character 0. For example, "abcab" has three inversions: (1,3)(1, 3), (2,3)(2, 3), (2,4)(2, 4).

You are given integers NN and VV and a string SS. Consider the strings of length NN that use each of the first NN lowercase letters exactly once, that is, the permutations of those NN letters. Call such a string RR when it meets both conditions below.

  • The number of inversions in RR is at least VV.
  • RR does not come before SS in lexicographic order.

Find the RR that comes first in lexicographic order.

Input

The first line contains NN (1≤N≤201 \le N \le 20).

The second line contains VV (0≤V≤N×(N−1)/20 \le V \le N \times (N-1) / 2).

The third line contains the string SS. SS is made of some of the first NN lowercase letters, and no letter appears twice. The length of SS is at least 1 and at most NN.

Output

Print an RR that meets the conditions on the first line. If no such RR exists, print -1.

Examples3

  1. Example 1

    Input
    2
    1
    ab
    
    Expected output
    ba
    
  2. Example 2

    Input
    9
    1
    efcdgab
    
    Expected output
    efcdgabhi
    
  3. Example 3

    Input
    11
    55
    debgikjfc
    
    Expected output
    kjihgfedcba