#!/usr/bin/python3

import math
from functools import cache


terraces = int(input())
line = input()
heights = [int(i) for i in line.split()]
unique_heights = list(sorted(set(heights)))

eligible_pairs = []

for idx, height in enumerate(heights):
    eligible_len = 0
    length = -1
    if idx == len(heights)-1 or height == heights[idx+1]:
        continue
    for i2 in range(idx+1, len(heights)):
        length += 1
        if heights[i2] > height:
            break
        if heights[i2] == height:
            eligible_len = length
            break

    if eligible_len > 0:
        eligible_pairs.append((idx, idx+eligible_len+1))

# print(eligible_pairs)

occupied = []


@cache
def find_best(pair_idx: int) -> int:
    global eligible_pairs
    global occupied
    current = eligible_pairs[pair_idx]
    current_height = heights(current[0])
    current_len = current[1] - current[0] - 1
    occupied.append(current_len)

    best = 0
    for i in range(pair_idx+1):
        best = max(best, find_best(pair_idx))
        if eligible_pairs[i][0] > current[1]:
            next_eligible.append(i)
    return max(map(find_best, next_eligible)) if next_eligible else current[1] - current[0]


total = 0
for pair in eligible_pairs:
    total += pair[1] - pair[0] - 1

print(total)

