#include using namespace std; using ll=long long; using vin=vector; using vll=vector; using vvin=vector>; using vvll=vector>; using vstr=vector; using vvstr=vector>; using vch=vector; using vvch=vector>; using vbo=vector; using vvbo=vector>; using vpii=vector>; using pqsin=priority_queue,greater>; #define mp make_pair #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define decp(n) cout<>n; set l,r; vin a(n); rep(i,n){ cin>>a[i]; r.insert(a[i]); } r.erase(a[0]); vin ls(n),rs(n); ls[0]=a[0];rs[0]=a[n-1]; rep(i,n-1){ ls[i+1]=min(ls[i],a[i+1]); rs[i+1]=min(rs[i],a[n-2-i]); } int ans=inf; rep2(i,1,n-1){ l.insert(a[i-1]); r.erase(a[i]); if(a[i]>ls[i-1]&&a[i]>rs[n-2-i])ans=min(ans,ls[i-1]+a[i]+rs[n-2-i]); if(l.lower_bound(a[i])==l.end())continue; if(r.lower_bound(a[i])==r.end())continue; ans=min(ans,a[i]+*l.lower_bound(a[i])+*r.lower_bound(a[i])); } if(ans==inf)ans=-1; cout<