Language: C++

#include <queue>
#include <memory>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std; 

int
main(int argc, char const *argv[])
{
    uint64_t x, y, z, k;
    cin >> x >> y >> z >> k;

    vector<uint64_t> a(x);
    vector<uint64_t> b(y);
    vector<uint64_t> c(z);
    uint64_t v;

    for (uint64_t i = 0; i < x; ++i) {
	cin >> v;
	a[i] = v;
    }
    for (uint64_t i = 0; i < y; ++i) {
	cin >> v;
	b[i] = v;
    }
    for (uint64_t i = 0; i < z; ++i) {
	cin >> v;
	c[i] = v;
    }

    sort(a.begin(),a.end(),std::greater<uint64_t>());
    sort(b.begin(),b.end(),std::greater<uint64_t>());
    sort(c.begin(),c.end(),std::greater<uint64_t>());

    list<vector<uint64_t>> lst(0);
    list<vector<uint64_t>> result(0);

    vector<uint64_t> first = {0, 0, 0, a[0] + b[0] + c[0]};
 
    lst.push_back(first);
    while (true) {
    	auto v = lst.front();
	result.push_back(v);
	lst.pop_front();
	if (result.size() > 1) {
	result.sort([](const vector<uint64_t> &a,const vector<uint64_t> &b){
	    return a[3] > b[3];
	});
	result.unique([](const vector<uint64_t> &a,const vector<uint64_t> &b){
	    return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
	});
	}
	if (result.size() > k) break;
    	// cout << v[0] << "," << v[1] << "," << v[2] << ": " << v[3] << endl;
    	// cout << a[v[0]] << " + " << b[v[1]] << " + " << c[v[2]] << " = " << v[3] << endl;
	if (v[0] + 1 < x) lst.push_back({v[0] + 1, v[1], v[2], v[3] + a[(v[0]+1)] - a[v[0]]});
    	if (v[1] + 1 < y) lst.push_back({v[0], v[1] + 1, v[2], v[3] + b[(v[1]+1)] - b[v[1]]});
    	if (v[2] + 1 < z) lst.push_back({v[0], v[1], v[2] + 1, v[3] + c[(v[2]+1)] - c[v[2]]});
	lst.sort([](const vector<uint64_t> &a,const vector<uint64_t> &b){
	    return a[3] > b[3];
	});

	lst.unique([](const vector<uint64_t> &a,const vector<uint64_t> &b){
	    return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
	});
    }

    for (uint64_t i = 0; i < k; ++i) {
    	auto v = result.front();
	result.pop_front();
    	cout << v[3] << endl;
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:14:5: error: 'cin' was not declared in this scope
   14 |     cin >> x >> y >> z >> k;
      |     ^~~
a.cc:7:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    6 | #include <functional>
  +++ |+#include <iostream>
    7 | 
a.cc:38:5: error: 'list' was not declared in this scope
   38 |     list<vector<uint64_t>> lst(0);
      |     ^~~~
a.cc:7:1: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
    6 | #include <functional>
  +++ |+#include <list>
    7 | 
a.cc:38:25: error: expected primary-expression before '>' token
   38 |     list<vector<uint64_t>> lst(0);
      |                         ^~
a.cc:38:28: error: 'lst' was not declared in this scope
   38 |     list<vector<uint64_t>> lst(0);
      |                            ^~~
a.cc:39:25: error: expected primary-expression before '>' token
   39 |     list<vector<uint64_t>> result(0);
      |                         ^~
a.cc:39:28: error: 'result' was not declared in this scope
   39 |     list<vector<uint64_t>> result(0);
      |                            ^~~~~~
a.cc:74:9: error: 'cout' was not declared in this scope
   74 |         cout << v[3] << endl;
      |         ^~~~
a.cc:74:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:74:25: error: 'endl' was not declared in this scope
   74 |         cout << v[3] << endl;
      |                         ^~~~
a.cc:7:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    6 | #include <functional>
  +++ |+#include <ostream>
    7 |