N and M (6)

Given N distinct natural numbers and M, print every ascending M-element subsequence in lexicographic order without repeats.

Easy3BacktrackingSortingRecursionBrute forceInterviewNo attempts yetTime limit1sMemory limit512 MB

Problem

You are given NN natural numbers and a natural number MM. Write a program that finds every sequence of length MM that satisfies the conditions below. The NN natural numbers are all different.

  • the sequence consists of MM numbers chosen from the given NN natural numbers
  • the chosen sequence is in ascending order

Input

The first line contains NN and MM. (1MN81 \le M \le N \le 8)

The second line contains NN numbers. Every number given in the input is a natural number less than or equal to 10,000.

Output

Print one sequence that satisfies the conditions per line. Do not print the same sequence twice, and separate the elements of each sequence with a space.

Print the sequences in increasing lexicographic order.