#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair; using pll = pair; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; int main() { int N; cin >> N; vector A(N); rep(i, N) cin >> A[i]; set left, right; rep(i, N) right.insert(A[i]); int res = INF; for(int i = 0; i < N - 1; i++) { right.erase(A[i]); if(i > 0) { left.insert(A[i - 1]); auto litr = left.lower_bound(A[i]), ritr = right.lower_bound(A[i]); if(litr != left.end() && ritr != right.end()) { res = min(res, *litr + *ritr + A[i]); } auto lmin = left.begin(), rmin = right.begin(); if(*lmin < A[i] && *rmin < A[i]) { res = min(res, A[i] + *rmin + *lmin); } } } cout << (res == INF ? -1 : res) << endl; return 0; }