Problem:
問題 G : プログラミングコンテストチャレンジブック
今，G○○gle Code Jam の地区大会が始まろうとしている．
前の席に座っている男の ID は omeometo と言うらしい．
後ろの席に座っている男の ID は jellies と言うらしい．
東京大学時代の記憶に，似たような ID の仲間が居た覚えがあるが，僕の仲間は一人残さず美少女だったはずだ．
彼らは，机の上に蟻のイラストが掛かれた本を持っている．
あれはもしや…プログラミングコンテストのアルゴリズムを最初に網羅し，今では発売禁止となった伝説の本，
「プログラミングコンテストチャレンジブック」ではないか？
僕は，omeometo がトイレに行ったのを見計らい，少し拝借して，読んでみることにした．
プログラミングコンテストチャレンジブック
…しかし，僕は一瞬で本を読み終えてしまった．
拍子抜けだ．簡単すぎる．
少し古い本だからといって，こんな内容でよく売る気になったものだ．
例えば，最初の三角形の問題．これは，簡単すぎてお話にならない．自分だったら，こうする．
問題
N
本の直線状の棒がある．棒
i
の長さは
a
i
である．
あなたは，それらの棒から 6 本を選び，
それらの 3 本ずつで，2 個の三角形を作ろうと考えている．
3 本の棒はそれぞれ三角形の辺として用い，2 つの棒が触れる位置は棒の端点のみとする．
つまり，棒の一部を三角形の辺として使うことは許されず，必ず棒全体を辺としなければならない．
また，棒の太さは考えず，三角形は正の面積を持たなければならないものとする．
2 個の三角形の周長の和の最大値を求めよ．
ただし，2 個の三角形を作ることができない際には 0 を答えとせよ．
入力
入力の最初の行には整数
N
が書かれている．
続く
N
行の
i
行目には 1 つの整数
a
i
が書かれている．
出力
答えの整数を出力せよ．
制約
1 ≤
N
≤ 10
5
1 ≤
a
i
≤ 10
15
部分点
この問題の判定には，20 点分のテストケースのグループが設定されている．
このグループに含まれるテストケースの入力は以下を満たす．
1 ≤
N
≤ 100
入出力例
入力例:
6
1
1
1
1
1
1
入力例に対する出力:
6

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

bool check3(vector<int> a) {
    //for (int i: a) cout << i << ' '; cout << endl;
	sort(a.begin(), a.end());
	return a[2] < a[1] + a[0];
}

