A backstage manager is assembling the final performance roster for a variety show, one act at a time. There are n acts, and they arrive in a fixed order, numbered 0 through n-1 by arrival. When the i-th act arrives, the manager is told a value describing that act and the exact slot in the roster built so far where it must be inserted; every act already occupying that slot or a later one shifts one place to the right to make room. After all n acts have arrived, report the finished roster from left to right.
Line 1: one integer n -- the number of acts.
Line 2: n integers values[0] ... values[n-1] -- the value carried by the act that arrives i-th (0-indexed).
Line 3: n integers positions[0] ... positions[n-1] -- the 0-indexed slot at which that act is inserted into the roster at the moment it arrives.
Print the n values of the final roster, left to right, space-separated on a single line.
1 <= n <= 1000 0 <= values[i] <= 1000 0 <= positions[i] <= i, for every i from 0 to n-1 (so positions[i] is always a valid insertion slot for the roster's length just before the i-th act is inserted).
Example 1
Input
4 4 7 2 9 0 1 0 2
Expected
2 4 9 7
Explanation
Start with an empty roster []. Insert 4 at slot 0 -> [4]. Insert 7 at slot 1 -> [4,7]. Insert 2 at slot 0 -> [2,4,7]. Insert 9 at slot 2 -> [2,4,9,7]. Final roster: "2 4 9 7".
Example 2
Input
1 5 0
Expected
5
Explanation
The only act arrives and is inserted at slot 0 of the empty roster, giving the single-element roster "5".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →