fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n, x, a[25], ok;
  4. vector<string> ans;
  5. vector<int> v;
  6. void Try(int i, int sum) {
  7. if(sum>x) return;
  8. if(sum==x) {
  9. string s="{";
  10. for(int i=0; i<v.size()-1; i++) s+=to_string(v[i])+" ";
  11. s+=to_string(v.back())+"}";
  12. ans.push_back(s);
  13. ok=1;
  14. }
  15. for(int j=i; j<n; j++) {
  16. v.push_back(a[j]);
  17. Try(j,sum+a[j]);
  18. v.pop_back();
  19. }
  20. }
  21. main() {
  22. int t;
  23. cin >> t;
  24. while(t--) {
  25. cin >> n >> x;
  26. for(int i=0; i<n; i++) cin >> a[i];
  27. ok=0;
  28. Try(0,0);
  29. if(!ok) cout << "-1";
  30. else {
  31. cout << ans.size() << " ";
  32. for(int i=0; i<ans.size(); i++) cout << ans[i] <<" ";
  33. }
  34. cout << endl;
  35. }
  36. }
Success #stdin #stdout 0.01s 5460KB
stdin
2
4  8

2  4  6  8

2 9

10 11
stdout
5 {2 2 2 2} {2 2 4} {2 6} {4 4} {8} 
-1