def is_kadomatsu(a, b, c): return len({a, b, c}) == 3 and (b > max(a, c) or b < min(a, c)) def main(): n = int(input()) a = list(map(int, input().split())) left = [a[0]] * n right = [a[-1]] * n for i in range(1, n): left[i] = min(left[i - 1], a[i]) for i in reversed(range(n - 1)): right[i] = min(a[i], right[i + 1]) ans = 10 ** 9 for i in range(1, n - 1): if is_kadomatsu(left[i - 1], a[i], right[i + 1]): ans = min(ans, left[i - 1] + a[i] + right[i + 1]) if ans == 10 ** 9: ans = -1 print(ans) if __name__ == "__main__": main()