Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vector<ll> reds(a), greens(b), colorless(c);
  for (int i = 0; i < a; ++i)
    cin >> reds[i];
  for (int i = 0; i < b; ++i)
    cin >> greens[i];
  for (int i = 0; i < c; ++i)
    cin >> colorless[i];

  ll ans = 0;
  sort(reds.begin(), reds.end(), greater<ll>());
  sort(greens.begin(), greens.end(), greater<ll>());
  sort(colorless.begin(), colorless.end());

  vector<ll> ar(reds.begin(), reds.begin() + x);
  vector<ll> ag(greens.begin(), greens.begin() + y);

  int r = x - 1, g = y - 1;
  --c;

  while (c >= 0 && (r >= 0 || g >= 0)) {
    int v = colorless[c];

    int aa = r >= 0 ? ar[r] : LLONG_MAX;
    int bb = g >= 0 ? ag[g] : LLONG_MAX;

    if (min(aa, bb) > v)
      break;

    if (aa < bb)
      ar[r--] = v;
    else
      ag[g--] = v;

    c--;
  }

  cout << accumulate(ar.begin(), ar.end(), 0LL) +
              accumulate(ag.begin(), ag.end(), 0LL)
       << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vector<ll> reds(a), greens(b), colorless(c);
  for (int i = 0; i < a; ++i)
    cin >> reds[i];
  for (int i = 0; i < b; ++i)
    cin >> greens[i];
  for (int i = 0; i < c; ++i)
    cin >> colorless[i];

  ll ans = 0;
  sort(reds.begin(), reds.end(), greater<ll>());
  sort(greens.begin(), greens.end(), greater<ll>());
  sort(colorless.begin(), colorless.end());

  vector<ll> ar(reds.begin(), reds.begin() + x);
  vector<ll> ag(greens.begin(), greens.begin() + y);

  int r = x - 1, g = y - 1;
  --c;

  while (c >= 0 && (r >= 0 || g >= 0)) {
    int v = colorless[c];

    ll aa = r >= 0 ? ar[r] : 1e10;
    ll bb = g >= 0 ? ag[g] : 1e10;

    if (min(aa, bb) > v)
      break;

    if (aa < bb)
      ar[r--] = v;
    else
      ag[g--] = v;

    c--;
  }

  cout << accumulate(ar.begin(), ar.end(), 0LL) +
              accumulate(ag.begin(), ag.end(), 0LL)
       << endl;

  return 0;
}