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++ (GCC 9.2.1) (judge verdict: Accepted)

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
using namespace std;
using ll=long long;
using ld=long double;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
struct edge{ll to,cost;};
struct edge2{ll from,to,cost;};

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N,K;
    string S,T;
    cin >> N >> K >> S;
    T=S;
    reverse(T.begin(),T.end());
    S=S+T;
    char c='z';
    rep(i,S.size()){
        chmin(c,S[i]);
    }
    ll now=0,mx=0;
    rep(i,S.size()){
        if(S[i]==c){
            now++;
        }
        else{
            now=0;
        }
        //cout << now << endl;
        chmax(mx,now);
    }
    //cout << S << mx << endl;
    K--;
    ll cnt=0,k=mx;
    while(cnt<K&&k<N){
        cnt++;
        k*=2;
    }
    chmin(k,N);
    reverse(S.begin(),S.end());
    string min;
    rep(i,N+1){
        min+="z";
    }
    rep(i,S.size()/2){
        if(S[i]==c){
            chmin(min,S.substr(i));
        }
    }
    rep(i,k){
        cout << c;
    }
    if(N-k!=0){
        cout << min.substr(mx,N-k) << endl;
    }
}