#P11068. Staircase Flip Challenge

    ID: 13123 Type: Default 1000ms 256MiB

Staircase Flip Challenge

Staircase Flip Challenge

Students perform their daily exercises by walking down a special staircase. The staircase has \(n+1\) steps. For the \(i\)-th step (where \(1 \leq i \leq n+1\)), it has a turning value \(a_i\) where \(a_i \in \{0,1\}\), with the property that \(a_{n+1}=0\) (the last step always has a value 0).

When moving from step \(i\) to step \(i+1\), the time required is determined as follows:

  • If \(a_{i+1}=a_i\), you simply walk down to the next step, which takes 1 second.
  • If \(a_{i+1}\neq a_i\), you must first spend 1 second to perform a full circle turn to flip the current step's value to \(1-a_i\), and then spend another second to walk down to the next step (totaling 2 seconds).

The students perform this exercise for \(k\) days, starting each day from the first step and ending at the \(n+1\)-th step. Your task is to calculate the total time spent over \(k\) days.

Note: The turning value of the last step \(a_{n+1}\) is always 0 and is not provided in the input.

inputFormat

The first line contains two integers \(n\) and \(k\) (where \(n\) is the number of steps excluding the last fixed step and \(k\) is the number of days).

The second line contains \(n\) space-separated integers: \(a_1, a_2, \dots, a_n\), each being either 0 or 1.

outputFormat

Output a single integer representing the total time (in seconds) that the students take over \(k\) days.

sample

3 1
0 0 1
5