Under construction

Still being built. Things may change

N-Rock-Paper-Scissors

Time limit1sMemory limit1024 MB

Summary
Given the opponent's card sequence, play each card 1 to N exactly once to maximize rounds where your card is not cyclically adjacent to theirs.
Level

Medium6 of 10

Topics
Greedy, Math
Solved
No attempts yet

Problem

Dohun, a game enthusiast, wanted to make rock-paper-scissors more fun, so he invented a new game called N-Rock-Paper-Scissors. The rules are as follows.

  • N+1N+1 players each start with cards numbered from 11 to NN, one card each.
  • Each player plays one round against each of the other NN players, for NN rounds in total. In each round, the two players each play one of their cards.
  • If two players play cards ii and i+1i+1, the player who played card ii wins, and the player who played card i+1i+1 loses. (1≤i≤N−1)(1\le i\le N-1)
  • If two players play card NN and card 11, the player who played card NN wins, and the player who played card 11 loses.
  • All other matchups are draws.
  • Once a round is decided as a win, loss, or draw, the played card is discarded, and the next round is played with the remaining cards.

Dohun designed these rules. He wants to know the maximum number of draws possible in the next NN rounds, given the list of cards the opponent will play in those rounds.

Input

The first line contains the number of rounds NN. (3≤N≤200 000)(3\le N\le 200\,000)

The second line contains the cards A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N that the opponent will play in order, separated by spaces. (1≤Ai≤N)(1 \le A_i \le N)

Output

Print the maximum number of draws you can get by playing cards appropriately in the NN rounds.

Examples3

  1. Example 1

    Input
    4
    1 1 2 2
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    1 1 3
    
    Expected output
    2
    
  3. Example 3

    Input
    4
    2 2 4 4
    
    Expected output
    2