atcoder#ARC134D. [ARC134D] Concatenate Subsequences
[ARC134D] Concatenate Subsequences
Score : points
Problem Statement
Given is an integer sequence of length .
Snuke is going to make a sequence using a non-empty (not necessarily contiguous) subsequence of . The sequence will be made by extracting and concatenating the -th, -th, , -th, -th, , -th elements of in this order.
Find the lexicographically smallest sequence that Snuke can make.
Lexicographical order on sequences
Here is the algorithm to determine the lexicographical order between different sequences and .
Below, let denote the -th element of . Also, if is lexicographically smaller than , we will denote that fact as ; if is lexicographically larger than , we will denote that fact as .
- Let be the smaller of the lengths of and . For each , we check whether and are the same.
- If there is an such that , let be the smallest such . Then, we compare and . If is smaller than in numerical order, we determine that and quit; if is greater than , we determine that and quit.
- If there is no such that , we compare the lengths of and . If is shorter than , we determine that and quit; if is longer than , we determine that and quit.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the lexicographically smallest sequence that Snuke can make.
3
2 1 3 1 2 2
1 2
- We choose .
- Then, the resulting sequence will be , the lexicographically smallest possible result.
10
38 38 80 62 62 67 38 78 74 52 53 77 59 83 74 63 80 61 68 55
38 38 38 52 53 77 80 55
12
52 73 49 63 55 74 35 68 22 22 74 50 71 60 52 62 65 54 70 59 65 54 60 52
22 22 50 65 54 52