from sys import stdin def main(): input = lambda: stdin.readline()[:-1] N = int(input()) A = list(map(int, input().split())) left, right = [A[0]], [A[-1]] min_ = A[0] for i in range(1, N): n = min(min_, A[i]) left.append(n) min_ = n min_ = A[-1] for i in range(N - 2, -1, -1): n = min(min_, A[i]) right.append(n) min_ = n right = right[::-1] ans = float('inf') for i in range(1, N - 1): if left[i-1] < A[i] > right[i+1]: ans = min(ans, left[i-1] + A[i] + right[i+1]) l, r = min(A[:i]), min(A[i+1:]) if l > A[i] < r: ans = min(ans, l + A[i] + r) if isinstance(ans, float): print(-1) else: print(ans) main()