#include #define rep(i,n) for(ll i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair ; using pll = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; int main(){ int n; scanf("%d",&n); vector a(n); rep(i,n) scanf("%d",&a[i]); vector minl(n),minr(n); ll mn = INF; rep(i,n){ minl[i] = mn; mn = min(mn,a[i]); } mn = INF; for(ll i=n-1;i>=0;i--){ minr[i] = mn; mn = min(mn,a[i]); } ll ans = INF; rep(i,n){ if(a[i] > minl[i] && a[i] > minr[i]){ ans = min(ans,a[i] + minr[i] + minl[i]); } } vector maxr(n,-1),maxl(n,-1); set stl; rep(i,n){ auto it = stl.upper_bound(a[i]); if(it!=stl.end()) maxl[i] = (*it); stl.insert(a[i]); } set str; for(ll i=n-1;i>=0;i--){ auto it = str.upper_bound(a[i]); if(it!=str.end()) maxr[i] = (*it); str.insert(a[i]); } rep(i,n){ if(a[i] < maxl[i] && a[i] < maxr[i]){ ans = min(ans,a[i] + maxr[i] + maxl[i]); } } if(ans==LINF) cout << -1 << endl; else cout << ans << endl; return 0; }