Problem p03141 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

bool comp(pair<ll, pair<ll, int>> a, pair<ll, pair<ll, int>> b) {
  return (a.first + a.second.first < b.first + b.second.first);
}

int main() {
  int N;
  cin >> N;
  vector<pair<ll, int>> A(N), B(N);
  vector<pair<ll, pair<ll, int>>> data;
  pair<ll, pair<ll, int>> a;
  rep(i, N) {
    cin >> A[i].first >> B[i].first;
    A[i].second = B[i].second = i;
    a.first = A[i].first;
    a.second.first = B[i].first;
    a.second.second = i;
    data.pb(a);
  }

  sort(all(data), comp);
  reverse(all(data));

  vector<bool> used(N + 1);
  ll cnt = 0, sum_a = 0, sum_b = 0;
  for (int i = 0; i < N * 2; i++) {
    if (!used[data[i].second.second]) {
      if (cnt % 2 == 0) {
        sum_a += A[data[i].second.second].first;
      } else {
        sum_b += B[data[i].second.second].first;
      }
      used[data[i].second.second] = true;
      cnt++;
    }
  }
  cout << sum_a - sum_b << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

bool comp(pair<ll, pair<ll, int>> a, pair<ll, pair<ll, int>> b) {
  return (a.first + a.second.first < b.first + b.second.first);
}

int main() {
  int N;
  cin >> N;
  vector<pair<ll, int>> A(N), B(N);
  vector<pair<ll, pair<ll, int>>> data;
  pair<ll, pair<ll, int>> a;
  rep(i, N) {
    cin >> A[i].first >> B[i].first;
    A[i].second = B[i].second = i;
    a.first = A[i].first;
    a.second.first = B[i].first;
    a.second.second = i;
    data.pb(a);
  }

  sort(all(data), comp);
  reverse(all(data));

  vector<bool> used(N + 1);
  ll cnt = 0, sum_a = 0, sum_b = 0;
  for (int i = 0; i < N; i++) {
    if (i % 2 == 0) {
      sum_a += data[i].first;
    } else {
      sum_b += data[i].second.first;
    }
  }
  cout << sum_a - sum_b << endl;
  return 0;
}