#include using namespace std; int INF = 1000000000; int main(){ int N; cin >> N; vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } //想定WA2 (場合分けもれ) vector L2(N); set L; for (int i = 0; i < N; i++){ auto itr = L.upper_bound(A[i]); if (itr == L.end()){ L2[i] = INF; } else { L2[i] = *itr; } L.insert(A[i]); } vector R2(N); set R; for (int i = N - 1; i >= 0; i--){ auto itr = R.upper_bound(A[i]); if (itr == R.end()){ R2[i] = INF; } else { R2[i] = *itr; } R.insert(A[i]); } int ans2 = INF; for (int i = 1; i < N - 1; i++){ ans2 = min(ans2, L2[i] + A[i] + R2[i]); } if (ans2 == INF){ cout << -1 << endl; } else { cout << ans2 << endl; } }