Under construction

Still being built. Things may change

Obstacle Course

Interview

Time limit1sMemory limit2048 MB

Summary
Given heights on odd-numbered segments with free even-numbered segments, find the longest run of consecutive segments where Adrian can step up or down by exactly 1 each time.
Level

Medium4 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

Morgan is a businessman and parkour enthusiast who owns an obstacle course of NN road segments, numbered from 11 to NN. Segment 11 is adjacent to segment 22, segment 22 is adjacent to segment 33, and so on, up to segment N−1N-1 adjacent to segment NN. Each odd-numbered segment has a positive integer height AiA_i, and each even-numbered segment has height 00. On Morgan's course, anyone can start from any segment, move in one direction, and stop at any segment they can reach.

Adrian is a beginner. He keeps going while the height difference between the current segment and the next one is exactly 11. If the difference is more than 11, he cannot climb up or jump down, so he stops. If the next segment has the same height as the current one, he loses interest and stops. He also stops when he reaches segment NN or segment 11, the end of the course. Adrian's happiness is the number of segments he passes before he stops. He starts from whichever segment gives him the largest happiness.

Morgan wants Adrian to enjoy the course, so he changes the heights of some even-numbered segments (possibly none) to maximize Adrian's largest happiness. Find the largest happiness Adrian can have after the changes.

For example, let A1..9=[6,0,4,0,8,0,12,0,14]A_{1..9} = [6, 0, 4, 0, 8, 0, 12, 0, 14]. If AA is changed to [6,5,4,5,8,11,12,13,14][6, 5, 4, 5, 8, 11, 12, 13, 14], Adrian can go from segment 1 to 4, or from segment 6 to 9, and his happiness is 4, which is the largest after the change. Other changes also reach 4, such as [6,5,4,3,8,7,12,12,14][6, 5, 4, 3, 8, 7, 12, 12, 14], but no change can make the value larger than 4.

Input

The first line contains an integer NN (2≤N≤1000002 \le N \le 100000). The second line contains NN integers AiA_i. Ai=0A_i = 0 if ii is even, and 1≤Ai≤1000001 \le A_i \le 100000 if ii is odd.

Output

Print one integer on a single line: the largest possible happiness of Adrian on Morgan's new course.

Examples4

  1. Example 1

    Input
    9
    6 0 4 0 8 0 12 0 14
    
    Expected output
    4
    
  2. Example 2

    Input
    7
    3 0 5 0 5 0 7
    
    Expected output
    7
    
  3. Example 3

    Input
    6
    6 0 2 0 8 0
    
    Expected output
    3
    
  4. Example 4

    Input
    7
    4 0 6 0 10 0 9
    
    Expected output
    4