#P9770. Inverse KMP

    ID: 22916 Type: Default 1000ms 256MiB

Inverse KMP

Inverse KMP

Walk Alone is a master of strings, but he has grown bored with traditional algorithms such as the KMP algorithm. He now poses the "Inverse KMP" challenge:

Given an integer sequence \(a\) of length \(n\) where for each \(1\le i\le n\), \(0\le a_i < i\), construct another integer sequence \(s\) of length \(n\) where every element \(s_i\) satisfies \(1\le s_i\le n\). The sequence \(s\) must satisfy the condition that for every integer \(i\) (\(1\le i\le n\)) and every integer \(j\) (\(1\le j\le a_i\)), the following holds:

\[ s_j = s_{i-a_i+j} \]

Among all sequences \(s\) that satisfy the above condition, you are required to choose one in which the number of distinct elements is maximized. It is guaranteed that at least one valid sequence exists.

inputFormat

The first line contains a single integer \(n\) (\(1\le n\le 1000\)), denoting the length of the sequence.

The second line contains \(n\) space-separated integers representing the sequence \(a\), where for each \(i\) (\(1\le i\le n\)), \(0\le a_i < i\).

outputFormat

Output \(n\) space-separated integers denoting the sequence \(s\) which satisfies the conditions and maximizes the number of distinct elements.

sample

3
0 0 0
1 2 3