#pragma once //https://atcoder.jp/contests/dp/tasks/dp_t #include #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector VI; typedef pair P; typedef tuple t3; typedef tuple t4; typedef tuple t5; #define rep(a,n) for(ll a = 0;a < n;a++) #define repi(a,b,n) for(ll a = b;a < n;a++) #include using namespace std; static const ll INF = 1e15; static const ll mod = 1e9 + 7; ll solve(const vector& as) { ll res = -INF; ll s = 0; rep(j, as.size()) { s = max(s + as[j], as[j]); res = max(res, s); } return res; } int main() { int n; cin >> n; vector vs(n); rep(i, n) cin >> vs[i]; set right; rep(i, n) right.insert(vs[i]); set left; left.insert(vs[0]); right.erase(vs[0]); ll lower = INF; for (int i = 1; i < n-1; i++) { ll c = vs[i]; right.erase(c); auto l = left.lower_bound(vs[i]); auto r = right.lower_bound(vs[i]); if (l != left.end() && r != right.end()) { lower = min(lower, *l + *r + c); } if (*left.begin() < c && *right.begin() < c) { lower = min(lower, *left.begin() + *right.begin() + c); } left.insert(c); } if (lower != INF) { cout << lower << endl; } else { cout << -1 << endl; } return 0; }