Problem 845
Orderly Queue
Hard · 30 pts · String, Math, Sorting
Problem
You may repeat this move as often as you like: take one of the first K letters of the string and move it to the end. Find the lexicographically smallest string you can reach. With K = 1 you can only rotate the string, so try all rotations and pick the smallest. With K >= 2 you can swap neighbouring letters through a sequence of moves, so any order is reachable and the answer is simply the sorted string.
Input
The first line contains S (lowercase letters). The second line contains K.
Output
Print the smallest reachable string.
Constraints
1 <= K <= |S| <= 1000
Example input
cba 1
Example output
acb
Explanation
For cba with K = 1, the rotations are cba, bac and acb, and the smallest is acb.