#include "bits/stdc++.h" using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define REP1(i, n) for(int i=1; i<=(n); i++) #define RREP1(i, n) for(int i=(n); i>=1; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define out(...) printf(__VA_ARGS__) #if DEBUG #define debug(...) printf(__VA_ARGS__) #else #define debug(...) /* ... */ #endif templatebool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b a && b > c) return true; return false; } template class ST { using F = function; int size; T init; F f; vector seg; int depth = 0; public: ST(int size, T init, F f): size(size), init(init), f(f) { while (1<>=1) seg[k] = f(seg[2*k], seg[2*k+1]); } T query(int a, int b) { a += 1<>= 1; b >>= 1; } return f(l, r); } T operator[] (int k) { return seg[k + (1<>N; auto minst = new ST(N, 1e9, [](int a, int b){ return min(a,b); }); REP(i,N)cin>>A[i]; REP(i,N) { minst->update(i, A[i]); } int ret = 1e9; REP1(i,N-2) { int a = minst->query(0, i); int b = minst->query(i+1, N); if (is_kadomatsu(a, A[i], b)) { debug("ok: %lld %lld %lld\n",a,A[i],b); chmin(ret, a+A[i]+b); } } if (ret==1e9) ret=-1; cout << ret << endl; }