Under construction

Still being built. Things may change

N and M (8)

Time limit1sMemory limit512 MB

Summary
Given N distinct natural numbers and a length M, print all non-decreasing sequences of length M drawn from the numbers, in lexicographic order.
Level

Medium4 of 10

Topics
Backtracking, Sorting, Recursion
Solved
No attempts yet

Problem

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

  • a sequence of M numbers chosen from the N natural numbers
  • the same number may be chosen more than once.
  • the chosen sequence must be non-decreasing.
    • a sequence AA of length KK is non-decreasing if it satisfies A1≤A2≤⋯≤AK−1≤AKA_1 \le A_2 \le \dots \le A_{K-1} \le A_K.

Input

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

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

Output

Print one sequence satisfying the conditions per line. Do not print the same sequence more than once, and separate the numbers of each sequence with a space.

Print the sequences in increasing lexicographic order.

Examples3

  1. Example 1

    Input
    3 1
    4 5 2
    
    Expected output
    2
    4
    5
    
  2. Example 2

    Input
    4 2
    9 8 7 1
    
    Expected output
    1 1
    1 7
    1 8
    1 9
    7 7
    7 8
    7 9
    8 8
    8 9
    9 9
    
  3. Example 3

    Input
    4 4
    1231 1232 1233 1234
    
    Expected output
    1231 1231 1231 1231
    1231 1231 1231 1232
    1231 1231 1231 1233
    1231 1231 1231 1234
    1231 1231 1232 1232
    1231 1231 1232 1233
    1231 1231 1232 1234
    1231 1231 1233 1233
    1231 1231 1233 1234
    1231 1231 1234 1234
    1231 1232 1232 1232
    1231 1232 1232 1233
    1231 1232 1232 1234
    1231 1232 1233 1233
    1231 1232 1233 1234
    1231 1232 1234 1234
    1231 1233 1233 1233
    1231 1233 1233 1234
    1231 1233 1234 1234
    1231 1234 1234 1234
    1232 1232 1232 1232
    1232 1232 1232 1233
    1232 1232 1232 1234
    1232 1232 1233 1233
    1232 1232 1233 1234
    1232 1232 1234 1234
    1232 1233 1233 1233
    1232 1233 1233 1234
    1232 1233 1234 1234
    1232 1234 1234 1234
    1233 1233 1233 1233
    1233 1233 1233 1234
    1233 1233 1234 1234
    1233 1234 1234 1234
    1234 1234 1234 1234