fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m, a[31];
  4. vector<int>ans;
  5. void dfs(int id, int sum, int ch) {
  6. if (sum == m) {
  7. ans.push_back(ch);
  8. return;
  9. }
  10. for(int i=id; i<n; i++) {
  11. if (sum + a[i] > m) break;
  12. if (ch & (1 << i)) continue;
  13. dfs(i+1, sum+a[i], ch | (1 << i));
  14. }
  15. }
  16. int main() {
  17. while(cin >> n >> m) {
  18. ans.clear();
  19. for(int i=0; i<n; i++) cin >> a[i];
  20. sort(a, a+n);
  21. dfs(0, 0, 0);
  22. if (ans.size() == 0) cout << -1 << endl;
  23. for(int r: ans) {
  24. for(int i=0; i<31; i++) {
  25. if (r & (1 << i)) cout << a[i] << " ";
  26. }
  27. cout << endl;
  28. }
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0s 4472KB
stdin
4 100
10 40 50 90
stdout
10 40 50 
10 90