#P5797. [SEERC 2019] Max or Min

[SEERC 2019] Max or Min

Description

Kevin has nn integers a1,a2,,ana_1, a_2, \dots, a_n, arranged in a circular order. On the circle, the numbers aia_i and ai+1a_{i+1} (1i<n)(1 \leq i < n) are adjacent, and a1a_1 and ana_n are adjacent. Therefore, each number has exactly two adjacent numbers.

In 1 minute, Kevin can change aia_i to the minimum or the maximum among these three numbers: aia_i and its two adjacent numbers. For example, if ai=5a_i = 5 and its two adjacent numbers are 33 and 22, then after changing aia_i to the minimum, aia_i becomes 22; if Kevin changes aia_i to the maximum, then aia_i is still 55.

For each integer xx from 11 to mm, compute the shortest time to make all numbers on the circle become xx.

Input Format

The first line contains two integers nn and mm $(3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5)$, representing the number of integers on the circle and the range of xx for which answers are required.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1aim)(1 \leq a_i \leq m), representing the numbers on the circle.

Output Format

Output mm integers. The ii-th integer is the minimum number of minutes needed to make all numbers on the circle equal to ii. If it is impossible to make all integers become ii, output 1-1 for the ii-th integer.

7 5
2 5 1 1 2 3 2
5 5 7 -1 6

Hint

To make all integers on the circle become 22, Kevin needs at least 55 minutes. One feasible sequence of operations is:

  1. Change a6a_6 to the minimum among it and its adjacent numbers, i.e., change it to 22.
  2. Change a4a_4 to the maximum among it and its adjacent numbers, i.e., change it to 22.
  3. Change a3a_3 to the maximum among it and its adjacent numbers, i.e., change it to 55.
  4. Change a2a_2 to the minimum among it and its adjacent numbers, i.e., change it to 22.
  5. Change a3a_3 to the minimum among it and its adjacent numbers, i.e., change it to 22.

Translated by ChatGPT 5