human
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.

Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:

  * b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. 
  * For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1. 


The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.

In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.

Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.

Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

Input

The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of...
"""
C ... Write me a Python program to determine whether it ever becomes stable?
assistant
#Here is Python program to determine whether it ever becomes stable.
odeforces Round #327 (Div. 2)

Problem 591 C. Median Smoothing

@author yamaton
@date 2015-11-06
"""

def reduce_consec(iterable):
    """
    [1, 2, 3, 6, 7, 9, 10, 11, 12, 13, 20]
    --> [(1, 3), (6, 2), (9, 5), (20, 1)]
    Detect consecutive part and (starting_value, length) pair

    :param xs: List of int
    :return: List of pair of int
    """
    stack = []
    for x in iterable:
        if stack:
            # check if consective
            if stack[-1] + 1 == x:
                stack.append(x)
            # if not consecutive, flush stack and start with new element
            else:
                yield (stack[0], len(stack))
                stack = [x]
        else:
            # starting element
            stack.append(x)

    if stack:
        yield (stack[0], len(stack))

def alternating_indices(xs):
    for i, x in enumerate(xs):
        if i == 0 or i == len(xs) - 1:
            continue
        if xs[i-1] != x and xs[i+1] != x:
            yield i

def solve(xs, n):
    # zigzag = []  # alternating part
    # for i, x in enumerate(xs):
    #     if i == 0 or i == n - 1:
    #         continue
    #     if xs[i-1] != x and xs[i+1] != x:
    #         zigzag.append(i)

    zigzag = alternating_indices(xs)
    zigzag_start_length_pairs = reduce_consec(zigzag)
    count = 0
    result = xs[:]
    for (i, n) in zigzag_start_length_pairs:
        n_half = n // 2
        count = max(count, (n + 1) // 2)
        if n % 2 == 0:
            for j in range(i, i + n_half):
                result[j] = xs[i-1]
            for j in range(i + n_half, i + n):
                result[j] = 1 - xs[i-1]
        else:
            for j in range(i, i + n):
                result[j] = xs[i-1]
    return count, result

def solve_bruteforce(xs, n):
    def transform(ps):
        result = []
        for i in range(n):
            if i == 0 or i == n-1:
                result.append(ps[i])
            else:
                median = int(sum(ps[i-1:i+2]) >= 2)
                result.append(median)
        return...