import java.util.*;
import java.io.*;

public class Main {
    public static int n; public static int m;
    public static int[] ans;
    public static int[] numbers;
    public static boolean[] tf;
    public static LinkedHashSet<String> set;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] arr1 = br.readLine().split(" "); String[] arr2 = br.readLine().split(" ");
        n = Integer.parseInt(arr1[0]); m = Integer.parseInt(arr1[1]);
        numbers = new int[n]; ans = new int[m]; tf = new boolean[n];
        set = new LinkedHashSet<>();
        StringBuilder sb2 = new StringBuilder();
        
        for(int i=0; i<n; i++) { numbers[i] = Integer.parseInt(arr2[i]); }
        Arrays.sort(numbers);
        
        calc(0);
        for(String s : set) { sb2.append(s).append("\n"); }
        System.out.println(sb2);
    }
    
    public static void calc(int index) {
        if(index==m) {
            StringBuilder sb = new StringBuilder();
            for(int a : ans) {
                sb.append(String.valueOf(a)).append(" ");
            }
            
            set.add(sb.toString()); return;
        }
        
        for(int i=0; i<n; i++) {
            if(tf[i]) { continue; }
            
            tf[i] = true;
            ans[index] = numbers[i];
            calc(index+1);
            tf[i] = false;
        }
    }
}

'Problem Solving > BOJ' 카테고리의 다른 글

[Silver 3] 15657번 N과 M (8)  (0) 2022.04.01
[Silver 3] 15656번 N과 M (7)  (0) 2022.04.01
[Silver 3] 15655번 N과 M (6)  (0) 2022.04.01
[Silver 3] 15654번 N과 M (5)  (0) 2022.04.01
[Silver 3] 15652번 N과 M (4)  (0) 2022.04.01