#include using namespace std; #define rep(i, n) for (int i=0; i> N; rep(i, N) cin >> A[i]; multiset ls, rs; ls.insert(A[0]); ls.insert(1000000000000); for (int i=2; ilm && A[i]>rm) ans = min(ans, lm+A[i]+rm); int ln = *ls.upper_bound(A[i]), rn = *rs.upper_bound(A[i]); ans = min(ans, ln+A[i]+rn); ls.insert(A[i]); rs.erase(rs.find(A[i+1])); } if (ans==1000000000000) cout << -1 << endl; else cout << ans << endl; }