N = int(input()) V = tuple(map(int, input().split())) s = 0 e = False if N > 2: for i in range(N-1, 1, -1): if e: if V[i-2] > V[i-1]: e = True else: e = False else: if V[i] + V[i-2] > V[i-1]: s += V[i] e = True else: e = False else: if e: s += V[0] else: s += max(V[0], V[1]) print(s) else: print(max(V))