// Iterative C++ program to print sums of all
// possible subsets.
#include <bits/stdc++.h>
using namespace std;
// Prints sums of all subsets of array
void subsetSums(int nums[], int n)
{
// There are total 2^n subsets
vector<int> s = {0};//store the sums
for (int i = 0; i <n; i++) {
const int v = s.size();
for (int t = 0; t < v; t++) {
s.push_back(s[t] + nums[i]); //add this element with previous subsets
}
}
// Print
for(int i=0;i<s.size();i++)
cout << s[i] << " ";
}
// Driver code
int main()
{ int n;cin>>n;int a[n];for(auto &p:a){cin>>p;}
subsetSums(a, n);
return 0;
}
Ly8gSXRlcmF0aXZlIEMrKyBwcm9ncmFtIHRvIHByaW50IHN1bXMgb2YgYWxsCi8vIHBvc3NpYmxlIHN1YnNldHMuCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gUHJpbnRzIHN1bXMgb2YgYWxsIHN1YnNldHMgb2YgYXJyYXkKdm9pZCBzdWJzZXRTdW1zKGludCBudW1zW10sIGludCBuKQp7CgkvLyBUaGVyZSBhcmUgdG90YWwgMl5uIHN1YnNldHMKCXZlY3RvcjxpbnQ+IHMgPSB7MH07Ly9zdG9yZSB0aGUgc3VtcwoJCQoJCWZvciAoaW50IGkgPSAwOyBpIDxuOyBpKyspIHsKCQkJY29uc3QgaW50IHYgPSBzLnNpemUoKTsKCQkJZm9yIChpbnQgdCA9IDA7IHQgPCB2OyB0KyspIHsKCQkJCXMucHVzaF9iYWNrKHNbdF0gKyBudW1zW2ldKTsgLy9hZGQgdGhpcyBlbGVtZW50IHdpdGggcHJldmlvdXMgc3Vic2V0cwoJCQl9CgkJfQoJCS8vIFByaW50CgkJZm9yKGludCBpPTA7aTxzLnNpemUoKTtpKyspCgkJY291dCA8PCBzW2ldIDw8ICIgIjsKfQoKLy8gRHJpdmVyIGNvZGUKaW50IG1haW4oKQp7CWludCBuO2Npbj4+bjtpbnQgYVtuXTtmb3IoYXV0byAmcDphKXtjaW4+PnA7fQoKCXN1YnNldFN1bXMoYSwgbik7CglyZXR1cm4gMDsKfQo=