N = int(input())
A = list(map(int, input().split()))

for i in range(N - 1):
    if A[i] < A[i + 1]:
        break
else:
    print(-1)
    exit()

for i in range(N - 1):
    if A[i] > A[i + 1]:
        break
else:
    print(-1)
    exit()

LMIN = [A[0]] * N
RMIN = [A[-1]] * N
for i in range(1, N):
    LMIN[i] = min(LMIN[i - 1], A[i])
for i in reversed(range(N - 1)):
    RMIN[i] = min(RMIN[i + 1], A[i])

ans = 10 ** 10
for i in range(1, N - 1):
    if LMIN[i - 1] < A[i] > RMIN[i + 1] or LMIN[i - 1] > A[i] < RMIN[i + 1]:
        ans = min(ans, LMIN[i - 1] + A[i] + RMIN[i + 1])
print(ans)