Problem p03767 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double pi = 3.1415926535898;
// double pi=acos(-1.0);
#define speed                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define lop(i, n) for (int i = 0; i < int(n); i++)
#define rep(i, a, b)                                                           \
  for (int i = a; i <= int(b); ++i) /*                                         \
#define rng(i,a,b) for(int i=a;i>=int(b);i--)                                  \
#define ms(x,a)    memset(x,a,sizeof(x))                                       \
#define all(x)     (x).begin(),(x).end()                                       \
#define sz(x)      int(x.size())                                               \
#define pb         push_back                                                   \
#define r0         return 0;                                                   \
#define CC         continue;                                                   \
#define bb(x)      cerr<< x <<endl;                                            \
#define pii        pair<int,int>                                               \
#define pis        pair<int,string>                                            \
#define vi         vector<int>                                                 \
#define F          first                                                       \
#define S          second                                                      \
//#define max(a,b) a<b?b:a  __builtin_popcount;                                \
//#define min(a,b) a<b?a:b                                                     \
//freopen("output.txt","w",stdout),freopen("input.txt","r",stdin);             \
const ll M  = 1e9+7 ;                                                          \
const int N = 100100;                                                          \
const int inf = INT_MAX;                                                       \
const double eps = 1e-7;                                                       \
//////////////////////    Hi    ^_^  hack me if you can :P                     \
*/

ll a[100100];
int n;
ll ans;
int main() {
  cin >> n;
  int m = n;
  m = 2 * n;
  n *= 3;
  lop(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  lop(i, m) {
    // cout << a[i] << ' ';
    if (i & 1) {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}

/*
https://ideone.com/goT6Vy//http:
codeforces.com/problemset/problem/466/D

, vector < pair <int,int > >, greater <pair <int, int> >
http://codeforces.com/contest/295/submission/17929453
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
    ll ret = 1;
    while (n)
    {
        if (n & 1) ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ret;
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P

int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};
bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> pbds;

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
    if(!m)
    {
        d=n;
        xx=1;
        yy=0;
    }
    else
    {
        gcd(m,n%m,d,yy,xx);
        yy-=xx*(n/m);
    }
}

int getm(int l,int r)
{
    if(l>r)
        return N;
    if(l==r)
        return 0;
    if(l==r-1)
        return s[l] != s[r];
    if(s[l]==s[r])
        return getm(l+1,r-1);
    else
        return 1+min(getm(l+1,r),getm(l,r-1));
}

int BIT[si], a[si], n;
void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
int query(int x)
{
    int sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

#define sc(x)      scanf("%d",&x)
#define scl(x)     scanf("%lld",&x)
#define pr(x)      printf("%d ",x)
#define prl(x)     printf("%lld ",x)
#define prln(x)    printf("%d\n",x)
#define prlln(x)   printf("%lld\n",x)
#define scd(x)     scanf("%lf",&x)
#define prd(x)     printf("%.10f\n",x)

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
    if( L == R )
    {
        seg[nd]=a[L];
        return ;
    }
    int mid = (L+R) >>1;
    build(nd<<1,L, mid);
    build(nd<<1|1,mid+1,R);
    seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
    if( L == R )
    {
        seg[nd]=val;
        return ;
    }
    int mid = (L+R) >>1;
    if( idx<=mid )
        update(nd<<1,  L,mid,idx,val);
    else update(nd<<1|1,mid+1, R,idx,val);
    seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
    if( from<=L && R<=to )return seg[nd];
    if( from>R || to<L )return -inf;

    int mid = (L+R) >>1;

    int leftAns = query(nd<<1,  L,mid,from,to);
    int rightAns= query(nd<<1|1,mid+1, R,from,to);
    return max( leftAns,rightAns );
}


*/


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double pi = 3.1415926535898;
// double pi=acos(-1.0);
#define speed                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define lop(i, n) for (int i = 0; i < int(n); i++)
#define rep(i, a, b)                                                           \
  for (int i = a; i <= int(b); ++i) /*                                         \
#define rng(i,a,b) for(int i=a;i>=int(b);i--)                                  \
#define ms(x,a)    memset(x,a,sizeof(x))                                       \
#define all(x)     (x).begin(),(x).end()                                       \
#define sz(x)      int(x.size())                                               \
#define pb         push_back                                                   \
#define r0         return 0;                                                   \
#define CC         continue;                                                   \
#define bb(x)      cerr<< x <<endl;                                            \
#define pii        pair<int,int>                                               \
#define pis        pair<int,string>                                            \
#define vi         vector<int>                                                 \
#define F          first                                                       \
#define S          second                                                      \
//#define max(a,b) a<b?b:a  __builtin_popcount;                                \
//#define min(a,b) a<b?a:b                                                     \
//freopen("output.txt","w",stdout),freopen("input.txt","r",stdin);             \
const ll M  = 1e9+7 ;                                                          \
const int N = 100100;                                                          \
const int inf = INT_MAX;                                                       \
const double eps = 1e-7;                                                       \
//////////////////////    Hi    ^_^  hack me if you can :P                     \
*/

ll a[300300];
int n;
ll ans;
int main() {
  cin >> n;
  int m = n;
  m = 2 * n;
  n *= 3;
  lop(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  lop(i, m) {
    // cout << a[i] << ' ';
    if (i & 1) {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}

/*
https://ideone.com/goT6Vy//http:
codeforces.com/problemset/problem/466/D

, vector < pair <int,int > >, greater <pair <int, int> >
http://codeforces.com/contest/295/submission/17929453
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
    ll ret = 1;
    while (n)
    {
        if (n & 1) ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ret;
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P

int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};
bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> pbds;

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
    if(!m)
    {
        d=n;
        xx=1;
        yy=0;
    }
    else
    {
        gcd(m,n%m,d,yy,xx);
        yy-=xx*(n/m);
    }
}

int getm(int l,int r)
{
    if(l>r)
        return N;
    if(l==r)
        return 0;
    if(l==r-1)
        return s[l] != s[r];
    if(s[l]==s[r])
        return getm(l+1,r-1);
    else
        return 1+min(getm(l+1,r),getm(l,r-1));
}

int BIT[si], a[si], n;
void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
int query(int x)
{
    int sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

#define sc(x)      scanf("%d",&x)
#define scl(x)     scanf("%lld",&x)
#define pr(x)      printf("%d ",x)
#define prl(x)     printf("%lld ",x)
#define prln(x)    printf("%d\n",x)
#define prlln(x)   printf("%lld\n",x)
#define scd(x)     scanf("%lf",&x)
#define prd(x)     printf("%.10f\n",x)

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
    if( L == R )
    {
        seg[nd]=a[L];
        return ;
    }
    int mid = (L+R) >>1;
    build(nd<<1,L, mid);
    build(nd<<1|1,mid+1,R);
    seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
    if( L == R )
    {
        seg[nd]=val;
        return ;
    }
    int mid = (L+R) >>1;
    if( idx<=mid )
        update(nd<<1,  L,mid,idx,val);
    else update(nd<<1|1,mid+1, R,idx,val);
    seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
    if( from<=L && R<=to )return seg[nd];
    if( from>R || to<L )return -inf;

    int mid = (L+R) >>1;

    int leftAns = query(nd<<1,  L,mid,from,to);
    int rightAns= query(nd<<1|1,mid+1, R,from,to);
    return max( leftAns,rightAns );
}


*/