Andrei's route to the IIOT finals begins in the famous Prahova Valley! During the road trip, he got bored and came up with the following algorithm (note: we denote the assignment operation as ):
for i ← N … 1 do
j ← i + 1
while j ≤ N and Vⱼ % Vᵢ = 0 do
j ← Rⱼ
end while
Rᵢ ← j
end for
During a brief stop at the famous Peleș Castle in Sinaia, he wrote down all the house numbers he found around the city and applied the aforementioned algorithm. He wrote down the resulting sequence on a separate sheet of paper.
Task
When Andrei gets into the car, he realizes that he has lost all the pages of his notebook except the ones containing sequence and the algorithm.
Your task is to find the lexicographically smallest sequence of integers such that applying the algorithm to produces the sequence .
Input data
The first line of the input contains the integer , representing the number of integers in the sequence.
The second line of the input contains space-separated integers , representing the elements of the sequence.
Due to the high volume of input data, we recommend adding the following lines before reading any input:
ios_base::sync_with_stdio(false);
cin.tie(0);
Output data
The first line of the output must contain space-separated integers , representing the lexicographically smallest sequence such that applying the algorithm to it produces sequence .
Constraints and clarifications
- , for all
- We guarantee that, for the given input, there is always a solution.
- , for all
- The algorithm produces the same result regardless of the initial values of , meaning we can safely assume for all .
- We define array as being lexicographically smaller than array (both of size ) if, for some index where , the prefixes of length are identical ( for all ) and the -th elements satisfy .
| # | Score | Constraints |
|---|---|---|
| 0 | 0 | Examples |
| 1 | 15 | for all |
| 2 | 15 | for all |
| 3 | 25 | |
| 4 | 45 | No further restrictions. |
Example
stdin
5
6 5 4 5 6
stdout
1 2 4 2 1
Explanation
We will apply the algorithm on .
- : The pointer is initialized to . Since , the inner loop condition is not met, resulting in .
- : Initializing , we find that . The loop terminates immediately, setting .
- : Starting with , we check , thus .
- : We begin with . Since , we update . Subsequently, as , we update . Finally, , yielding .
- : Starting with , we perform the following updates:
- .
- .
The loop terminates as , resulting in .
We can show that this is the lexicographically smallest solution for the given input.