import sys input=sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 N=I() A=LI() inf=10**9 ml=[inf]*N mr=[inf]*N m=inf for i in range(N): ml[i]=m if A[i]m: m=A[i] m=inf for i in range(N-1,-1,-1): mr[i]=m if A[i]>m: m=A[i] ans2=inf for i in range(N): if ml[i]>A[i] and mr[i]>A[i]: temp=ml[i]+mr[i]+A[i] ans2=max(ans2,temp) ans2*=-1 ans=min(ans,ans2) inf=10**9 if ans>=inf: ans=-1 print(ans) main()