#include using namespace std; typedef long long ll; typedef unsigned long long LL; typedef long double ld; typedef vectorVI; typedef pairP; #define VV(T) vector> #define sz(x) int(x.size()) #define PI 3.1415926535897932384626433832795 #define rep(i, n) for (ll i = 0; i < (ll)n; i++) #define reprev(i,n) for (ll i=(ll)n-1;i>=0;i--) #define ALL(a) (a).begin(),(a).end() #define rALL(a) (a).rbegin(),(a).rend() #define c_max(a, b) (((ll)a)>((ll)b)?(a):(b)) #define c_min(a,b) (((ll)a)<((ll)b)?(a):(b)) #define chmax(x,a) x=c_max(x,a) #define chmin(x,a) x=c_min(x,a) #define vmax(v) *max_element(ALL(v)) #define vmin(v) *min_element(ALL(v)) #define $(x) {cout<<#x<<" = " <<(x)< bool contain(const std::string& s, const T& v) { return s.find(v) != std::string::npos; } const int MOD = 998244353; inline ll mod_add(ll a,ll b){return (a+b)%MOD;} inline ll mod_sub(ll a,ll b){return (a+MOD-b)%MOD;} inline ll mod_mul(ll a,ll b){return ((a%MOD)*(b%MOD))%MOD;} inline ll mod_pow(ll x,ll n,ll mod=(ll)MOD){ll res=1;while(n>0){ if(n&1)res=res*x%mod; x=x*x%mod;n>>=1;}return res;} inline ll mod_inv(ll x){return mod_pow(x,MOD-2);} inline ll mod_div(ll a, ll b){return mod_mul(a,mod_inv(b));} const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; // vector型から重複を削除 list.erase(unique(ALL(list)),list.end()); // g++ -o a a.cpp -Wall -lm -std=c++17 //push_back -> emplace_back int n; VI a; int main(){ cin>>n; a.resize(n); rep(i,n)cin>>a[i]; VI L(n+1),R(n+1); L[0]=INFTY,R[n]=INFTY; rep(i,n){ L[i+1]=min(L[i],a[i]); } for(int i=n-1;i>=0;i--){ R[i]=min(R[i+1],a[i]); } ll ans=INFTY; for(int i=1;iR[i+1])chmin(ans,sum); else if(L[i]>a[i] && a[i]=INFTY)cout<<-1<