N = int(input()) A = list(map(int,input().split())) INF = 10**10 L = [INF]*N R = [INF]*N L[0] = A[0] R[N-1] = A[N-1] for i in range(1,N): L[i] = min(A[i],L[i-1]) R[N-1-i] = min(A[N-1-i],R[N-i]) ans = INF for i in range(1,N-1): if L[i-1] < A[i] and R[i+1] < A[i]: ans = min(ans,L[i-1] + A[i] + R[i+1]) A = [-i for i in A] L = [INF]*N R = [INF]*N L[0] = A[0] R[N-1] = A[N-1] for i in range(1,N): L[i] = min(A[i],L[i-1]) R[N-1-i] = min(A[N-1-i],R[N-i]) tans = -INF for i in range(1,N-1): if L[i-1] < A[i] and R[i+1] < A[i]: tans = max(tans,L[i-1] + A[i] + R[i+1]) tans *= -1 ans = min(tans,ans) if ans == INF: print(-1) else: print(ans)