SDE Path

Merge K Sorted Arrays

Hard

Merge K Sorted Arrays

Merge K sorted arrays into a single sorted array using a min-heap.

Input format

The first line contains K. The next K lines contain sorted arrays in the format "len v1 v2 ... vlen".

Output format

Print the merged sorted array, space-separated.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
8
7 -35 -31 -19 -17 15 41 45
4 -45 -44 -33 -17
8 -25 -7 0 5 15 29 36 47
8 -48 -48 -22 -16 -1 7 33 37
2 -26 -8
7 -33 -31 -11 7 29 46 49
3 -29 19 34
5 -18 -10 -3 26 41
Expected output
-48 -48 -45 -44 -35 -33 -33 -31 -31 -29 -26 -25 -22 -19 -18 -17 -17 -16 -11 -10 -8 -7 -3 -1 0 5 7 7 15 15 19 26 29 29 33 34 36 37 41 41 45 46 47 49
Example 2
Input
3
2 -47 -23
5 -28 -14 9 20 36
9 -47 -26 -25 -22 -21 -12 34 47 50
Expected output
-47 -47 -28 -26 -25 -23 -22 -21 -14 -12 9 20 34 36 47 50