n = int(input()) a = list(map(int, input().split())) lminList = [a[0]] rminList = [a[-1]] lmaxList = [a[0]] rmaxList = [a[-1]] lmin = a[0] rmin = a[-1] lmax = a[0] rmax = a[-1] for i in range(1, n): if lmin > a[i]: lminList.append(a[i]) lmin = a[i] else: lminList.append(lmin) if lmax < a[i]: lmaxList.append(a[i]) lmax = a[i] else: lmaxList.append(lmax) for i in range(n-1)[::-1]: if rmin > a[i]: rminList.append(a[i]) rmin = a[i] else: rminList.append(rmin) if rmax < a[i]: rmaxList.append(a[i]) rmax = a[i] else: rmaxList.append(rmax) rminList = rminList[::-1] rmaxList = rmaxList[::-1] ans = float("inf") for i in range(1, n-1): lmin = lminList[i-1] rmin = rminList[i+1] lmax = lmaxList[i-1] rmax = rmaxList[i+1] m = a[i] if lmin < m and m > rmin and lmin != rmin: ans = min(ans, lmin+m+rmin) if lmax > m and m < rmax and lmax != rmax: ans = min(ans, lmax+m+rmax) if ans == float("inf"): print(-1) else: print(ans)