#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; int main(){ int n; cin >> n; vector> p(n); rep(i, n){ int in; cin >> in; p.at(i) = make_pair(in, i); } sort(p.begin(), p.end()); list> list; list.push_back(p.front()); list.push_back(p.at(1)); bool mode = list.front().second < list.back().second; rep2(i, 2, n){ int l = min(list.front().second, list.back().second); int r = max(list.front().second, list.back().second); if(l < p.at(i).second && p.at(i).second < r){ for(auto q : list){ if(mode == true){ if(q.second > p.at(i).second){ cout << q.first + p.at(i).first + list.front().first << endl; return 0; } }else{ if(q.second < p.at(i).second){ cout << q.first + p.at(i).first + list.front().first << endl; return 0; } } } return 0; } list.push_back(p.at(i)); } cout << -1 << endl; return 0; }