luogu#P5184. [COCI 2009/2010 #2] PASIJANS

[COCI 2009/2010 #2] PASIJANS

Background

The original time limit was 5 s. The time limit here has been adjusted according to the speed of Luogu's judge machines.

Problem Description

Translated from COCI 2009.11 T6 “PASIJANS

You are given NN stacks that already contain numbers (the number of elements in each stack may be different), and an empty “answer queue”. Each time, you may “pop the top element of one stack and append it to the end of the answer queue”, until all stacks become empty. Find the lexicographically smallest possible answer queue.

If two answer queues a,ba, b (from front to back) have the same first i1i-1 numbers, and ai<bia_i<b_i, then we say the lexicographical order of aa is smaller than that of bb.

Input Format

The first line contains an integer NN.
In the next NN lines, the first integer is LL, the number of elements in the stack. Then LL integers are given in order from the top of the stack to the bottom.

Output Format

Output L\sum L integers, representing the lexicographically smallest answer queue.

3
1 2
1 100
1 1
1 2 100

2
5 10 20 30 40 50
2 28 27
10 20 28 27 30 40 50
2
3 5 1 2
3 5 1 1
5 1 1 5 1 2

Hint

1N1000,1\le N\le 1000, 1L10001\le L\le 1000

Translated by ChatGPT 5