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: Wrong Answer)

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD 998244353ll
char S[5050],U[5050];
ll N,N2, K;
char ans[5050];
ll i, j, k;
int main(){
    scanf("%lld%lld%s",&N, &K,S);
    N2 = N * 2;
    fornum(i,0,N){
        U[i] = S[i];
        U[N * 2 - 1 - i] = S[i];
    }
    K--;
    char a = 'z';
    ll b = 0;
    j = 0;
    fornum(i,0,N2){
        if(U[i]<a){
            a = S[i];
            b = 1;
            j = 1;
        }else if(U[i]==a){
            j++;
            b = max(b, j);
        }else{
            j = 0;
        }
    }
    for (i = 0; (b<<i)<N;i++)
        ;
    if(i<=K){
        fornum(i,0,N){
            printf("%c", a);
        }
        return 0;
    }
    fornum(i,0,b){
        ans[i] = a;
    }
    fornum(i,i,N){
        ans[i] = 'z';
    }
    for (i = N2 - 1; i >= N-1;i--){
        bool f = false;

        fornum (j ,0,N){
            if(ans[j]>U[i-j]){
                break;
            }else if(ans[j]<U[i-j]){
                f = true;
                break;
            }
        }
        if(f)
            continue;
        fornum(j,j,N){
            ans[j] = U[i - j];
        }
    }
    //printf("%s\n", ans);
    fornum(i,0,(b<<K)){
        printf("%c", a);
    }
    fornum(j,i,N)
    {
        printf("%c", ans[b+j-i]);
    }

    return 0;
}