Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<ll> r(A);
  vector<ll> g(B);
  vector<ll> m(C);
  for (ll i = 0; i < A; i++) {
    ll a;
    cin >> a;
    r.at(i) = a;
  }
  for (ll i = 0; i < B; i++) {
    ll a;
    cin >> a;
    g.at(i) = a;
  }
  for (ll i = 0; i < C; i++) {
    ll a;
    cin >> a;
    m.at(i) = a;
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(m.begin(), m.end());
  reverse(r.begin(), r.end());
  reverse(g.begin(), g.end());
  reverse(m.begin(), m.end());

  ll rsum = 0;
  ll gsum = 0;
  for (ll i = 0; i < X; i++) {
    rsum += r.at(i);
  }
  for (ll i = 0; i < Y; i++) {
    gsum += g.at(i);
  }

  ll current_r = X - 1;
  ll current_g = Y - 1;
  for (ll i = 0; i < C; i++) {
    ll dr, dg;
    dr = m.at(i) - r.at(current_r);
    dg = m.at(i) - g.at(current_g);
    if ((dr <= 0) && (dg <= 0))
      break;
    else if (dr >= dg) {
      rsum += dr;
      current_r--;
    } else if (dr <= dg) {
      gsum += dg;
      current_g--;
    }
  }
  cout << rsum + gsum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<ll> r(A);
  vector<ll> g(B);
  vector<ll> m(C);
  for (ll i = 0; i < A; i++) {
    ll a;
    cin >> a;
    r.at(i) = a;
  }
  for (ll i = 0; i < B; i++) {
    ll a;
    cin >> a;
    g.at(i) = a;
  }
  for (ll i = 0; i < C; i++) {
    ll a;
    cin >> a;
    m.at(i) = a;
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(m.begin(), m.end());
  reverse(r.begin(), r.end());
  reverse(g.begin(), g.end());
  reverse(m.begin(), m.end());

  ll rsum = 0;
  ll gsum = 0;
  for (ll i = 0; i < X; i++) {
    rsum += r.at(i);
  }
  for (ll i = 0; i < Y; i++) {
    gsum += g.at(i);
  }

  ll current_r = X - 1;
  ll current_g = Y - 1;
  for (ll i = 0; i < C; i++) {
    if ((current_r >= 0) && (current_g >= 0)) {
      ll dr, dg;
      dr = m.at(i) - r.at(current_r);
      dg = m.at(i) - g.at(current_g);
      if ((dr <= 0) && (dg <= 0))
        break;
      else if (dr >= dg) {
        rsum += dr;
        current_r--;
      } else if (dr <= dg) {
        gsum += dg;
        current_g--;
      }
    } else if (current_r >= 0) {
      ll dr;
      dr = m.at(i) - r.at(current_r);
      if (dr <= 0)
        break;
      else {
        rsum += dr;
        current_r--;
      }
    } else if (current_g >= 0) {
      ll dg;
      dg = m.at(i) - g.at(current_g);
      if (dg <= 0)
        break;
      else {
        gsum += dg;
        current_g--;
      }
    }
  }
  cout << rsum + gsum << endl;
}