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>
using namespace std;

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9 + 10;
const ll MOD = 1e9 + 7;

const int MAXN = 1e4 + 5;

char c[MAXN], c1[MAXN], c2[MAXN];

char ans[MAXN];

char minc;

int n, k;

void str_double (char *c) {
	REP(i, 0, n) {
		c[2 * n - 1 - i] = c[i];
	}
}

void process() {
	int len = 0;
	for(int i = n - 1; i >= 0 && c2[i] == minc; i--, len++);
	//printf("%d %d\n", k, len);
	REP(j, 0, k - 1) {
		str_double(c2);
		REP(i, 0, n) {
			c2[i] = c2[i + len];
		}
		c2[n] = 0;
		//printf("%s\n", c2);
		len = min(len * 2, n);
	}
	str_double(c2);
	c2[2 * n - len] = 0;
	if (strcmp(ans, c2 + n - len) == 1) {
		strcpy(ans, c2 + n - len);
	}
}

void solve(){
	//printf("aabca\n");
	//return;
	
	scanf("%d%d%s", &n, &k, &c[0]);
	strcpy(ans, c);
	strcpy(c1, c);
	str_double(c1);
	//printf("%s\n", c1);
	if (k == 1) {
		REPN(i, 0, n) {
			REP(j, i, i + n) {
				c2[j - i] = c1[j];
			}
			if (strcmp(ans, c2) == 1) {
				strcpy(ans, c2);
			}
		}
		printf("%s\n", ans);
		return;
	}
	minc = 'z';
	REP(i, 0, n) {
		minc = min(minc, c[i]);
	}
	int lim = 1;
	for(int i = 1; i < n; i *= 2, lim++);
	
	//printf("%d\n", lim);
	
	if (k >= lim) {
		REP(i, 0, n) {
			putchar(minc);
		}
		putchar('\n');
		return;
	}
	
	REP(i, n - 1, 2 * n) {
		if (c1[i] == minc && (i == n - 1 || c1[i + 1] != minc)) {
			REPN(j, i - n + 1, i) {
				c2[j - (i - n + 1)] = c1[j];
			}
			c2[n] = 0;
			//printf("%d %s\n", i, c2);
			process();
		}
		if (i == n - 1) {
			k--;
		}
	}
	
	printf("%s\n", ans);
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}