Prahova Valley

Time limit: 1.5s Memory limit: 64MB Input: Output:

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 xyx \gets y):

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 RR 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 RR and the algorithm.
Your task is to find the lexicographically smallest sequence VV of NN integers such that applying the algorithm to VV produces the sequence RR.

Input data

The first line of the input contains the integer NN, representing the number of integers in the RR sequence.

The second line of the input contains NN space-separated integers R1,R2,,RNR_1, R_2, \ldots, R_N, representing the elements of the RR 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 NN space-separated integers V1,V2,,VNV_1, V_2, \ldots, V_N, representing the lexicographically smallest sequence such that applying the algorithm to it produces sequence RR.

Constraints and clarifications

  • 1N5 000 0001 \leq N \leq 5 \ 000 \ 000
  • i+1RiN+1i + 1 \leq R_i \leq N + 1, for all 1iN1 \leq i \leq N
  • We guarantee that, for the given input, there is always a solution.
  • 1Vi2501 \leq V_i \leq 2^{50}, for all 1iN1 \leq i \leq N
  • The algorithm produces the same result regardless of the initial values of RiR_i, meaning we can safely assume Ri=0R_i = 0 for all 1iN1 \leq i \leq N.
  • We define array AA as being lexicographically smaller than array BB (both of size KK) if, for some index pp where 0p<K0 \le p < K, the prefixes of length pp are identical (Ai=BiA_i = B_i for all 1ip1 \le i \le p) and the (p+1)(p+1)-th elements satisfy Ap+1<Bp+1A_{p+1} < B_{p+1}.
# Score Constraints
0 0 Examples
1 15 N100, Ri=N+1N \leq 100, \ R_i = N + 1 for all 1iN1 \leq i \leq N
2 15 N100, Ri=i+1N \leq 100, \ R_i = i + 1 for all 1iN1 \leq i \leq N
3 25 N5000N \leq 5000
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 {1,2,4,2,1}\{1, 2, 4, 2, 1\}.

  • i=5i = 5: The pointer jj is initialized to 66. Since jN+1j \ge N+1, the inner loop condition is not met, resulting in R5=6R_5 = 6.
  • i=4i = 4: Initializing j=5j = 5, we find that V5 % V40V_5 \ \% \ V_4 \neq 0. The loop terminates immediately, setting R4=5R_4 = 5.
  • i=3i = 3: Starting with j=4j = 4, we check V4 % V30V_4 \ \% \ V_3 \neq 0, thus R3=4R_3 = 4.
  • i=2i = 2: We begin with j=3j = 3. Since V3 % V2=0V_3 \ \% \ V_2 = 0, we update jR3=4j \gets R_3 = 4. Subsequently, as V4 % V2=0V_4 \ \% \ V_2 = 0, we update jR4=5j \gets R_4 = 5. Finally, V5 % V20V_5 \ \% \ V_2 \neq 0, yielding R2=5R_2 = 5.
  • i=1i = 1: Starting with j=2j = 2, we perform the following updates:
    • V2 % V1=0    jR2=5V_2 \ \% \ V_1 = 0 \implies j \gets R_2 = 5.
    • V5 % V1=0    jR5=6V_5 \ \% \ V_1 = 0 \implies j \gets R_5 = 6.

The loop terminates as j=6j=6, resulting in R1=6R_1 = 6.

We can show that this is the lexicographically smallest solution for the given input.

Log in or sign up to be able to send submissions!