Language: C++

#include <iostream>
#include <list>
#include <algorithm>

int main() {
  int X,Y,Z,K;
  std::cin >> X >> Y >> Z >> K;
  using Type = std::list<unsigned long long>;
  Type A(X);
  Type B(Y);
  Type C(Z);
  for(auto &a:A)
    std::cin >> a;
  for(auto &b:B)
    std::cin >> b;
  for(auto &c:C)
    std::cin >> c;

  std::sort(A.begin(),A.end(),std::greater<unsigned long long>());
  std::sort(B.begin(),B.end(),std::greater<unsigned long long>());
  std::sort(C.begin(),C.end(),std::greater<unsigned long long>());
  for(int i=0;i<K;i++){
    std::cout << A.front() + B.front() + C.front() << std::endl;
    bool a_is_end = A.size() == 1;
    bool b_is_end = B.size() == 1;
    bool c_is_end = C.size() == 1;
    if((b_is_end || A.front() <= B.front()) &&
       (c_is_end || A.front() <= C.front()) && !a_is_end)
      A.pop_front();
    else if((a_is_end || B.front() < A.front()) &&
       (c_is_end || B.front() <= C.front()) && !b_is_end)
      B.pop_front();
    else
      C.pop_front();
  }
  return 0;
}


Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = _List_iterator<long long unsigned int>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<long long unsigned int> >]':
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = _List_iterator<long long unsigned int>; _Compare = greater<long long unsigned int>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:19:12:   required from here
   19 |   std::sort(A.begin(),A.end(),std::greater<unsigned long long>());
      |   ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1906:50: error: no match for 'operator-' (operand types are 'std::_List_iterator<long long unsigned int>' and 'std::_List_iterator<long long unsigned int>')
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/string:48,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:618:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  618 |     operator-(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:618:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:1906:50: note:   'std::_List_iterator<long long unsigned int>' is not derived from 'const std::reverse_iterator<_Iterator>'
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1790 |     operator-(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:1906:50: note:   'std::_List_iterator<long long unsigned int>' is not derived from 'const std::move_iterator<_IteratorL>'
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~