Problem:
Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data2 = System.Tuple<long, int[]>;
using static ABC123.Util;

namespace ABC123
{
    class Program
    {
        static void Main(string[] args)
        {
            // FiveAntennas(); // 25min
            // FiveDishes(); // 35min
            // FiveTransportation(); // 1h11m
            //Cake123_old();
            Cake123_pq2();
        }

        public static void Cake123_pq2()
        {
            int[] N = new int[3];
            int[] tmp = ReadIntArray();
            for (int i = 0; i < 3; i++)
            {
                N[i] = tmp[i];
            }
            int K = tmp[3];

            List<long[]> v = new List<long[]>();
            v.Add(ReadLongArray());
            v.Add(ReadLongArray());
            v.Add(ReadLongArray());

            ReverseSort(v[0]);
            ReverseSort(v[1]);
            ReverseSort(v[2]);

            PriorityQueue<Data2> que = new PriorityQueue<Data2>(K * 3, 
                (a,b) => b.Item1.CompareTo(a.Item1));

            HashSet<Data2> set = new HashSet<Data2>(
                new EqualityComparer<Data2>(
                    (a,b) => a.Item1 == b.Item1 && a.Item2.SequenceEqual(b.Item2),
                    (obj) => obj.Item1.GetHashCode() ^ obj.Item2[0] ^ obj.Item2[1] ^ obj.Item2[2]));

            Data2 first = new Data2(v[0][0] + v[1][0] + v[2][0], new int[] { 0, 0, 0 });
            que.Push(first);

            for (int k = 0; k < K; k++)
            {
                var data = que.Pop();
                Console.WriteLine(data.Item1);

                // 次の3候補
                for (int i = 0; i < 3; i++)
                {
                    if (data.Item2[i] + 1 < N[i])
                    {
                        var sum = data.Item1
                            - v[i][data.Item2[i]]
                            + v[i][data.Item2[i] + 1];
                        int[] num = new int[3];
                        data.Item2.CopyTo(num, 0);
                        num[i]++;
                        var d = new Data2(sum, num);

                        if (!set.Contains(d))
                        {
                            set.Add(d);
                            que.Push(d);
                        }
                    }
                }
            }
        }

        public static void Cake123_old()
        {
            int[] tmp = ReadIntArray();
            int k = tmp[3];
            int x = Math.Min(tmp[0],k);
            int y = Math.Min(tmp[1],k);
            int z = Math.Min(tmp[2],k);

            long[] aa = ReadLongArray();
            long[] bb = ReadLongArray();
            long[] cc = ReadLongArray();

            // aとbの合計だけを計算してeとする
            int e = x*y;
            long[] ee = new long[e];
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    ee[y * i + j] = aa[i] + bb[j];
                }
            }

            ReverseSort(ee);
            int e2 = Math.Min(e, k);
            long[] ans = new long[e2*z];
            for (int i = 0; i < e2; i++)
            {
                for (int j = 0; j < z; j++)
                {
                    ans[z * i + j] = ee[i] + cc[j];
                }
            }

