import heapq def main(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) k = int(data[1]) a = data[2:2+n] # Convert each number to its string representation str_list = a # Convert each string to a tuple of ASCII values str_tuples = [tuple(ord(c) for c in s) for s in str_list] heap = [] visited = set() for i in range(n): current_tuple = str_tuples[i] remaining = [] for j in range(n): if j != i: remaining.append(str_tuples[j]) # Sort the remaining tuples lexicographically remaining_sorted = tuple(sorted(remaining)) # Create the heap element with negative ASCII values heap_element = tuple(-x for x in current_tuple) state = (heap_element, remaining_sorted) if state not in visited: heapq.heappush(heap, (heap_element, remaining_sorted)) visited.add(state) results = [] while len(results) < k and heap: current_neg_tuple, remaining = heapq.heappop(heap) current_tuple = tuple(-x for x in current_neg_tuple) if not remaining: s = ''.join(chr(c) for c in current_tuple) results.append(s) if len(results) == k: break else: for i in range(len(remaining)): next_tuple = remaining[i] new_tuple = current_tuple + next_tuple new_remaining = list(remaining) del new_remaining[i] new_remaining_sorted = tuple(sorted(new_remaining)) new_neg_tuple = tuple(-x for x in new_tuple) state = (new_neg_tuple, new_remaining_sorted) if state not in visited: heapq.heappush(heap, (new_neg_tuple, new_remaining_sorted)) visited.add(state) for s in results[:k]: print(s) if __name__ == "__main__": main()