#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec A(N); ll inf = 1e18; ll ans = inf; set L,R; for(int i=0;i> A[i]; R.insert(A[i]); } L.insert(A[0]); R.erase(A[0]); for(int i=1;iml && A[i]>mr) ans = min(ans,A[i]+ml+mr); bool ok = true; auto itl = L.lower_bound(A[i]); ok &= itl!=L.end(); auto itr = R.lower_bound(A[i]); ok &= itr!=R.end(); if(ok){ // cerr << i << *itl << " " << *itr << "\n"; ans = min(ans,A[i]+*itl+*itr); } L.insert(A[i]); } cout << (ans!=inf? ans:-1) << "\n"; }