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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            String s = in.next();
            String t = s + reverse(s);
            String res = "" + ((char) ('z' + 1));
            if (k == 1) {
                for (int i = 0; i <= n; ++i) {
                    String cur = t.substring(i, i + n);
                    if (cur.compareTo(res) < 0) res = cur;
                }
                out.println(res);
                return;
            }
            char minLetter = s.charAt(0);
            for (char c : s.toCharArray()) if (c < minLetter) minLetter = c;
            int maxCount = 0;
            int count = 0;
            for (char c : s.toCharArray()) {
                if (c == minLetter) {
                    ++count;
                    maxCount = Math.max(maxCount, count);
                } else {
                    count = 0;
                }
            }
            maxCount = Math.max(maxCount, count * 2);
            int finalCount = maxCount;
            for (int stage = 1; stage < k; ++stage) {
                finalCount *= 2;
                if (finalCount >= n) {
                    StringBuilder b = new StringBuilder();
                    for (int i = 0; i < n; ++i) b.append(minLetter);
                    out.println(b.toString());
                    return;
                }
            }
            count = 0;
            for (int i = 0; i < n; ++i) {
                char c = s.charAt(i);
                if (c == minLetter) {
                    ++count;
                } else {
                    count = 0;
                }
                if (count == maxCount) {
                    int tailPos = 2 * n - (i + 1);
                    String cur = reverse(t.substring(tailPos - (n - finalCount), tailPos));
                    if (cur.compareTo(res) < 0) {
                        res = cur;
                    }
                }
            }
            if (count * 2 == maxCount) {
                int tailPos = n - count;
                String cur = reverse(t.substring(tailPos - (n - finalCount), tailPos));
                if (cur.compareTo(res) < 0) {
                    res = cur;
                }
            }
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < finalCount; ++i) b.append(minLetter);
            b.append(res);
            out.println(b.toString());
        }

        private String reverse(String s) {
            StringBuilder b = new StringBuilder(s);
            b.reverse();
            return b.toString();
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}