Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

struct Num {
  ll val;
  int id;
};

bool lessN(const Num &n1, const Num &n2) { return (n1.val < n2.val); }
bool greaterN(const Num &n1, const Num &n2) { return (n1.val > n2.val); }

int main() {
  int n;
  ll org[3000];
  Num a[2000], b[2000];
  ll pr[1000] = {}, pos[1000] = {};
  bool used_a[1000] = {}, used_b[2000] = {};
  cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    ll temp;
    cin >> temp;
    org[i] = temp;
    if (i < 2 * n) {
      a[i].val = temp;
      a[i].id = i;
    }
    if (i >= n) {
      b[3 * n - i - 1].id = 3 * n - i - 1;
      b[3 * n - i - 1].val = temp;
    }
  }
  sort(a, a + 2 * n, greaterN);
  sort(b, b + 2 * n, lessN);
  for (int i = 0; i < n; i++) {
    pr[n] += a[i].val;
    used_a[a[i].id] = true;
    pos[n] += b[i].val;
    used_b[b[i].id] = true;
  }
  int idx_a = n, idx_b = n;
  for (int i = 1; i <= n; i++) {
    if (used_a[2 * n - i]) {
      while (a[idx_a].id >= 2 * n - i)
        idx_a++;
      used_a[2 * n - i] = false;
      used_a[a[idx_a].id] = true;
      pr[n - i] = pr[n - i + 1] - org[2 * n - i] + a[idx_a++].val;
    } else {
      pr[n - i] = pr[n - i + 1];
    }
    if (used_b[2 * n - i]) {
      while (b[idx_b].id >= 2 * n - i)
        idx_b++;
      used_b[2 * n - i] = false;
      used_b[b[idx_b].id] = true;
      pos[n - i] = pos[n - i + 1] - org[n + i - 1] + b[idx_b++].val;
    } else {
      pos[n - i] = pos[n - i + 1];
    }
  }
  ll ans = -1000000000000000000;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, pr[i] - pos[n - i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

struct Num {
  ll val;
  int id;
};

bool lessN(const Num &n1, const Num &n2) { return (n1.val < n2.val); }
bool greaterN(const Num &n1, const Num &n2) { return (n1.val > n2.val); }

int main() {
  int n;
  ll org[300010];
  Num a[200010], b[200010];
  ll pr[100010] = {}, pos[100010] = {};
  bool used_a[200010] = {}, used_b[200010] = {};
  cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    ll temp;
    cin >> temp;
    org[i] = temp;
    if (i < 2 * n) {
      a[i].val = temp;
      a[i].id = i;
    }
    if (i >= n) {
      b[3 * n - i - 1].id = 3 * n - i - 1;
      b[3 * n - i - 1].val = temp;
    }
  }
  sort(a, a + 2 * n, greaterN);
  sort(b, b + 2 * n, lessN);
  for (int i = 0; i < n; i++) {
    pr[n] += a[i].val;
    used_a[a[i].id] = true;
    pos[n] += b[i].val;
    used_b[b[i].id] = true;
  }
  int idx_a = n, idx_b = n;
  for (int i = 1; i <= n; i++) {
    if (used_a[2 * n - i]) {
      while (a[idx_a].id >= 2 * n - i)
        idx_a++;
      used_a[2 * n - i] = false;
      used_a[a[idx_a].id] = true;
      pr[n - i] = pr[n - i + 1] - org[2 * n - i] + a[idx_a++].val;
    } else {
      pr[n - i] = pr[n - i + 1];
    }
    if (used_b[2 * n - i]) {
      while (b[idx_b].id >= 2 * n - i)
        idx_b++;
      used_b[2 * n - i] = false;
      used_b[b[idx_b].id] = true;
      pos[n - i] = pos[n - i + 1] - org[n + i - 1] + b[idx_b++].val;
    } else {
      pos[n - i] = pos[n - i + 1];
    }
  }
  ll ans = -1000000000000000000;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, pr[i] - pos[n - i]);
  }
  cout << ans << endl;
  return 0;
}