            ReverseSort(ans);
            for (int i = 0; i < k; i++)
            {
                Console.WriteLine(ans[i]);
            }
        }

        public static void FiveTransportation()
        {
            long n = ReadLong();
            long[] time = new long[5];
            for (int i = 0; i < 5; i++)
            {
                time[i] = ReadLong();
            }

            long min = time.Min();

            long ans = (long)Math.Ceiling(n * 1.0m / min) + 4;

            Console.WriteLine(ans);
        }

        public static void FiveDishes()
        {
            int[] dishes = new int[5];
            for(int i = 0; i < 5; i++)
            {
                dishes[i] = ReadInt();
            }

            dishes = dishes.OrderByDescending(i => i % 10 == 0 ? 10 : i % 10).ToArray();

            int ans = 0;
            for(int i = 0; i < 4; i++)
            {
                ans += (int)Math.Ceiling(dishes[i] / 10m) * 10;
            }

            ans += dishes[4];

            Console.WriteLine(ans);
        }

        public static void FiveAntennas()
        {
            int[] antennas = new int[5];
            for(int i = 0; i < 5; i++)
            {
                antennas[i] = ReadInt();
            }

            int k = ReadInt();

            string ans = "Yay!";
            if (antennas[0] + k < antennas[4])
            {
                ans = ":(";
            }

            Console.WriteLine(ans);
        }

        #region util

       

        #endregion
    }

    public class Util
    {
        public static void ReverseSort<T>(T[] array)
        {
            Array.Sort<T>(array);
            Array.Reverse(array);
        }

        public static int[] ReadBinaryString()
        {
            return Console.ReadLine().Select(c => int.Parse(c.ToString())).ToArray();
        }

        public static bool[] ReadBoolArray()
        {
            return Console.ReadLine().Select(c => c == '1').ToArray();
        }

        public static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        public static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }

        public static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }

        public static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
    }

    /// <summary>
    /// HashSetのコンストラクタに渡します。
    /// EqualsとGetHashCodeを提供します。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class EqualityComparer<T> : IEqualityComparer<T>
    {
        private Func<T, T, bool> _equals;
        private Func<T, int> _getHashCode;

        public EqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
        {
            _equals = equals;
            _getHashCode = getHashCode;
        }

        public bool Equals(T x, T y)
        {
            return _equals(x, y);
        }

        public int GetHashCode(T obj)
        {
            return _getHashCode(obj);
        }
    }

    public class PriorityQueue<T> where T : IComparable
    {
        private Comparison<T> _comparison = null;
        private IComparer<T> _comparer = null;
        private int _type = 0;

        private T[] _heap;
        private int _sz = 0;

        private int _count = 0;

        /// <summary>
        /// Priority Queue with custom comparer
        /// </summary>
        public PriorityQueue(int maxSize, IComparer<T> comparer)
        {
            _heap = new T[maxSize];
            _comparer = comparer;
        }

        /// <summary>
        /// Priority queue
        /// </summary>
        /// <param name="maxSize">max size</param>
        /// <param name="type">0: asc, 1:desc</param>
        public PriorityQueue(int maxSize, int type = 0)
        {
            _heap = new T[maxSize];
            _type = type;
        }

        public PriorityQueue(int maxSize, Comparison<T> comparison)
        {
            _heap = new T[maxSize];
            _comparison = comparison;
        }


        private int Compare(T x, T y)
        {
            if (_comparison != null)
                return _comparison(x, y);
            if (_comparer != null) return _comparer.Compare(x, y);
            return _type == 0 ? x.CompareTo(y) : y.CompareTo(x);
        }

        public void Push(T x)
        {
            _count++;

            //node number
            var i = _sz++;

            while (i > 0)
            {
                //parent node number
                var p = (i - 1) / 2;

                if (Compare(_heap[p], x) <= 0) break;

                _heap[i] = _heap[p];
                i = p;
            }

            _heap[i] = x;
        }

        public T Pop()
        {
            _count--;

            T ret = _heap[0];
            T x = _heap[--_sz];

            int i = 0;
            while (i * 2 + 1 < _sz)
            {
                //children
                int a = i * 2 + 1;
                int b = i * 2 + 2;

                if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b;

                if (Compare(_heap[a], x) >= 0) break;

                _heap[i] = _heap[a];
                i = a;
            }

            _heap[i] = x;

            return ret;
        }

        public int Count()
        {
            return _count;
        }

        public T Peek()
        {
            return _heap[0];
        }

        public bool Contains(T x)
        {
            for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true;
            return false;
        }

        public void Clear()
        {
            while (this.Count() > 0) this.Pop();
        }

        public IEnumerator<T> GetEnumerator()
        {
            var ret = new List<T>();

            while (this.Count() > 0)
            {
                ret.Add(this.Pop());
            }

            foreach (var r in ret)
            {
                this.Push(r);
                yield return r;
            }
        }

        public T[] ToArray()
        {
            T[] array = new T[_sz];
            int i = 0;

            foreach (var r in this)
            {
                array[i++] = r;
            }

            return array;
        }
    }
}