Problem p03767 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define end                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0
int inf = 1000000000;
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int mypow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2)
    return mypow(x, y - 1) * x;
  else
    return mypow(x, y / 2) * mypow(x, y / 2);
}
int Max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int Min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int kai(int x) {
  int cnt = 1;
  if (x == 0)
    return 1;
  cnt = kai(x - 1);
  return x * cnt;
}
int prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
void prime_factor(int n) {
  int cnt = 1;
  map<int, int> res;
  int m = n;
  for (int i = 2; i <= n; i++) {
    if (prime(i)) {
      while (m % i == 0) {
        ++res[i];
        m /= i;
      }
    }
  }
  cout << n << ":" << endl;
  for (int i = 2; i <= n; i++) {
    if (res[i]) {
      cout << i << " " << res[i] << endl;
    }
  }
  cout << endl;
}
/*Library End!*/
int n, a[100005], ans, memo;
signed main() {
  cin >> n;
  for (int i = 0; i < n * 3; i++)
    cin >> a[i];
  sort(a, a + n * 3);
  reverse(a, a + n * 3);
  for (int i = 1; memo < n; i += 2) {
    ans += a[i];
    memo++;
  }
  cout << ans << endl;
  end;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define end                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0
int inf = 1000000000;
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int mypow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2)
    return mypow(x, y - 1) * x;
  else
    return mypow(x, y / 2) * mypow(x, y / 2);
}
int Max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int Min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int kai(int x) {
  int cnt = 1;
  if (x == 0)
    return 1;
  cnt = kai(x - 1);
  return x * cnt;
}
int prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
void prime_factor(int n) {
  int cnt = 1;
  map<int, int> res;
  int m = n;
  for (int i = 2; i <= n; i++) {
    if (prime(i)) {
      while (m % i == 0) {
        ++res[i];
        m /= i;
      }
    }
  }
  cout << n << ":" << endl;
  for (int i = 2; i <= n; i++) {
    if (res[i]) {
      cout << i << " " << res[i] << endl;
    }
  }
  cout << endl;
}
/*Library End!*/
int n, a[300005], ans, memo;
signed main() {
  cin >> n;
  for (int i = 0; i < n * 3; i++)
    cin >> a[i];
  sort(a, a + n * 3);
  reverse(a, a + n * 3);
  for (int i = 1; memo < n; i += 2) {
    ans += a[i];
    memo++;
  }
  cout << ans << endl;
  end;
}