#include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> n; vector a(n); REP(i,n){ cin >> a[i]; } ll ans=INF; vector u(n,INF),v(n,INF); set st,ts; ts.insert(a[n-1]); REPR(i,n-1){ auto p=ts.begin(); auto q=ts.upper_bound(a[i]); if(*pa[i]){ v[i]=*q; } ts.insert(a[i]); } st.insert(a[0]); FOR(i,1,n-1){ auto p=st.begin(); auto q=st.upper_bound(a[i]); ll x=*p,y=*q; if(xa[i]){ chmin(ans,y+a[i]+v[i]); } } cout << (ans==INF?-1:ans) << endl; }