fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i, a, b) for (auto i = (a); i <= (b); ++i)
  5. #define ROF(i, a, b) for (auto i = (a); i >= (b); --i)
  6. #define sz(x) (int)(x).size()
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define endl '\n'
  10. #define fi first
  11. #define se second
  12.  
  13. #ifdef Juhan404
  14. #include "debug.h"
  15. #else
  16. #define debug(...)
  17. #endif
  18.  
  19. using ll = long long;
  20. using pii = pair<int, int>;
  21.  
  22. const int MOD = 1e9 + 7;
  23. const int N = 1e5 + 5;
  24.  
  25. int n, k, a[50];
  26. vector<string> v;
  27.  
  28. void back_tracking(string s, int cnt) {
  29. if (sz(s) == n) {
  30. if (cnt == k)
  31. v.pb(s);
  32. return;
  33. }
  34. back_tracking(s + '0', cnt);
  35. back_tracking(s + '1', cnt + 1);
  36. }
  37.  
  38. void run_case() {
  39. cin >> n >> k;
  40. FOR(i, 1, n) {
  41. cin >> a[i];
  42. }
  43. sort(a + 1, a + n + 1, greater<int>());
  44.  
  45. back_tracking("", 0);
  46.  
  47. for (string s : v) {
  48. ROF(i, n - 1, 0) {
  49. if (s[i] == '1')
  50. cout << a[i + 1] << " ";
  51. }
  52. cout << endl;
  53. }
  54. }
  55.  
  56. int main() {
  57. cin.tie(0)->sync_with_stdio(0);
  58.  
  59. #ifdef Juhan404
  60. freopen("Error.txt", "w", stderr);
  61. #endif
  62.  
  63. int T = 1;
  64.  
  65. for (int test = 1; test <= T; ++test) {
  66.  
  67. run_case();
  68. }
  69. return 0;
  70. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout