K Smallest Pair Sums

medium heap arrays sorting 1000 ms · 256 MB

K Smallest Pair Sums

You are given two arrays a (of length n) and b (of length m), each sorted in non-decreasing order.

Consider all n·m index pairs (i, j) with 1 ≤ i ≤ n and 1 ≤ j ≤ m, and the value a_i + b_j of each pair. Print the k smallest of these values in non-decreasing order. Different index pairs count separately, even if their sums (or their elements) are equal.

Input

The first line contains three integers n, m and k (1 ≤ n, m ≤ 10^5, 1 ≤ k ≤ min(n·m, 2·10^5)).

The second line contains n integers a_1, a_2, …, a_n (−10^9 ≤ a_i ≤ 10^9, a_1 ≤ a_2 ≤ … ≤ a_n).

The third line contains m integers b_1, b_2, …, b_m (−10^9 ≤ b_j ≤ 10^9, b_1 ≤ b_2 ≤ … ≤ b_m).

Output

Print k integers on one line, separated by spaces: the k smallest pair sums in non-decreasing order.

Notes

In the first example the nine pair sums are 3, 5, 7, 9, 11, 13, 13, 15, 17, and the five smallest are printed.

In the second example a = [1, 1, 2] and b = [1, 2, 3]. The sum 2 comes from two different pairs, (1, 1) and (2, 1), so it appears twice; the sum 3 comes from three pairs and appears three times.

Example 1

Input

3 3 5
1 7 11
2 4 6

Output

3 5 7 9 11

Example 2

Input

3 3 5
1 1 2
1 2 3

Output

2 2 3 3 3

Hints · reveal one at a time if you're stuck

Hint 1

There can be up to 10^10 pairs, so do not list them. Both arrays are sorted, so look at the matrix of sums a_i + b_j: which pairs can possibly be among the k smallest, and where do they sit?

Hint 2

Think of the sums as a k-way merge of sorted rows. Keep a min-heap holding the current candidate from each row, pop the smallest, and then advance within that row. Only the first few rows can ever matter.

Hint 3

Each pair must enter the heap at most once, or repeated sums appear in the output. Make each pair have a single predecessor, or track pushed index pairs rather than sums, since equal sums from different pairs are all printed.

report an issue Prefer your own editor? sooks prep 45 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.