Problem p03767 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define REPD(i, n) for (int i = (n)-1; i >= 0; --i)

#define DEBUG(X)                                                               \
  { cerr << #X << " = " << (X) << endl; }
#define PR(A, n)                                                               \
  {                                                                            \
    cerr << #A << " = ";                                                       \
    FOR(_, 1, n) cerr << A[_] << ' ';                                          \
    cerr << endl;                                                              \
  }
#define PR0(A, n)                                                              \
  {                                                                            \
    cerr << #A << " = ";                                                       \
    REP(_, n) cerr << A[_] << ' ';                                             \
    cerr << endl;                                                              \
  }

#define sqr(x) ((x) * (x))
#define ll long long
#define double long double
typedef pair<int, int> II;
#define PI (2 * acos((double)0))
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
#define ALL(a) (a).begin(), (a).end()
#define MS(a, x) memset(a, x, sizeof(a))
#define next ackjalscjaowjico
#define prev ajcsoua0wucckjsl
#define y1 alkscj9u20cjeijc
#define left lajcljascjljl
#define right aucouasocjolkjl
#define y0 u9cqu3jioajc

#define TWO(X) (1LL << (X))
#define CONTAIN(S, X) ((S)&TWO(X))

double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); }
int GI(int &x) { return scanf("%lld", &x); }

int n, a[100111];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << (fixed) << setprecision(9) << boolalpha;
  while (cin >> n) {
    n *= 3;
    FOR(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    int res = 0;
    int i = n - 1;
    REP(turn, n / 3) {
      res += a[i];
      i -= 2;
    }
    cout << res << endl;
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define REPD(i, n) for (int i = (n)-1; i >= 0; --i)

#define DEBUG(X)                                                               \
  { cerr << #X << " = " << (X) << endl; }
#define PR(A, n)                                                               \
  {                                                                            \
    cerr << #A << " = ";                                                       \
    FOR(_, 1, n) cerr << A[_] << ' ';                                          \
    cerr << endl;                                                              \
  }
#define PR0(A, n)                                                              \
  {                                                                            \
    cerr << #A << " = ";                                                       \
    REP(_, n) cerr << A[_] << ' ';                                             \
    cerr << endl;                                                              \
  }

#define sqr(x) ((x) * (x))
#define ll long long
#define double long double
typedef pair<int, int> II;
#define PI (2 * acos((double)0))
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
#define ALL(a) (a).begin(), (a).end()
#define MS(a, x) memset(a, x, sizeof(a))
#define next ackjalscjaowjico
#define prev ajcsoua0wucckjsl
#define y1 alkscj9u20cjeijc
#define left lajcljascjljl
#define right aucouasocjolkjl
#define y0 u9cqu3jioajc

#define TWO(X) (1LL << (X))
#define CONTAIN(S, X) ((S)&TWO(X))

double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); }
int GI(int &x) { return scanf("%lld", &x); }

int n, a[300111];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << (fixed) << setprecision(9) << boolalpha;
  while (cin >> n) {
    n *= 3;
    FOR(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    int res = 0;
    int i = n - 1;
    REP(turn, n / 3) {
      res += a[i];
      i -= 2;
    }
    cout << res << endl;
  }
  return 0;
}