K-Way Merge of Sorted Lists (min-heap based)

EXTREME · C · 5000ms

Now Imran has K different class sections, each already internally sorted by height, and he needs all of them merged into one grand sorted line for the final march-past. Read K sorted arrays (given consecutively). Merge them all into a single sorted array. Use a min-heap of size K (comparing current fronts) for O(total_elements * log K), not concatenating and sorting everything from scratch.

Input

Line1: K. Then for each of the K lists: a line with its size, then a line with its sorted elements.

Output

All elements merged into one fully sorted sequence, space-separated.

Constraints

1 ≤ K ≤ 1000, total elements ≤ 10^5

Sample Input

2
3
1 4 5
3
2 3 6

Sample Output

1 2 3 4 5 6
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

K-Way Merge of Sorted Lists (min-heap based) · DIU ContestHub