#include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template void ndfill(V &x, const T &val) { x = val; } template void ndfill(vector &vec, const T &val) { for (auto &v : vec) ndfill(v, val); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector srtunq(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; // Range Minimum Query for static sequence by sparse table // Complexity: O(NlogN) for precalculation, O(1) per query template struct StaticRMQ { inline T func(const T &l, const T &r) { return std::min(l, r); } int N, lgN; T defaultT; std::vector> data; std::vector lgx_table; StaticRMQ(const std::vector &sequence, T defaultT) : N(sequence.size()), defaultT(defaultT) { lgx_table.resize(N + 1); for (int i = 2; i < N + 1; i++) lgx_table[i] = lgx_table[i >> 1] + 1; lgN = lgx_table[N] + 1; data.assign(lgN, std::vector(N, defaultT)); data[0] = sequence; for (int d = 1; d < lgN; d++) { for (int i = 0; i + (1 << d) <= N; i++) { data[d][i] = func(data[d - 1][i], data[d - 1][i + (1 << (d - 1))]); } } } T get(int l, int r) { // [l, r), 0-indexed assert(l >= 0 and r <= N); if (l >= r) return defaultT; int d = lgx_table[r - l]; return func(data[d][l], data[d][r - (1 << d)]); } }; int main() { int N; cin >> N; vector A(N); cin >> A; StaticRMQ rmq(A, 1e9); int ret = 1e9; FOR(i, 1, N - 1) { int l = rmq.get(0, i); int r = rmq.get(i + 1, N); if (l < A[i] and r < A[i]) chmin(ret, A[i] + l + r); } dbg(ret); set sl, sr; REP(i, N) sr.insert(A[i]); sl.insert(1e9); REP(i, N) { sr.erase(A[i]); auto itr = sl.upper_bound(A[i]); auto itr2 = sr.upper_bound(A[i]); if (itr != sl.end() and itr2 != sr.end()) { chmin(ret, A[i] + *itr + *itr2); } sl.insert(A[i]); } cout << (ret != 1e9 ? ret : -1) << '\n'; }