#include using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template using max_heap = std::priority_queue; template using min_heap = std::priority_queue, std::greater>; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template void bset(T& mask, const int ind) { mask |= ((T)1 << ind); } template void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); } template void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); } template void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; constexpr ull TEN(const int n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; } template struct fix : F { fix(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; class printer { public: printer(std::ostream& os_ = std::cout) : os{os_} {} template int operator()(const T& v) { return os << v, 0; } template int operator()(const std::vector& vs) { for (int i = 0; i < (int)vs.size(); i++) { os << (i ? " " : ""), this->operator()(vs[i]); } return 0; } template int operator()(const std::vector>& vss) { for (int i = 0; i < (int)vss.size(); i++) { os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), this->operator()(vss[i]); } return 0; } template int operator()(const T& v, const Args&... args) { return this->operator()(v), os << ' ', this->operator()(args...), 0; } template int ln(const Args&... args) { return this->operator()(args...), os << '\n', 0; } template int el(const Args&... args) { return this->operator()(args...), os << std::endl, 0; } template int fmt(const std::string& s, const Args&... args) { return rec(s, 0, args...); } private: int rec(const std::string& s, int index) { return os << s.substr(index, s.size()), 0; } template int rec(const std::string& s, int index, const T& v, const Args&... args) { return index == s.size() ? 0 : s[index] == '%' ? (this->operator()(v), rec(s, index + 1, args...)) : (os << s[index], rec(s, index + 1, v, args...)); } std::ostream& os; }; printer out; template std::vector generated(const int n, F f) { std::vector ans(n); return std::generate(ans.begin(), ans.end(), f), ans; } std::vector ioted(const int n, const int offset = 0) { std::vector ans(n); return std::iota(ans.begin(), ans.end(), offset), ans; } template Vs reversed(const Vs& vs) { auto ans = vs; return std::reverse(ans.begin(), ans.end()), ans; } template> std::vector sorted(const std::vector& vs, F comp = F{}) { auto ans = vs; return std::sort(ans.begin(), ans.end(), comp), ans; } template std::vector sorted_iota(const int n, F comp = F{}, const int offset = 0) { return sorted(ioted(n, offset), comp); } class scanner { public: scanner(std::istream& is_ = std::cin) : is{is_} { is.tie(nullptr), std::ios::sync_with_stdio(false); } template T val() { static T v; return is >> v, v; } template T val(const T offset) { return val() - offset; } template std::vector vec(const int n) { return generated(n, [&] { return val(); }); } template std::vector vec(const int n, const T offset) { return generated(n, [&] { return val(offset); }); } template std::vector> vvec(const int n0, const int n1) { return generated>(n0, [&] { return vec(n1); }); } template std::vector> vvec(const int n0, const int n1, const T offset) { return generated>(n0, [&] { return vec(n1, offset); }); } template auto tup() { return std::tuple...>{val()...}; } template auto tup(const Args&... offsets) { return std::tuple...>{val(offsets)...}; } private: std::istream& is; }; scanner in; # define SHOW(...) static_cast(0) template auto make_v(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], make_v(szs, x)); } } const auto min_merge = [](const auto& x1, const auto& x2) { return std::min(x1, x2); }; const auto max_merge = [](const auto& x1, const auto& x2) { return std::max(x1, x2); }; const auto minmax_merge = [](const auto& x1, const auto& x2) { return decltype(x1){std::min(x1.first, x2.first), std::max(x1.second, x2.second)}; }; const auto sum_merge = [](const auto& x1, const auto& x2) { return x1 + x2; }; const auto affine_merge = [](const auto& x1, const auto& x2) { return decltype(x1){x1.first * x2.first, x1.first * x2.second + x1.second}; }; const auto plus_comp = [](const auto& f1, const auto& f2) { return f1 + f2; }; const auto mult_comp = [](const auto& f1, const auto& f2) { return f1 * f2; }; const auto assign_comp = [](const auto& f1, const auto& f2) { return f1 == inf_v ? f2 : f1; }; const auto affine_comp = [](const auto& f1, const auto& f2) { return decltype(f1){f1.first * f2.first, f1.first * f2.second + f1.second}; }; const auto min_plus_apply = [](const auto& f, const auto& x, const auto&) { return x + f; }; const auto max_plus_apply = [](const auto& f, const auto& x, const auto&) { return x + f; }; const auto minmax_plus_apply = [](const auto& f, const auto& x, const auto&) { return decltype(x){x.first + f, x.second + f}; }; const auto minmax_mult_apply = [](const auto& f, const auto& x, const auto&) { return f >= 0 ? decltype(x){f * x.first, f * x.second} : decltype(x){f * x.second, f * x.first}; }; const auto min_assign_apply = [](const auto& f, const auto& x, const auto&) { return f == inf_v ? x : f; }; const auto max_assign_apply = [](const auto& f, const auto& x, const auto&) { return f == inf_v ? x : f; }; const auto minmax_assign_apply = [](const auto& f, const auto& x, const auto&) { return f == inf_v ? x : decltype(x){f, f}; }; const auto minmax_affine_apply = [](const auto& f, const auto x, const auto&) { return f.first >= 0 ? decltype(x){x.first * f.first + f.second, x.second * f.first + f.second} : decltype(x){x.second * f.first + f.second, x.first * f.first + f.second}; }; const auto sum_plus_apply = [](const auto& f, const auto& x, const auto& l) { return x + f * l; }; const auto sum_mult_apply = [](const auto& f, const auto& x, const auto&) { return x * f; }; const auto sum_assign_apply = [](const auto& f, const auto& x, const auto& l) { return f == inf_v ? x : f * l; }; const auto sum_affine_apply = [](const auto& f, const auto x, const auto& l) { return x * f.first + f.second * l; }; const auto reverse = [](const auto f) { return [f](const auto& x1, const auto& x2) { return f(x2, x1); }; }; template class segtree { public: segtree(const std::vector& vs, const Merge merge_, const T e_) : size{(int)vs.size()}, half{(int)ceil2(size)}, vals(half << 1, e_), merge{merge_}, e{e_} { std::copy(vs.begin(), vs.end(), vals.begin() + half); for (int i = half - 1; i >= 1; i--) { up(i); } } T get(const int a) const { return assert(0 <= a and a < size), vals[a + half]; } void set(int a, const T& v) { assert(0 <= a and a < size); vals[a += half] = v; while (a >>= 1) { up(a); } } T fold(int l, int r) const { assert(0 <= l and l <= r and r <= size); T accl = e, accr = e; for (l += half, r += half; l < r; l >>= 1, r >>= 1) { if (l & 1) { accl = merge(accl, vals[l++]); } if (r & 1) { accr = merge(vals[--r], accr); } } return merge(accl, accr); } template int bs_right(const int l, const Pred& pred) const { assert(0 <= l and l < size); if (not pred(vals[l + half])) { return l + 1; } int index = l + half; T acc = vals[index]; bool from_left = false; while (index >= 1) { if (from_left) { const T nacc = merge(acc, vals[index << 1 | 1]); if (not pred(nacc)) { break; } acc = nacc; } from_left = 1 - (index & 1), index >>= 1; } if (index == 0) { return size + 1; } index = index << 1 | 1; while (index < half) { index <<= 1; const T nacc = merge(acc, vals[index]); if (pred(nacc)) { acc = nacc, index |= 1; } } return index - half + 1; } template int bs_left(int r, const Pred& pred) const { assert(0 < r and r <= size); r--; if (not pred(vals[r + half])) { return r; } int index = r + half; T acc = vals[index]; bool from_right = false; while (index >= 1) { if (from_right) { const T nacc = merge(vals[index << 1], acc); if (not pred(nacc)) { break; } acc = nacc; } from_right = (index & 1), index >>= 1; } if (index == 0) { return -1; } index = index << 1; while (index < half) { index = index << 1 | 1; const T nacc = merge(vals[index], acc); if (pred(nacc)) { acc = nacc, index ^= 1; } } return index - half; } friend std::ostream& operator<<(std::ostream& os, const segtree& seg) { os << "["; for (int i = seg.half; i < seg.half + seg.size; i++) { os << seg.vals[i] << (i + 1 == seg.half + seg.size ? "" : ","); } return (os << "]\n"); } private: void up(const int i) { vals[i] = merge(vals[i << 1], vals[i << 1 | 1]); } const int size, half; std::vector vals; const Merge merge; const T e; }; int main() { const auto N = in.val(); const auto as = in.vec(N); std::vector lit(N), git(N); std::iota(lit.begin(), lit.end(), 0); std::sort(lit.begin(), lit.end(), [&](int i, int j) { return as[i] < as[j]; }); std::iota(git.begin(), git.end(), 0); std::sort(git.begin(), git.end(), [&](int i, int j) { return as[i] > as[j]; }); auto rmq1 = segtree(std::vector(N, inf_v), min_merge, inf_v); auto rmq2 = segtree(std::vector(N, inf_v), min_merge, inf_v); ll ans = inf_v; for (const int i : lit) { chmin(ans, as[i] + rmq1.fold(0, i) + rmq1.fold(i + 1, N)); rmq1.set(i, as[i]); } for (const int i : git) { chmin(ans, as[i] + rmq2.fold(0, i) + rmq2.fold(i + 1, N)); rmq2.set(i, as[i]); } out.ln(ans == inf_v ? -1LL : ans); return 0; }