bool check6(vector<int> a) {
    //for (int i: a) cout << i << ' '; cout << endl;
    sort(a.begin(), a.end());
	for (int i = 2; i < 5; i++) {
		vector<int> p;
		for (int j = 0; j < 5; j++) {
		    if (i != j) p.push_back(j);
		}
		
		do {
		    vector<int> s(2); 
		    if (p[0] < i && p[1] < i) s[0] = a[p[0]] + a[p[1]] - a[i];
		    if (p[2] < 5 && p[3] < 5) s[1] = a[p[2]] + a[p[3]] - a[5];
		    
		    if (s[0] > 0 && s[1] > 0) return true;
		} while (next_permutation(p.begin(), p.end()));
	}
	return false;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	
	vector<int> ans2;
	for (int i = 0; i+5 < n; i++) {
	    vector<int> tmp;
	    for (int j = 0; j < 6; j++) {
	        tmp.push_back(a[i+j]);
	    }
	    if (check6(tmp)) {
	        ans2 = tmp;
	    }
	}
	
	vector<int> ans;

	while (a.size() >= 3) {
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();

		if (check3(ans)) break;
		else {
			a.push_back(ans.back()); ans.pop_back();
			a.push_back(ans.back()); ans.pop_back();
			ans.pop_back();
		}
	}

	while (a.size() >= 3) {
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();

		if (check6(ans)) break;
		else {
			a.push_back(ans.back()); ans.pop_back();
			a.push_back(ans.back()); ans.pop_back();
			ans.pop_back();
		}
	}

	cout << max(accumulate(ans.begin(), ans.end(), 0LL), accumulate(ans2.begin(), ans2.end(), 0LL)) << '\n';

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3880
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll N;
    cin >> N;
    vector<ll> a(N);
    for(auto &x: a) cin >> x;
    
    // Сортируем в неубывающем порядке
    sort(a.begin(), a.end(), [](const ll &x, const ll &y) -> bool{
        return x > y;
    });
    
    // Если палочек меньше 6, выводим 0
    if(N < 6){
        cout << 0 << endl;
        return 0;
    }
    
    // Выбираем первые K палочек
    ll K = min((ll)30, N);
    vector<ll> topK(a.begin(), a.begin() + K);
    
    // Перебираем все возможные тройки и сохраняем валидные
    struct Triple {
        int i, j, k;
        ll sum;
    };
    
    vector<Triple> triples;
    
    for(int i = 0; i < K-2; ++i){
        for(int j = i+1; j < K-1; ++j){
            for(int k = j+1; k < K; ++k){
                if(topK[j] + topK[k] > topK[i]){
                    triples.push_back(Triple{ i, j, k, topK[i] + topK[j] + topK[k] });
                }
            }
        }
    }
    
    // Если меньше двух тройк, невозможно
    if(triples.size() < 2){
        // Возможно, стоит проверить дальше, но для упрощения ограничимся K=30
        cout << 0 << endl;
        return 0;
    }
    
    // Ищем максимальную сумму двух не пересекающихся тройк
    ll max_sum = 0;
    for(int i = 0; i < triples.size(); ++i){
        for(int j = i+1; j < triples.size(); ++j){
            // Проверяем, не пересекаются ли тройки
            int a1 = triples[i].i;
            int b1 = triples[i].j;
            int c1 = triples[i].k;
            
            int a2 = triples[j].i;
            int b2 = triples[j].j;
            int c2 = triples[j].k;
            
            // Проверяем уникальность индексов
            if(a1 != a2 && a1 != b2 && a1 != c2 &&
               b1 != a2 && b1 != b2 && b1 != c2 &&
               c1 != a2 && c1 != b2 && c1 != c2){
                   ll total = triples[i].sum + triples[j].sum;
                   if(total > max_sum){
                       max_sum = total;
                   }
               }
        }
    }
    
    // Выводим результат
    cout << max_sum << endl;
}
accuracy: 0.136986301369863
time_ms: 10
memory_kb: 3592
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll N;
    cin >> N;
    vector<ll> a(N);
    for(auto &x: a) cin >> x;
    
    // Сортируем в неубывающем порядке
    sort(a.begin(), a.end(), [](const ll &x, const ll &y) -> bool{
        return x > y;
    });
    
    // Если палочек меньше 6, выводим 0
    if(N < 6){
        cout << "0";
        return 0;
    }
    
    // Выбираем первые K палочек
    ll K = min((ll)30, N);
    vector<ll> topK(a.begin(), a.begin() + K);
    
    // Перебираем все возможные тройки и сохраняем валидные
    struct Triple {
        int i, j, k;
        ll sum;
    };
    
    vector<Triple> triples;
    
    for(int i = 0; i < K-2; ++i){
        for(int j = i+1; j < K-1; ++j){
            for(int k = j+1; k < K; ++k){
                if(topK[j] + topK[k] > topK[i]){
                    triples.push_back(Triple{ i, j, k, topK[i] + topK[j] + topK[k] });
                }
            }
        }
    }
    
    // Если меньше двух тройк, невозможно
    if(triples.size() < 2){
        // Возможно, стоит проверить дальше, но для упрощения ограничимся K=30
        cout << "0";
        return 0;
    }
    
    // Ищем максимальную сумму двух не пересекающихся тройк
    ll max_sum = 0;
    for(int i = 0; i < triples.size(); ++i){
        for(int j = i+1; j < triples.size(); ++j){
            // Проверяем, не пересекаются ли тройки
            int a1 = triples[i].i;
            int b1 = triples[i].j;
            int c1 = triples[i].k;
            
            int a2 = triples[j].i;
            int b2 = triples[j].j;
            int c2 = triples[j].k;
            
            // Проверяем уникальность индексов
            if(a1 != a2 && a1 != b2 && a1 != c2 &&
               b1 != a2 && b1 != b2 && b1 != c2 &&
               c1 != a2 && c1 != b2 && c1 != c2){
                   ll total = triples[i].sum + triples[j].sum;
                   if(total > max_sum){
                       max_sum = total;
                   }
               }
        }
    }
    
    // Выводим результат
    cout << max_sum << endl;
}
accuracy: 0.1232876712328767
time_ms: 10
memory_kb: 3588
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    ll res = 0;
    auto can = [&](ll x, ll y, ll z) -> ll {
        if (x + y > z) return x + y + z;
        else return -1e18;
    };
    {
        ll mx = -1e18;
        for (int i = 5; i < n; i++) {
            mx = max(mx, can(a[i - 5], a[i - 4], a[i - 3]));
            ll u = can(a[i - 2], a[i - 1], a[i]);
            res = max(res, u + mx);
        }
    }
    {
        for (int i = 0; i + 5 < n; i++) {
            res = max(res, can(a[i], a[i + 1], a[i + 3]) + can(a[i + 2], a[i + 4], a[i + 5]));
            res = max(res, can(a[i], a[i + 1], a[i + 4]) + can(a[i + 2], a[i + 3], a[i + 5]));
            res = max(res, can(a[i], a[i + 1], a[i + 5]) + can(a[i + 2], a[i + 3], a[i + 4]));
            res = max(res, can(a[i], a[i + 2], a[i + 3]) + can(a[i + 1], a[i + 4], a[i + 5]));
            res = max(res, can(a[i], a[i + 2], a[i + 4]) + can(a[i + 1], a[i + 3], a[i + 5]));
            res = max(res, can(a[i], a[i + 2], a[i + 5]) + can(a[i + 1], a[i + 3], a[i + 4]));
            res = max(res, can(a[i], a[i + 3], a[i + 4]) + can(a[i + 1], a[i + 2], a[i + 5]));
            res = max(res, can(a[i], a[i + 3], a[i + 5]) + can(a[i + 1], a[i + 2], a[i + 4]));
            res = max(res, can(a[i], a[i + 4], a[i + 5]) + can(a[i + 1], a[i + 2], a[i + 3]));
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3896
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;

int main() {
  int n;
  cin >> n;
  if(n<6){
    cout << 0 << endl;
    return 0;
  }
  
  priority_queue<ll> pq;
  ll a;
  
  for(int i=0;i<n;i++){
    cin >> a;
    pq.push(a);
  }
  
  ll ans1=0,ans=0;
  deque<ll> san; 
  int cnt=0,num=0;
  while(!pq.empty()){
    //cout << pq.size() << " " << cnt << " " << num << endl;
    if(num==0){
      if(cnt<3){ 
        san.push_back(pq.top());
        pq.pop();
        cnt++;
      }
      if(cnt==3){
        if(san[0]<san[1]+san[2]){
          ans1=san[0]+san[1]+san[2];
          //cout << ans1 << " " << pq.size() << endl;
          cnt=0;
          num++;
        }else{
          san.pop_front();
          cnt--;
        }
      }
    }else{
      if(cnt<3){ 
        san.push_back(pq.top());
        pq.pop();
        cnt++;
      }
      if(cnt==3){
        if((san[3]<san[4]+san[5])||
          ((san[1]<san[2]+san[5])&&(san[0]<san[3]+san[4]))||
          ((san[2]<san[3]+san[5])&&(san[0]<san[1]+san[4]))||
          ((san[2]<san[3]+san[4])&&(san[0]<san[1]+san[5]))||
          ((san[2]<san[4]+san[5])&&(san[0]<san[1]+san[3]))||
          ((san[1]<san[4]+san[5])&&(san[0]<san[2]+san[3]))||
          ((san[1]<san[3]+san[5])&&(san[0]<san[2]+san[4]))||
          ((san[1]<san[3]+san[4])&&(san[0]<san[2]+san[5]))||
          ((san[1]<san[2]+san[4])&&(san[0]<san[3]+san[5]))||
          ((san[1]<san[3]+san[4])&&(san[0]<san[4]+san[5]))){
           ans=ans1+san[3]+san[4]+san[5];
           //for(int i=0;i<6;i++) cout << san[i] << endl; 
        }else{
          san[3]=san[4];
          san[4]=san[5];
          san[5]=pq.top();
          pq.pop();
          if(san[3]<san[4]+san[5]) ans=ans1+san[3]+san[4]+san[5];
        }
      }
      
    }
    if(ans>0) break;
  }
  
  cout << max(ans,ans1) << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4204
final_rank: 12

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;


#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()

#define MOD 1000000007

template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-9, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
   
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;

  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }

  inline T fact(int k) const { return _fact[k]; }

  inline T rfact(int k) const { return _rfact[k]; }

  inline T inv(int k) const { return _inv[k]; }

  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }

  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }

  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
struct UnionFind{
  vector<int> par;
  vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
bool used[101010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int N; cin >> N;
  vector<ll> a(N);
  REP(i,N) cin >> a[i];
  sort(all(a));
  ll t1=-1, t2=-1;
  ll ans = 0;
  for(int i=N-1; i>=2; i--){
    if(used[i]) continue;
    ll e1 = a[i], e2 = a[i-1], e3 = a[i-2];
    if(e2+e3 > e1) {
      if(t1 == -1){
        t1 = e1+e2+e3;
        used[i] = used[i-1] = used[i-2] = true;
      }else{
        t2 = e1+e2+e3;
        break;
      }
    }
  }
  if(t2 != -1) ans = t1+t2;
  for(int i=N-1; i>=5; i--){
    vector<ll> v{a[i-5], a[i-4], a[i-3], a[i-2], a[i-1], a[i]};
    ll sum = accumulate(all(v), 0LL);

    vector<bool> ok(1<<6);
    REP(bit,(1<<6)){
      if(__builtin_popcount(bit) != 3) continue;
      vector<ll> e;
      for(int j=0; j<6; j++){
        if(bit >> j & 1) e.push_back(v[j]);
      }

      if(e[0]+e[1]>e[2] && e[0]+e[2]>e[1] && e[1]+e[2]>e[0]){
        ok[bit] = true;
      }
    }
    REP(bit,(1<<6)){
      if(__builtin_popcount(bit) != 3) continue;
      if(ok[bit] && ok[((1<<6)-1) & ~bit]){
        ans = max(ans, sum);
        break;
      } 
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3624
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

bool check3(vector<int> a) {
    //for (int i: a) cout << i << ' '; cout << endl;
	sort(a.begin(), a.end());
	return a[2] < a[1] + a[0];
}

bool check6(vector<int> a) {
    //for (int i: a) cout << i << ' '; cout << endl;
    sort(a.begin(), a.end());
	for (int i = 2; i < 5; i++) {
		vector<int> p;
		for (int j = 0; j < 5; j++) {
		    if (i != j) p.push_back(j);
		}
		
		do {
		    vector<int> s(2); 
		    s[0] = a[p[0]] + a[p[1]] - a[i];
		    s[1] = a[p[2]] + a[p[3]] - a[5];
		    
		    if (s[0] > 0 && s[1] > 0) return true;
		} while (next_permutation(p.begin(), p.end()));
	}
	return false;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	
	vector<int> ans2;
	for (int i = 0; i+5 < n; i++) {
	    vector<int> tmp;
	    for (int j = 0; j < 6; j++) {
	        tmp.push_back(a[i+j]);
	    }
	    if (check6(tmp)) {
	        ans2 = tmp;
	    }
	}
	
	vector<int> ans;

	while (a.size() >= 3) {
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();

		if (check3(ans)) break;
		else {
			a.push_back(ans.back()); ans.pop_back();
			a.push_back(ans.back()); ans.pop_back();
			ans.pop_back();
		}
	}

	while (a.size() >= 3) {
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();
		ans.push_back(a.back()); a.pop_back();

		if (check6(ans)) break;
		else {
			a.push_back(ans.back()); ans.pop_back();
			a.push_back(ans.back()); ans.pop_back();
			ans.pop_back();
		}
	}

	cout << max(accumulate(ans.begin(), ans.end(), 0LL), accumulate(ans2.begin(), ans2.end(), 0LL)) << '\n';

	return 0;
}
accuracy: 0.6438356164383562
time_ms: 30
memory_kb: 3552
final_rank: 18

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int calc_left[20][3],calc_right[20][3];

bool check(ll a,ll b, ll c){
	return a+b > c;
}

void divideGroup(){

	int index = 0,left_index,right_index;
	int left_table[6],right_table[6];

	for(int i = 0; i < 64; i++){
		left_index = right_index = 0;
		for(int loop = 0; loop < 6; loop++){
			if(i & (1 << loop)){
				left_table[left_index++] = loop;
			}else{
				right_table[right_index++] = loop;
			}
		}
		if(left_index != 3 || right_index != 3)continue;

		for(int k = 0; k < 3; k++){
			calc_left[index][k] = left_table[k];
			calc_right[index][k] = right_table[k];
		}
		index++;
	}

}

int main(){

	divideGroup();

	int N;
	scanf("%d",&N);

	ll* table = new ll[N];
	for(int i = 0; i < N; i++)scanf("%lld",&table[i]);

	sort(table,table+N);

	ll* max_3seq = new ll[N];
	for(int i = 0; i < N; i++)max_3seq[i] = 0;

	for(int i = 0; i <= N-3; i++){
		if(check(table[i],table[i+1],table[i+2])){
			max_3seq[i] = table[i]+table[i+1]+table[i+2];
		}else{
			if(i > 0){
				max_3seq[i] = max_3seq[i-1];
			}
		}
	}

	ll ans = 0;
	int debug;
	for(int i = 0; i+5 <= N-1; i++){
		if(check(table[i+3],table[i+4],table[i+5])){
			ans = max(ans,max_3seq[i]+table[i+3]+table[i+4]+table[i+5]);
		}
		bool FLG = false;

		for(int k = 0; k < 20; k++){
			if(check(table[i+calc_left[k][0]],table[i+calc_left[k][1]],table[i+calc_left[k][2]]) == true &&
					check(table[i+calc_right[k][0]],table[i+calc_right[k][1]],table[i+calc_right[k][2]]) == true){
				FLG = true;
				debug = k;
				break;
			}
		}

		if(FLG){
			ans = max(ans,table[i]+table[i+1]+table[i+2]+table[i+3]+table[i+4]+table[i+5]);
		}
	}


	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4852
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

bool tri(const vector<ll> &x)
{
    return x[0]+x[1]>x[2];
}

bool check(const vector<ll> &v)
{
    // dbg(v);
    int comb = (1<<3) - 1;
    while(comb < (1<<6)-1)
    {
        vector<ll> p,q;
        rep(i,6)
        {
            if(comb>>i&1) p.pb(v[i]);
            else q.pb(v[i]);
        }
        // dbg(p);dbg(q);
        if(tri(p) && tri(q)) return true;

        int x = comb & -comb, y = comb + x;
        comb = ((comb & ~y) / x>>1) | y;
    }
    return false;
}

ll SUM(const vector<ll> &v)
{
    ll ret = 0;
    for(ll i:v) ret += i;
    return ret;
}

int main()
{
    int n;
    cin >>n;
    vector<ll> a(n);
    rep(i,n) cin >>a[i];
    sort(all(a));

    vector<int> ok;
    for(int i=2; i<n; ++i)
    {
        vector<ll> v;
        rep(j,3) v.pb(a[i-2+j]);
        if(tri(v)) ok.pb(i);
    }

    ll ans = 0;
    rep(i,n-5)
    {
        vector<ll> v;
        rep(j,6) v.pb(a[i+j]);
        if(check(v)) ans = max(ans,SUM(v));
    }

    if(ok.size()>0)
    {
        for(int i=n-1; i>=2; --i)
        {
            if(*lower_bound(all(ok),i) == i)
            {
                int idx = lower_bound(all(ok),i-2)-ok.begin();
                --idx;
                if(idx>=0)
                {
                    vector<ll> v;
                    rep(j,3) v.pb(a[ok[idx]-2+j]);
                    rep(j,3) v.pb(a[i-2+j]);
                    ans = max(ans,SUM(v));
                }
            }
        }
    }
    
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 4328
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n; cin >> n;
    vector<ll> v(n); cin >> v;
    sort(all(v), greater<ll>());
    ll ans = 0;
    { // xxxooo
        ll a = 0, k = 0;
        rep(i, n-2) {
            if (v[i] < v[i+1] + v[i+2]) {
                a += v[i] + v[i+1] + v[i+2];
                i += 2;
                if (++k == 2) {
                    chmax(ans, a);
                }
            }
        }
    }
    {
        rep(i, n-5) {
            if (v[i] < v[i+1] + v[i+5] && v[i+2] < v[i+3] + v[i+4] // xxooox
             || v[i] < v[i+1] + v[i+4] && v[i+2] < v[i+3] + v[i+5] // xxooxo
             || v[i] < v[i+3] + v[i+4] && v[i+1] < v[i+2] + v[i+5]) { // xooxxo
                ll sum = 0;
                rep(j, 6) sum += v[i+j];
                chmax(ans, sum);
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3620
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n; long long a[100009];
long long solve(int l, int r) {
	long long ret = 0;
	for(int i = 0; i < 1 << (r - l); i++) {
		if(__builtin_popcount(i) != 3) continue;
		vector<long long> x1, x2;
		for(int j = 0; j < 6; j++) {
			if(i & (1 << j)) x1.push_back(a[l + j]);
			else x2.push_back(a[l + j]);
		}
		if(x1[0] + x1[1] > x1[2] && x2[0] + x2[1] > x2[2]) {
			ret = max(ret, x1[0] + x1[1] + x1[2] + x2[0] + x2[1] + x2[2]);
		}
	}
	return ret;
}
int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i];
	sort(a, a + n);
	int pos = -1;
	for(int i = n - 3; i >= 0; i--) {
		if(a[i] + a[i + 1] > a[i + 2]) {
			pos = i; break;
		}
	}
	int pos2 = -1;
	for(int i = pos - 3; i >= 0; i--) {
		if(a[i] + a[i + 1] > a[i + 2]) {
			pos2 = i; break;
		}
	}
	long long ret = (pos2 != -1 ? a[pos2] + a[pos2 + 1] + a[pos2 + 2] + a[pos] + a[pos + 1] + a[pos + 2] : 0);
	for(int i = 0; i < n - 6; i++) {
		ret = max(ret, solve(i, i + 6));
	}
	cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 3952
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX_N 500000
#define MAX_S 600
long long x[MAX_N];
long long TOP[MAX_S], n;
long long maxn1[MAX_N], maxn2[MAX_N];
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x[i];
	}
	sort(x, x + n);
	long long cnt = 0;
	for (int i = n - MAX_S; i < n; i++) {
		if (i >= 0) {
			TOP[cnt] = x[i];
			cnt++;
		}
	}
	long long maxn = 0;
	for (int i = 0; i < cnt; i++) {
		for (int j = i + 1; j < cnt; j++) {
			for (int k = j + 1; k < cnt; k++) {
				if (TOP[i] + TOP[j] > TOP[k]) {
					maxn1[i] = max(maxn1[i], TOP[i] + TOP[j] + TOP[k]);
					maxn2[k] = max(maxn2[k], TOP[i] + TOP[j] + TOP[k]);
				}
			}
		}
	}
	for (int i = 0; i < cnt; i++) {
		for (int j = i + 1; j < cnt; j++) {
			maxn = max(maxn, maxn2[i] + maxn1[j]);
		}
	}

	for (int i = 0; i < cnt - 5; i++) {
		long long c[6], d[3], e[3];
		for (int j = 0; j < 6; j++) {
			c[j] = TOP[i + j];
		}
		long long cnt = 0, cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < 64; j++) {
			cnt = 0;
			for (int k = 0; k < 6; k++) { cnt += (j / (1 << k)) % 2; }
			if (cnt == 3) {
				cnt1 = 0; cnt2 = 0;
				for (int k = 0; k < 6; k++) {
					if ((j / (1 << k)) % 2 == 0) {
						d[cnt1] = c[k];
						cnt1++;
					}
					else {
						e[cnt2] = c[k];
						cnt2++;
					}
				}
				if (d[0] + d[1]>d[2] && e[0] + e[1]>e[2]) {
					maxn = max(maxn, d[0] + d[1] + d[2] + e[1] + e[2] + e[0]);
				}
			}
		}
	}
	cout << maxn << endl;
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 7320
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
	int N; cin >> N;
	vector<ll> v(N);
	for (int i = 0; i < N; ++i) {
		cin >> v[i];
	}
	sort( v.begin(), v.end() );

	ll ans = 0;

	// 012345
	// oooxox
	// ooooxx
	for (int t = N-6; t >= 0; --t) {
		vector<ll> a(6);
		for (int i = 0; i < 6; ++i) {
			a[i] = v[t+i];
		}

		ll sum = 0;
		for (int i = 0; i < 6; ++i) {
			sum += a[i];
		}

		if ((a[0] + a[1] > a[3] && a[2] + a[4] > a[5])
			|| (a[0] + a[2] > a[3] && a[1] + a[4] > a[5])
			|| (a[1] + a[2] > a[3] && a[0] + a[4] > a[5])
			|| (a[0] + a[1] > a[4] && a[2] + a[3] > a[5])
			|| (a[0] + a[2] > a[4] && a[1] + a[3] > a[5])
			|| (a[0] + a[3] > a[4] && a[1] + a[2] > a[5])
			|| (a[0] + a[1] > a[5] && a[2] + a[3] > a[4])
			|| (a[0] + a[2] > a[5] && a[1] + a[3] > a[4])
			|| (a[0] + a[3] > a[5] && a[1] + a[2] > a[4])) {
			if (sum > ans) ans = sum;
		}
	}
	{
		ll s = 0;

		int pos = -1;
		for (int i = N-3; i >= 0; --i) {
			if (v[i] + v[i+1] > v[i+2]) {
				s = v[i] + v[i+1] + v[i+2];
				pos = i;
				break;
			}
		}
		if (pos > -1) {
			for (int i = pos-3; i >= 0; --i) {
				if (v[i] + v[i+1] > v[i+2]) {
					ll s2 = s + v[i] + v[i+1] + v[i+2];
					if (s2 > ans) ans = s2;
				}
			}
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 1796
final_rank: 3

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef long long ll;

/* global variables */

ll as[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) cin >> as[i];
  sort(as, as + n, greater<ll>());

  ll maxsum0 = 0, maxsum1 = 0;
  ll sumi, sumj;
  int i;
  
  for (i = 0; i < n - 5; i++)
    if (as[i] < as[i + 1] + as[i + 2]) {
      sumi = as[i] + as[i + 1] + as[i + 2];
      //printf("%d:%lld+%lld+%lld=%lld\n",i,as[i],as[i+1],as[i+2],sumi);
      break;
    }

  for (int j = i + 3; j < n - 2; j++)
    if (as[j] < as[j + 1] + as[j + 2]) {
      sumj = as[j] + as[j + 1] + as[j + 2];
      maxsum0 = sumi + sumj;
      //printf("%d:%lld+%lld+%lld=%lld\n",j,as[j],as[j+1],as[j+2],sumj);
      break;
    }

  ll sum = as[0] + as[1] + as[2] + as[3] + as[4];
  for (i = 0; i < n - 5; i++) {
    sum += as[i + 5];
    if ((as[i] < as[i + 1] + as[i + 2] && as[i + 3] < as[i + 4] + as[i + 5]) ||
	(as[i] < as[i + 1] + as[i + 3] && as[i + 2] < as[i + 4] + as[i + 5]) ||
	(as[i] < as[i + 1] + as[i + 4] && as[i + 2] < as[i + 3] + as[i + 5]) ||
	(as[i] < as[i + 1] + as[i + 5] && as[i + 2] < as[i + 3] + as[i + 4]) ||
	(as[i] < as[i + 2] + as[i + 3] && as[i + 1] < as[i + 4] + as[i + 5]) ||
	(as[i] < as[i + 2] + as[i + 4] && as[i + 1] < as[i + 3] + as[i + 5]) ||
	(as[i] < as[i + 2] + as[i + 5] && as[i + 1] < as[i + 3] + as[i + 4]) ||
	(as[i] < as[i + 3] + as[i + 4] && as[i + 1] < as[i + 2] + as[i + 5]) ||
	(as[i] < as[i + 3] + as[i + 5] && as[i + 1] < as[i + 2] + as[i + 4]) ||
	(as[i] < as[i + 4] + as[i + 5] && as[i + 1] < as[i + 2] + as[i + 3])) {
      maxsum1 = sum;
      break;
    }
    sum -= as[i];
  }
  
  cout << max(maxsum0, maxsum1) << endl;
  
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1964
final_rank: 4

code_snippet: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v, w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 4;

int main() {
	int N; cin >> N;
	if (N < 6) {
		cout << 0 << endl;
		return 0;
	}
	vector<ll> a(N);
	for (int i = 0; i < N; i++)
		cin >> a[i];
	sort(a.begin(), a.end());
	vector<ll> b(N - 2);
	for (int i = 0; i + 3 <= N; i++)
		if (a[i] + a[i + 1] > a[i + 2])
			b[i] = a[i] + a[i + 1] + a[i + 2];
	vector<ll> c(N - 1);
	c[0] = 0;
	for (int i = 1; i - 1 < N - 2; i++)
		c[i] = max(c[i - 1], b[i - 1]);
	ll maxi = 0;
	for (int i = 3; i + 3 <= N; i++)
		if (c[i - 2] && b[i])
			maxi = max(maxi, c[i - 2] + b[i]);
	for (int i = N - 6; i >= 0; i--) {
		bool ok = false;
		for (int S = 0; S < (1 << 6); S++) {
			vector<ll> v, w;
			for (int j = 0; j < 6; j++)
				if ((S >> j) & 1) v.push_back(a[i + j]);
				else w.push_back(a[i + j]);
			if (v.size() == 3 && v[0] + v[1] > v[2] && w[0] + w[1] > w[2])
				ok = true;
		}
		if (ok) {
			maxi = max(maxi, a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5]);
			break;
		}
	}
	cout << maxi << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3384
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

inline bool check(ll a, ll b, ll c){ return a<b+c; }

int main(){
  ll n, a[100100];
  scanf("%lld",&n);
  for(int i=0;i<n;i++)scanf("%lld",a+i);
  sort(a,a+n,greater<ll>());

  ll ans = 0;
  for(int i=0;i<n-2;i++){
    if(check(a[i],a[i+1],a[i+2])){
      for(int j=i+3;j<n-2;j++){
	if(check(a[j],a[j+1],a[j+2])){
	  ll sum = 0;
	  for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];
	  ans = max(ans,sum);
	  goto END1;
	}
      }
    }
  }
 END1:;

  for(int i=0;i<n-5;i++){
    int comb = (1<<3)-1;
    while(comb < (1<<5)){
      vector<ll> A,B;
      for(int j=0;j<6;j++){
	if( (comb>>j)&1 )A.push_back(a[i+j]);
	else B.push_back(a[i+j]);
      }

      if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){
	ll sum = 0;
	for(int k=0;k<6;k++)sum += a[i+k];
	ans = max(ans,sum);
	goto END2;
      }

      int x = comb & -comb, y = comb + x;
      comb = ( ( (comb & ~y) /x ) >> 1) | y;
    }
  }

 END2: cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 2016
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

inline bool check(ll a, ll b, ll c){ return a<b+c; }

int main(){
  ll n, a[100100];
  cin >> n;
  for(int i=0;i<n;i++)cin >> a[i];
  sort(a,a+n,greater<ll>());

  ll ans = 0;
  for(int i=0;i<n-2;i++){
    if(check(a[i],a[i+1],a[i+2])){
      for(int j=i+3;j<n-2;j++){
	if(check(a[j],a[j+1],a[j+2])){
	  ll sum = 0;
	  for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];
	  ans = max(ans,sum);
	  goto END1;
	}
      }
    }
  }
 END1:;

  for(int i=0;i<n-5;i++){
    int comb = (1<<3)-1;
    while(comb < (1<<5)){
      vector<ll> A,B;
      for(int j=0;j<6;j++){
	if( (comb>>j)&1 )A.push_back(a[i+j]);
	else B.push_back(a[i+j]);
      }

      if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){
	ll sum = 0;
	for(int k=0;k<6;k++)sum += a[i+k];
	ans = max(ans,sum);
	goto END2;
      }

      int x = comb & -comb, y = comb + x;
      comb = ( ( (comb & ~y) /x ) >> 1) | y;
    }
  }

 END2: cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 1996
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

inline bool check(ll a, ll b, ll c){ return a<b+c; }

int main(){
  ll n, a[100100];
  cin >> n;
  for(int i=0;i<n;i++)cin >> a[i];
  sort(a,a+n,greater<ll>());

  ll ans = 0;
  for(int i=0;i<n-2;i++){
    if(check(a[i],a[i+1],a[i+2])){
      for(int j=i+3;j<n-2;j++){
	if(check(a[j],a[j+1],a[j+2])){
	  ll sum = 0;
	  for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];
	  ans = max(ans,sum);
	  goto END1;
	}
      }
    }
  }
 END1:;

  for(int i=0;i<n-5;i++){
    int comb = (1<<3)-1;
    while(comb < (1<<6)){
      vector<ll> A,B;
      for(int j=0;j<6;j++){
	if( (comb>>j)&1 )A.push_back(a[i+j]);
	else B.push_back(a[i+j]);
      }

      if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){
	ll sum = 0;
	for(int k=0;k<6;k++)sum += a[i+k];
	ans = max(ans,sum);
	goto END2;
      }

      int x = comb & -comb, y = comb + x;
      comb = ( ( (comb & ~y) /x ) >> 1) | y;
    }
  }

 END2: cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 1996
final_rank: 5

code_snippet: #include<iostream>
#include<algorithm>

using namespace std;
typedef long long Int;



bool able(Int a, Int b, Int c){
	Int sum = a + b + c;
	return a*2 < sum && b*2 < sum && c*2 < sum;
}

bool able2(Int a, Int b, Int c, Int d, Int e, Int f){
	if(able(a, b, c) && able(d, e, f))return true;
	if(able(a, b, d) && able(c, e, f))return true;
	if(able(a, b, e) && able(c, d, f))return true;
	if(able(a, b, f) && able(c, d, e))return true;
	if(able(a, c, d) && able(b, e, f))return true;
	if(able(a, c, e) && able(b, d, f))return true;
	if(able(a, c, f) && able(b, d, e))return true;
	if(able(a, d, e) && able(b, c, f))return true;
	if(able(a, d, f) && able(b, c, e))return true;
	if(able(a, e, f) && able(b, c, d))return true;
	return false;
}


Int n, res;
Int a[108000], right1 = -1, right2 = -1;
bool ok[108000];
int main(){
	cin >> n;
	for(Int i = 0;i < n;i++) cin >> a[i];
	sort(a, a + n);
	for(Int i = 2;i < n;i++){
		ok[i] = able(a[i], a[i-1], a[i-2]);
		if(ok[i])right1 = i;
	}
	for(Int i = 2;i <= right1 - 3;i++){
		if(ok[i])right2 = i;
	}
	if(right1 != -1 && right2 != -1)
		res = a[right1] + a[right1-1] + a[right1-2] + a[right2] + a[right2-1] + a[right2-2];
	for(Int i = 5;i < n;i++){
		if(able2(a[i], a[i-1], a[i-2], a[i-3], a[i-4], a[i-5])){
			res = max(res, a[i] + a[i-1] + a[i-2] + a[i-3] + a[i-4] + a[i-5]);
		}
	}
	cout << res << endl;
	return 0;

}
accuracy: 1
time_ms: 60
memory_kb: 2064
final_rank: 7

code_snippet: //Name: Programming Contest Challenge Book
//Level: 3
//Category: 数学,Math,考察
//Note:

/**
 * 棒を長さの降順でソートしてから考える。
 * 以下、三角形の辺に関して長い方からp,q,rと呼ぶことにする。
 *
 * 三角形を1つ作るとき、棒iを辺pとして採用すると、三角不等式のうちp+q>rとp+r>qが満たされることは自明である。
 * q+r>pであるためにはbとcを可能な限り大きく取ればよいから、結局棒i+1とi+2を加えて三角形を構成できなければ、棒iを辺pとした三角形は構成できないことがわかる。
 *
 * この考察から、連続する3辺で三角形が作れるかを考えながら貪欲に構成していくという方法が思いつくが、これでは3 3 2 2 1 1のような列のときにうまくいかない。
 * すなわち、a_{i+3}≧a_{i+4}+a_{i+5}であるが、2つの三角形に割り当てる順番をうまく入れ替えてやるとちょうど割り当てられる場合である。
 * このような場合は、棒iから連続する6つの棒だけを使って2つの三角形を構成しないと意味がないため、全探索すればよい
 * （周長を最大化する制約から、この6つよりも短い棒を使っている場合、iを超えない範囲で長い棒に入れ替えることは安全であり、かつ得をする）。
 *
 * オーダーは O(N log N)。
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>

using namespace std;

bool solve() {
    int N;
    if(!(cin >> N)) return false;

    vector<long long> as(N);
    for(int i = 0; i < N; ++i) {
        cin >> as[i];
    }
    sort(begin(as), end(as), greater<long long>());

    typedef vector<int> Choice;
    vector<pair<Choice,Choice>> choose_3;
    for(int pat = 0; pat < (1<<6); ++pat) {
        if(__builtin_popcount(pat) != 3) continue;
        Choice c1, c2;
        for(int i = 0; i < 6; ++i) {
            if(pat & (1<<i)) c1.push_back(i);
            else c2.push_back(i);
        }
        choose_3.emplace_back(c1, c2);
    }

    long long ans = 0;
    int composed = 0;
    for(int i = 0; i <= N-3; ++i) {
        // Check if all 6 sticks can be used
        if(composed == 0 && i <= N-6) {
            for(auto pat : choose_3) {
                const auto &c1 = pat.first;
                const auto &c2 = pat.second;
                if(as[i+c1[0]] < as[i+c1[1]]+as[i+c1[2]] && as[i+c2[0]] < as[i+c2[1]]+as[i+c2[2]]) {
                    ans = accumulate(begin(as)+i, begin(as)+i+6, 0LL);
                    composed = 2;
                    goto end;
                }
            }
        }
        // Check if 3 sticks can be used
        if(as[i] < as[i+1] + as[i+2]) {
            ans += as[i]+as[i+1]+as[i+2];
            if(++composed == 2) break;
            i += 2;
        }
    }
end:
    if(composed != 2) ans = 0;
    cout << ans << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1788
final_rank: 1