n = input() arr = map(int, raw_input().split()) maxes = [] while sum(arr) != 0: mx = arr.index(max(arr)) maxes.append(arr[mx]) arr[mx] = 0 if mx - 1 >= 0: arr[mx - 1] = 0 if mx + 1 < len(arr): arr[mx + 1] = 0 print sum(maxes)