N=int(input()) A=list(map(int, input().split())) LMI=[0 for i in range(N)] RMI=[0 for i in range(N)] mi,ma=10**9,0 for i in range(N): mi=min(A[i],mi) LMI[i]=mi mi,ma=10**9,0 for i in range(N): mi=min(A[-1-i],mi) RMI[-1-i]=mi ans=10**9 for i in range(1,N-1): if LMI[i-1]RMI[i+1]: d=LMI[i-1]+A[i]+RMI[i+1] ans=min(d,ans) import bisect for i in range(1,N-1): L=sorted(A[:i]) R=sorted(A[i+1:]) a=bisect.bisect_right(L,A[i]) b=bisect.bisect_right(R,A[i]) if a!=i and b!=N-i-1: d=L[a]+A[i]+R[b] ans=min(d,ans) if ans==10**9: print(-1) else: print(ans)