from functools import cmp_to_key import heapq class InvertedStr: def __init__(self, s): self.s = s def __lt__(self, other): return self.s > other.s def compare(x, y): x_str = str(x) y_str = str(y) if x_str + y_str > y_str + x_str: return -1 else: return 1 n, k = map(int, input().split()) a = list(map(int, input().split())) sorted_a = sorted(a, key=cmp_to_key(compare)) initial_str = ''.join(map(str, sorted_a)) heap = [] visited = set() heapq.heappush(heap, (InvertedStr(initial_str), sorted_a)) visited.add(initial_str) output = [] while len(output) < k and heap: inverted_str, current_arr = heapq.heappop(heap) current_str = inverted_str.s output.append(current_str) # Generate all possible swaps for i in range(len(current_arr)): for j in range(i + 1, len(current_arr)): if current_arr[i] != current_arr[j]: new_arr = current_arr.copy() new_arr[i], new_arr[j] = new_arr[j], new_arr[i] new_str = ''.join(map(str, new_arr)) if new_str not in visited: visited.add(new_str) heapq.heappush(heap, (InvertedStr(new_str), new_arr)) for s in output[:k]: print(s)