Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <numeric>
#include <list>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <bitset>
#include <queue>
#include <unordered_set>

using namespace std;
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
#define rep(i, n) for(int i = 0;i < n;i++)
const long long INF = 1LL << 61;

int MOD = 1000000007;

template <class C>
void print(const C& c, std::ostream& os = std::cout)
{
  std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, ", "));
  os << std::endl;
}


int main(){
    std::ios_base::sync_with_stdio(false);

    int x,y,z,cnt; cin >> x >> y >> z >> cnt;

    std::vector<ll> a(x); rep(i,x) cin >> a[i];
    std::vector<ll> b(y); rep(i,y) cin >> b[i];
    std::vector<ll> c(z); rep(i,z) cin >> c[i];

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());

    std::priority_queue<pair<ll, string>> q;


    int i = a.size()-1;
    int k = c.size()-1;
    int j = b.size()-1;

    q.push(make_pair(a[i]+b[j]+c[k], string(to_string(i)+to_string(j)+to_string(k))));
    unordered_set<string> used; 

    used.emplace(to_string(i)+to_string(j)+to_string(k));

    //print(used);

    while(cnt>0){
        --cnt;

        pair<ll, string> item = q.top();

        i = item.second[0]-'0';
        j = item.second[1]-'0';
        k = item.second[2]-'0';

        q.pop();



        if(i>0 && used.find(to_string(i-1)+to_string(j)+to_string(k))==used.end()){
            q.push(make_pair(a[i-1]+b[j]+c[k], to_string(i-1)+to_string(j)+to_string(k)));
            used.emplace(to_string(i-1)+to_string(j)+to_string(k));
        }
        if(j>0 && used.find(to_string(i)+to_string(j-1)+to_string(k))==used.end()){
            q.push(make_pair(a[i]+b[j-1]+c[k], to_string(i)+to_string(j-1)+to_string(k)));
            used.emplace(to_string(i)+to_string(j-1)+to_string(k));
        }
        if(k>0 && used.find(to_string(i)+to_string(j)+to_string(k-1))==used.end()){
            q.push(make_pair(a[i]+b[j]+c[k-1], to_string(i)+to_string(j)+to_string(k-1)));
            used.emplace(to_string(i)+to_string(j)+to_string(k-1));
        }
        if(i>0 && j>0 && used.find(to_string(i-1)+to_string(j-1)+to_string(k))==used.end()){
            q.push(make_pair(a[i-1]+b[j-1]+c[k], to_string(i-1)+to_string(j-1)+to_string(k)));
            used.emplace(to_string(i-1)+to_string(j-1)+to_string(k));
        }
        if(i>0 && k>0 && used.find(to_string(i-1)+to_string(j)+to_string(k-1))==used.end()){
            q.push(make_pair(a[i-1]+b[j]+c[k-1], to_string(i-1)+to_string(j)+to_string(k-1)));
            used.emplace(to_string(i-1)+to_string(j)+to_string(k-1));
        }
        if(j>0 && k>0 && used.find(to_string(i)+to_string(j-1)+to_string(k-1))==used.end()){
            q.push(make_pair(a[i]+b[j-1]+c[k-1], to_string(i)+to_string(j-1)+to_string(k-1)));
            used.emplace(to_string(i)+to_string(j-1)+to_string(k-1));
        }
        if(i>0 && j>0 && k>0 && used.find(to_string(i-1)+to_string(j-1)+to_string(k-1))==used.end()){
            q.push(make_pair(a[i-1]+b[j-1]+c[k-1], to_string(i-1)+to_string(j-1)+to_string(k-1)));
            used.emplace(to_string(i-1)+to_string(j-1)+to_string(k-1));
        }

        //print(used);

        cout << item.first << endl;
    }


    return 0;
}


Compiler output:
a.cc: In function 'void print(const C&, std::ostream&)':
a.cc:30:46: error: 'ostream_iterator' is not a member of 'std'
   30 |   std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, ", "));
      |                                              ^~~~~~~~~~~~~~~~
a.cc:17:1: note: 'std::ostream_iterator' is defined in header '<iterator>'; this is probably fixable by adding '#include <iterator>'
   16 | #include <unordered_set>
  +++ |+#include <iterator>
   17 | 
a.cc:30:85: error: expected '(' before '>' token
   30 |   std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, ", "));
      |                                                                                     ^
      |                                                                                     (