Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll,ll> P;
typedef pair<P,P> PP;
typedef pair<ll,P> lP;
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define fr(i,n) for(int i=0;i<(n);i++)
#define Fr(i,n) for(int i=0;i++<(n);)
#define ifr(i,n) for(int i=(n)-1;i>=0;i--)
#define iFr(i,n) for(int i=(n);i>0;i--)

template <class T>struct PairingHeap{
    struct node{
        T key;
        node *head,*next;
        explicit node(T key):key(key),head(nullptr),next(nullptr){}
    };
    node *r;
    PairingHeap():r(nullptr){}
    PairingHeap(node* r):r(r){}
    node* merge(node *a,node *b){
        if(!a||!b) return a?a:b;
        if(a->key<b->key){
            b->next=a->head,a->head=b;
            return a;
        }
        else{
            a->next=b->head,b->head=a;
            return b;
        }
    }
    node* mergelist(node *s){
        if(!s) return nullptr;
        node h(T());
        while(s){
            node *a=s,*b=nullptr;
            s=s->next;a->next=nullptr;
            if(s) b=s,s=s->next,b->next=nullptr;
            a=merge(a,b);a->next=h.next;
            h.next=a;
        }
        node *t=h.next;
        s=t;
        t=t->next;
        while(t){
            node *u=t;
            t=t->next;
            s=merge(s,u);
        }
        return s;
    }
    T top(){
        if(!r) return numeric_limits<T>::min();
        return r->key;
    }
    void pop(){
        if(!r) return;
        r=mergelist(r->head);
    }
    void push(T k){r=merge(r,new node(k));}
    bool empty(){return !r;}
};

int main(){
    ll x,y,z,k,a[1010],b[1010],c[1010],ans,S,X,Y,Z,cnt=0;
    cin>>x>>y>>z>>k;
    fr(i,x) cin>>a[i];
    fr(i,y) cin>>b[i];
    fr(i,z) cin>>c[i];
    sort(a,a+x);
    sort(b,b+y);
    sort(c,c+z);
    reverse(a,a+x);
    reverse(b,b+y);
    reverse(c,c+z);
    ans=a[0]+b[0]+c[0];
    ifr(i,x) a[i]=a[0]-a[i];
    ifr(i,y) b[i]=b[0]-b[i];
    ifr(i,z) c[i]=c[0]-c[i];
    PairingHeap<PP> pq;
    pq.push(mp(mp(0,0),mp(0,0)));
    map<lP,bool> lPb;
    while(cnt<k){
        PP pp=pq.top();
        pq.pop();
        if(lPb[mp(pp.ft.sd,pp.sd)]) continue;
        cout<<ans-pp.ft.ft<<endl;
        cnt++;
        lPb[mp(pp.ft.sd,pp.sd)]=1;
        S=pp.ft.ft;X=pp.ft.sd;Y=pp.sd.ft;Z=pp.sd.sd;
        if(X<x-1) pq.push(mp(mp(a[X+1]+b[Y]+c[Z],X+1),mp(Y,Z)));
        if(Y<y-1) pq.push(mp(mp(a[X]+b[Y+1]+c[Z],X),mp(Y+1,Z)));
        if(Z<z-1) pq.push(mp(mp(a[X]+b[Y]+c[Z+1],X),mp(Y,Z+1)));
    }
}


Compiler output:
a.cc: In member function 'PairingHeap<T>::node* PairingHeap<T>::mergelist(node*)':
a.cc:39:15: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
   39 |         node h(T());
      |               ^~~~~
a.cc:39:15: note: replace parentheses with braces to declare a variable
   39 |         node h(T());
      |               ^~~~~
      |               -
      |               {   -
      |                   }
a.cc: In instantiation of 'PairingHeap<T>::node* PairingHeap<T>::mergelist(node*) [with T = std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> >]':
a.cc:63:11:   required from 'void PairingHeap<T>::pop() [with T = std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> >]'
   63 |         r=mergelist(r->head);
      |           ^~~~~~~~~
a.cc:90:15:   required from here
   90 |         pq.pop();
      |         ~~~~~~^~
a.cc:44:36: error: request for member 'next' in 'h', which is of non-class type 'PairingHeap<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::node(std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > (*)())'
   44 |             a=merge(a,b);a->next=h.next;
      |                                  ~~^~~~
a.cc:45:15: error: request for member 'next' in 'h', which is of non-class type 'PairingHeap<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::node(std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > (*)())'
   45 |             h.next=a;
      |             ~~^~~~
a.cc:47:19: error: request for member 'next' in 'h', which is of non-class type 'PairingHeap<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::node(std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > (*)())'
   47 |         node *t=h.next;
      |                 ~~^~~~