import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int[] lefts = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if (i == 0) { lefts[i] = arr[i]; } else { lefts[i] = Math.min(arr[i], lefts[i - 1]); } } int[] rights = new int[n]; rights[n - 1] = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { rights[i] = Math.min(arr[i], rights[i + 1]); } int min = Integer.MAX_VALUE; for (int i = 1; i < n - 1; i++) { int front = lefts[i - 1]; int rear = rights[i + 1]; if ((arr[i] > front && arr[i] > rear) || (arr[i] < front && arr[i] < rear)) { min = Math.min(min, arr[i] + rear + front); } } if (min == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } }