#include using namespace std; #define rep(i, n) for(int i=0, i##_len=(n); i=0; --i) #define rreps(i, n) for(int i=((int)(n)); i>0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pl(s) cout << (s) << "\n"; #define pls(...) {bool space = false; for(auto i : __VA_ARGS__) (cout << (space?" ":"") << i), space = true; cout << "\n";} #define plexit(s) {cout << (s) << "\n"; exit(0);} #define yes(s) cout << ((s)?"Yes":"No") << "\n"; #define pb push_back #define pcnt __builtin_popcountll template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #ifdef __LOCAL #include #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define dump(...) #endif struct IOInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; IOInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); dump(IOS_PREC); if(AUTOFLUSH) cout << unitbuf; } } IO_INIT; using i64 = std::int_fast64_t; using i128 = __int128_t; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = std::pair; using pll = std::pair; using vi = std::vector; using vvi = std::vector>; using vl = std::vector; using vp = std::vector; constexpr int INFINT = (1 << 30) - 1; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18 constexpr ld eps = 1e-6; constexpr ll MOD = 1000000007; constexpr ld PI = 3.141592653589793238462643383279; constexpr int dx4[4] = {1, 0, -1, 0}; constexpr int dy4[4] = {0, 1, 0, -1}; constexpr int dx8[8] = {1, 0, -1, 0, 1, -1, -1, 1}; constexpr int dy8[8] = {0, 1, 0, -1, 1, 1, -1, -1}; signed main(void) { int N; cin >> N; vl A(N); rep(i,N) cin >> A[i]; ll ans = INFLL; bool ok = false; vector visited(N); auto dfs = [&](auto && dfs, int v, vl t) -> void { if (v == N) { if (t.size()<3) return; ll a = t[0]; ll b = t[1]; ll c = t[2]; if (max({a,b,c}) == b || min({a,b,c}) == b) { if (a != b && b != c && c != a) { chmin(ans,a+b+c); //dump(ans,a,b,c); ok = true; } } return; } if (t.size()<3) { t.pb(A[v]); dfs(dfs,v+1,t); t.pop_back(); } dfs(dfs,v+1,t); }; dfs(dfs,0,{}); // int k = 3; // int x, y; // for (int bit = (1 << k) - 1; bit < (1 << N); x = bit & -bit, y = bit + x, bit = (((bit & ~y) / x) >> 1) | y) { // vl t; // rep(i,N) { // if (bit&(1 << i)) t.pb(A[i]); // } // ll a = t[0]; // ll b = t[1]; // ll c = t[2]; // if (max({a,b,c}) != b && min({a,b,c}) != b){ // continue; // } // if (a != b && b != c && c != a) { // if(chmin(ans,a+b+c)) dump(ans,a,b,c); // ok = true; // } // } if (!ok) plexit("-1") pl(ans) return 0; }