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)

//wa time i don't believe in this at all
//suffix things are wack
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
const int A = 26;
struct SA {
	vector<unordered_map<char,int> > eg;
	vi len,link;
	int los,sz;
	void add(char ch) {
		int r = len.size();
		sz++;
		len.push_back(sz);
		link.push_back(0);
		eg.emplace_back();
		int p = los;
		while(p >= 0 && !eg[p].count(ch)) {
			eg[p][ch] = r;
			p = link[p];
		}
		if(p >= 0) {
			int q = eg[p][ch];
			if(len[p]+1 == len[q]) {
				link[r] = q;
			} else {
				int nq = len.size();
				len.push_back(len[p]+1);
				link.push_back(link[q]);
				eg.push_back(eg[q]);
				while(p >= 0 && eg[p][ch] == q) {
					eg[p][ch] = nq;
					p = link[p];
				}
				link[r] = link[q] = nq;
			}
		}
		los = r;
	}
	void add(const string& s) {
		for(int i=0;i<s.size();i++) {
			add(s[i]);
		}
	}
	SA(const string& s) {
		len.push_back(0);
		link.push_back(-1);
		eg.emplace_back();
		los = sz = 0;
		add(s);
	}
};
bool bup(int u, int len, string& res, SA& sa) {
	if(len == 0) {return true;}
	for(int i=0;i<A;i++) {
		char ch = i+'a';
		if(sa.eg[u].count(ch)) {
			int v = sa.eg[u][ch];
			if(bup(v,len-1,res,sa)) {
				res.push_back(ch);
				return true;
			}
		}
	}
	return false;
}
int main() {
	ll n,k;
	cin >> n >> k;
	string s;
	cin >> s;
	string rs = s;
	reverse(rs.begin(),rs.end());
	SA sa(s+rs);
	string ru;
	bup(0,n,ru,sa);
	reverse(ru.begin(),ru.end());
	ll nc = 0;
	for(;nc < n && ru[nc] == ru[0];nc++);
	int we = nc;
	while(nc < n && k > 1) {nc <<= 1;k--;}
	string res;
	for(int i=0;i<min(nc,n);i++) {
		res.push_back(ru[0]);
	}
	for(int i=we;i<n;i++) {
		res.push_back(ru[i]);
	}
	res = res.substr(0,n);
	cout << res << '\n';
}