#include using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); long long int res = 1e18; int n; long long int t; set l, r; vector v; cin >> n; for (int i = 0; i < n; i++) { cin >> t; v.push_back(t); r.insert(t); } for (int i = 0; i < n; i++) { r.erase(v[i]); if (l.size() > 0 && r.size() > 0) { long long int a = *l.begin(); long long int b = *r.begin(); if (a < v[i] && b < v[i]) { res = min(res, a + b + v[i]); } auto it1 = l.lower_bound(v[i]); auto it2 = r.lower_bound(v[i]); if (it1 != l.end() && it2 != r.end()) { a = *it1; b = *it2; res = min(res, a + b + v[i]); } } l.insert(v[i]); } if (res >= 1e18) { res = -1; } cout << res << '\n'; return 0; }