n = int(input()) sushi_plates = list(map(int, input().split())) sushi_plates.reverse() if n == 1: print(sushi_plates.pop()) exit() total = 0 index = 0 while index < n - 1: first = sushi_plates[index] second = sushi_plates[index + 1] if first > second: total += first index += 2 else: total += second index += 3 print(total)