#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; using P = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr long long MOD = 1e9 + 7; signed main() { int n; cin >> n; set L, R; int a[n]; rep(i, n) { cin >> a[i]; if (i) R.emplace(a[i]); } L.emplace(a[0]); int ans = INF; for (int i = 1; i < n - 1; i++) { auto litr = L.upper_bound(a[i]); auto ritr = R.upper_bound(a[i]); if(litr != L.end() && ritr != R.end()){ ans = min(ans, *litr + a[i] + *ritr); } if(*L.begin() < a[i] && a[i] > *R.begin()){ ans = min(ans, *L.begin() + a[i] + *R.begin()); } L.emplace(a[i]); R.erase(a[i]); } cout << (ans != INF ? ans : -1) << endl; return 0; }