#include using namespace std; using ll = long long; bool adhoc_comparator(const pair &a, const pair &b) { ll check_len = min(a.first.size(), b.first.size()); for (ll i = 0; i < check_len; i++) { if (a.first[i] != b.first[i]) { return a.first[i] > b.first[i]; } } if (a.first.size() < b.first.size()){ return adhoc_comparator(a, {b.first.substr(check_len), b.second}); } if (a.first.size() > b.first.size()){ return adhoc_comparator({a.first.substr(check_len), a.second}, b); } else { return a.second > b.second; } } int main() { ll n, k; cin >> n >> k; vector> v(n); for (ll i = 0; i < n; i++) { cin >> v[i].first; v[i].second = v[i].first.size(); } sort(v.begin(), v.end(), adhoc_comparator); priority_queue pq; for (ll i = 0; i < 800000; i++) { string s; for (ll j = 0; j < n; j++) { s += v[j].first; } pq.push(s); next_permutation(v.begin(), v.end(), adhoc_comparator); } string now = "z"; for (ll i = 0; i < k; i++){ while (now <= pq.top()){ pq.pop(); } cout << pq.top() << "\n"; now = pq.top(); pq.pop(); } cout << flush; }