#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define repr(i,n) for(int i = (int)(n); i >= 0; i--) #define all(v) v.begin(),v.end() typedef long long ll; int main(){ int N; cin >> N; vector vec(N); rep(i,N){ cin >> vec[i]; } vector > minimum_sort(N); rep(i,N){ minimum_sort[i].first = vec[i]; minimum_sort[i].second = i; } sort(all(minimum_sort)); int ans; if (minimum_sort[0].second < minimum_sort[1].second){ int fin_num = N; for (int i = 2; i < N; i++){ if (minimum_sort[i].second < minimum_sort[i - 1].second){ fin_num = i; break; } } if (fin_num == N){ ans = -1; } else if (minimum_sort[fin_num].second < minimum_sort[0].second){ ans = minimum_sort[fin_num].first + minimum_sort[0].first + minimum_sort[1].first; } else{ rep(i,fin_num){ if (minimum_sort[fin_num].second < minimum_sort[i].second){ ans = minimum_sort[fin_num].first + minimum_sort[0].first + minimum_sort[i].first; break; } } } } if (minimum_sort[0].second > minimum_sort[1].second){ int fin_num = N; for (int i = 2; i < N; i++){ if (minimum_sort[i].second > minimum_sort[i - 1].second){ fin_num = i; break; } } if (fin_num == N){ ans = -1; } else if (minimum_sort[fin_num].second > minimum_sort[0].second){ ans = minimum_sort[fin_num].first + minimum_sort[0].first + minimum_sort[1].first; } else{ rep(i,fin_num){ if (minimum_sort[fin_num].second > minimum_sort[i].second){ ans = minimum_sort[fin_num].first + minimum_sort[0].first + minimum_sort[i].first; } break; } } } cout << ans << endl; }