1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
n = int(input())
z = input().split()

a = [int(x) for x in z]

def calculate(a, n, reverse_first):
    help = [x for x in a]
    first_no_equal = 0
    for i in range(1, n):
        if a[i] != a[0]:
            first_no_equal = i
            break

    score = (first_no_equal)//2

    raised = a[first_no_equal-1] < a[first_no_equal]
    if reverse_first:
        if raised:
            help[first_no_equal] = a[first_no_equal-1]-1
        else:
            help[first_no_equal] = a[first_no_equal-1]+1
        raised = not raised
        score += 1
    for i in range(first_no_equal, n-1):
        if raised and help[i] <= help[i+1]:
            score += 1
            temp = help[i+2] if i+2 < n else help[i]
            help[i+1] = min(a[i], temp)-1
        elif not raised and help[i] >= help[i+1]:
            score += 1
            temp = help[i+2] if i+2 < n else help[i]
            help[i+1] = max(help[i], temp)+1
        raised = not raised
    
    return score

left_score = calculate(a, n, False)
left_score_r = calculate(a, n, True)
a.reverse()
right_score = calculate(a, n, False)
right_score_r = calculate(a, n, True)

print(min(left_score, right_score, left_score_r, right_score_r))