N = int(input()) A = list(map(int, input().split())) L0 = [A[0]] L1 = [A[-1]] for i in range(1, N): L0.append(min(L0[-1], A[i])) L1.append(min(L1[-1], A[-i - 1])) L1 = L1[::-1] ans = INF = 10 ** 9 for i in range(1, N - 1): if L0[i - 1] < A[i] and L1[i + 1] < A[i]: ans = min(ans, L0[i - 1] + A[i] + L1[i + 1]) mini = A.index(min(A)) if 0 < mini < N - 1: ans = min(ans, L0[mini - 1] + A[mini] + L1[mini + 1]) if ans == INF: print(-1) else: print(ans)