import numpy as np def solve(area): if area.shape[0] <= 1: return 0 selected_ind = area*0 max_ind = np.argmax(area) min_ind = max_ind for i in reversed(range(max_ind)): if selected_ind[i] == 1: break if area[min_ind] > area[i]: min_ind = i selected_ind[i] = 1 min_ind = max_ind for i in range(max_ind+1, area.shape[0]): if selected_ind[i] == 1: break if area[min_ind] > area[i]: min_ind = i selected_ind[i] = 1 count = np.sum(selected_ind) count += solve(area[0:max_ind], selected_ind[0:max_ind], 1) count += solve(area[max_ind+1:area.shape[0]], selected_ind[max_ind+1:selected_ind.shape[0]], 2) return count N = int(input()) input_np = np.array(input().split()).astype(np.int32) print(solve(input_np))