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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

int dp[5001][5001][2][2];

int main(){
  int N, K;
  char s[SIZE];

  scanf("%d%d%s", &N, &K, s);



  queue<int> que;

  string ans = "";

  // 0: <-, 1: ->

  for(int i=0; i<N; i++) {
    for(int j=0;j<N;j++) {
      dp[i+1][j][0][0] = dp[i+1][j][1][0] = INF;
      dp[i+1][j][0][1] = dp[i+1][j][1][1] = INF;
    }

    char minc = 'z';

    for(int j=0; j<N; j++) {
      for(int k=0; k<2; k++) {
        if(j > 0 && dp[i][j][0][k] < K) minc = min(minc, s[j-1]);
        if(j+1 < N && dp[i][j][1][k] < K) minc = min(minc, s[j+1]);
        if(dp[i][j][0][k] < K-1) minc = min(minc, s[j]);
        if(dp[i][j][1][k] < K-1) minc = min(minc, s[j]);
      }
    }

    if(dp[i][N-1][1][0] < K) minc = min(minc, s[N-1]);


    for(int j=0; j<N; j++) {
      for(int k=0; k<2; k++) {
        if(j > 0 && dp[i][j][0][k] < K && minc == s[j-1])
          dp[i+1][j-1][0][k] = min(dp[i+1][j-1][0][k], dp[i][j][0][k]);
        if(j+1 < N && dp[i][j][1][k] < K && minc == s[j+1])
          dp[i+1][j+1][1][k] = min(dp[i+1][j+1][1][k], dp[i][j][1][k]);
        if(dp[i][j][0][k] < K-1 && minc == s[j])
          dp[i+1][j][1][k] = min(dp[i+1][j][1][k], dp[i][j][0][k] + 1);
        if(dp[i][j][1][k] < K-1 && minc == s[j])
          dp[i+1][j][0][k] = min(dp[i+1][j][0][k], dp[i][j][1][k] + 1);
      }
    }

    if(dp[i][N-1][1][0] < K && minc == s[N-1])
      dp[i+1][N-1][0][1] = min(dp[i+1][N-1][0][1], dp[i][N-1][1][0]);


    ans += minc;
  }

  cout << ans << endl;

  return 0;
}