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)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define PII pair<int,int>
#define MP make_pair
#define fir first
#define sec second
#define PB push_back
#define db long double
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=5010;
char str[N<<1];
int n,k;
char ch;
int len[N<<1];
int ansl,ansr;
int anslen;
void upd(int l,int r) {
	if(!ansl) return (void)(ansl=l,ansr=r);
	for(int i=0;i<anslen;++i)
		if(str[r-i]!=str[ansr-i]) {
			if(str[r-i]<str[ansr-i])
				ansr=r,ansl=l;
			return;
		}
}
int main() {
	rd(n),rd(k);
	scanf("%s",str+1);
	ch=127;
	for(int i=1;i<=n;++i) {
		str[n+i]=str[n-i+1];
		ch=min(ch,str[i]);
	}
	int mxlen=0;
	for(int i=1;i<=n*2;++i) {
		if(str[i]==ch) len[i]=len[i-1]+1;
		else len[i]=0;
		mxlen=max(mxlen,len[i]);
//		cout<<i<<':'<<len[i]<<endl;
	}
	if(mxlen*(1ll<<(min(k-1,20)))>=n) {
		for(int i=1;i<=n;++i) putchar(ch);
		return 0;
	}
	int _len=mxlen*(1<<k-1); anslen=n-_len;
	if(k==1) {
		int ansp=1;
		for(int i=2;i<=n+1;++i) {
			for(int j=0;j<n;++j)
				if(str[i+j]!=str[ansp+j]) {
					if(str[i+j]<str[ansp+j]) ansp=i;
					break;
				}
		}
		for(int j=0;j<n;++j) putchar(str[ansp+j]);
		return 0;
	}
	for(int i=1;i<=n+1;++i) {
		if(len[i+n-1]<mxlen) continue;
		upd(i+n-mxlen-anslen,i+n-1-mxlen);
	}
//	cout<<ansl<<' '<<ansr<<endl;
	for(int i=1;i<=_len;++i) putchar(ch);
	for(int i=ansr;i>=ansl;--i) putchar(str[i]);
	return 0;
}