import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.TreeMap; import java.util.TreeSet; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } br.close(); TreeMap map = new TreeMap<>(); for (int i = 0; i < n; i++) { map.put(a[i], i); } TreeSet set = new TreeSet<>(); boolean h = false; for (Integer k : map.keySet()) { if (set.size() == 0) { } else if (set.size() == 1) { if (set.last() < map.get(k)) { h = true; } } else { if (h && set.last() > map.get(k)) { int v1 = a[set.pollFirst()]; int v2 = a[map.get(k)]; int v3 = a[set.higher(map.get(k))]; System.out.println(v1 + v2 + v3); return; } if (!h && set.first() < map.get(k)) { int v1 = a[set.pollLast()]; int v2 = a[map.get(k)]; int v3 = a[set.lower(map.get(k))]; System.out.println(v1 + v2 + v3); return; } } set.add(map.get(k)); } System.out.println(-1); } }