## Editorial for COCI '23 Contest 1 #4 Kocke

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Prepared by: Toni Brajko

Let's observe the part of the sequence that is strictly positive. Notice that it will form a continuous segment, so we can disregard the zeroes before and after it. Denote as the number of ways to form a sequence of length (without zeroes). To account in the zeroes, we see that we can place a sequence of length on ways () in the initial sequence. Therefore, the solution is given with the following expression:

It remains to calculate for each .

We can generate every possible sequence of moves and use it to calculate the final sequence we get. To count the number of different sequences, we can, for example, sort them (and then if two sequences are equal they will necessarily be neighboring). The complexity of this algorithm is or , depending on the implementation, and is sufficient to solve the first subtask.

Let's observe creating the sequence backwards. We have as the initial sequence. For each number from to in decreasing order, we put number next to . If we put a number on an already existing position, the sequence won't change (because the larger number will come after in the original process, and it will "overwrite" the lower one). If we want to put a number on a place where we haven't written any numbers yet (i.e. on a zero, which is not included in current sequence because we disregarded it), the sequence will extend by one spot either to the left or to the right. Notice that this process is equivalent to the original one.

We use dynamic programming to solve the rest of the task. Let be the number of achievable sequences of length , such that is the minimum of the sequence. Notice that the minimum of any such sequence will be one of its endpoints. This follows directly from the aforementioned description. WLOG, assume the smallest element in the sequence is the right endpoint. We have two possibilities:

• The second smallest element of the sequence is also on the right end, one place to the left from the smallest element. It can be any number of the form , for .
• The second smallest element of the sequence is the left endpoint. It can be any number of the form , for .

Detailed analysis of these expressions is left as an exercise for the reader. Now the following holds:

The complexity of each transition is , and there are a total of states, so the final complexity of direct implementation of this formula is , and is sufficient to solve the second and the third subtask.

Finally, we can speed up each transition by using prefix sums by parity, since the independent term is always . Final complexity is . Notice that it does not depend on the sequence length from the task statement at all. For details of implementation and base cases refer to the official solution.