Problem:
Score : 1300 points

Problem Statement

Takahashi has a string S of length N consisting of lowercase English letters.
On this string, he will perform the following operation K times:

Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order.

Let S' be some contiguous substring of U with length N, and replace S with S'.

Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one.

Constraints

1 \leq N \leq 5000

1 \leq K \leq 10^9

|S|=N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the lexicographically smallest possible string that can be the string S after the K operations.

Sample Input 1

5 1
bacba

Sample Output 1

aabca

When S=bacba, T=abcab, U=bacbaabcab, and the optimal choice of S' is S'=aabca.

Sample Input 2

10 2
bbaabbbaab

Sample Output 2

aaaabbaabb

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#define rep(i, m, n) for(int i = m; i < n; i++)
#define repM(i, m, n) for(int i = n-1; i>=m; i--)
#define all(x) (x).begin(),(x).end()
typedef long long ll; using namespace std;
//typedef pair<int,int> pii; using namespace std;

// d = distance(lower_bound(all(x)), upper_bound(all(x)));

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

int main(){
  int N, K, m=1; cin >> N >> K; string s; cin >> s;
  if (N==1) {cout << s << endl; return 0;}

  int bc = 1e5; bool best = false;
  rep(i, 0, N) bc = min((int)s[i], bc);
  
  string worst, bestest;
  rep(i, 0, N) worst += 'z';
  rep(i, 0, N) bestest += 'a';
  
  if (K > 15 || 1<<(K-1) >= N) {
    //cout << "in" << endl;
    sort(s.begin(), s.end());
    for(int i=0; i<N; i++) cout << s[0];
    cout << endl;
    return 0;
  }
  
  string sn = s;
  
  for (int k=0; k<K; k++) {
    string s2 = sn;
    reverse(s2.begin(), s2.end());
    string s3 = sn + s2; sn = worst;
    for(int i=0; i<=N; i++) {
      if (s3.substr(i,N) < sn) sn = s3.substr(i,N);
      //cout << k << " "<< sn << endl;
    }
    reverse(sn.begin(), sn.end());
  }
  reverse(sn.begin(), sn.end());
  cout << sn << endl;
}