#include using namespace std; #define int long long templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> n; int a[200010]; set l, r; for(int i = 0;i < n;i++){ cin >> a[i]; r.insert(a[i]); } int ans = LLONG_MAX; l.insert(a[0]); r.erase(a[0]); for(int i = 1;i < n-1;i++){ r.erase(a[i]); { int lnum = *l.begin(); int rnum = *r.begin(); if(lnum < a[i] && rnum < a[i]){ ans = min(ans, lnum + a[i] + rnum); } } { auto lnum = l.upper_bound(a[i]); auto rnum = r.upper_bound(a[i]); if(lnum != l.end() && rnum != r.end()){ ans = min(ans, *lnum + a[i] + *rnum); } } l.insert(a[i]); } if(ans == LLONG_MAX){ cout << -1 << endl; }else{ cout << ans << endl; } return 0; }