
const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'Wiki\\input.txt';
const [nm, inputs] = fs.readFileSync(path).toString().trim().split('\n');
const [n, m] = nm.split(' ').map((it) => Number(it));
const numbers = inputs.split(' ').map((it) => Number(it));
numbers.sort((a, b) => a - b);
let answers = '';
const bt = (current, idx) => {
if (idx === m) {
const answer = current.join(' ');
answers += answer + '\n';
return;
}
for (let i = 0; i < n; i++) {
const target = numbers[i];
if (current[current.length - 1] > target) continue;
if (current.includes(target)) continue;
bt([...current, target], idx + 1);
}
};
for (const num of numbers) {
bt([num], 1);
}
console.log(answers);
N과 M(5)와 동일한 문제이나 오름차순으로 출력해야한다는 조건이 있으므로 숫자를 선택하기 전에 내가 선택한 숫자에서 나보다 큰 수가 없다는 것을 확인하고 재귀를 호출해준다.