#include #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define max3(a, b, c) ((a) > (max(b, c)) ? (a) : (max(b, c))) #define min3(a, b, c) ((a) < (min(b, c)) ? (a) : (min(b, c))) #define MAX 200010 int main(){ int n, i, A[MAX], lm[MAX], rm[MAX]; scanf("%d", &n); for(i = 0; i < n; ++i) { scanf("%d", &A[i]); } lm[0] = A[0], rm[n - 1] = A[n - 1]; for(i = 1; i < n; ++i) { lm[i] = min(lm[i - 1], A[i]); rm[n - i - 1] = min(rm[n - i], A[n - i - 1]); } int ans = 1e9; for(i = 1; i < n - 1; ++i) { if(max3(lm[i - 1], A[i], rm[i + 1]) == A[i] || min3(lm[i - 1], A[i], rm[i + 1]) == A[i]) { ans = min(ans, lm[i - 1] + A[i] + rm[i + 1]); } } if(ans != 1e9)printf("%d\n", ans); else printf("%d\n", -1); return 0; }