Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define x_ real()
#define y_ imag()
#define cross(a, b) (conj(a) * (b)).imag()
#define dot(a, b) (conj(a) * (b)).real()
#define PI acos(-1)
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
#define fileIO                                                                 \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<ld> point;
typedef tuple<int, int, int> line;
typedef vector<point> polygon;
typedef pair<double, double> pd;
pair<int, int> dirs[] = {{1, 2},  {-1, 2},  {2, 1},  {2, -1},
                         {-2, 1}, {-2, -1}, {1, -2}, {-1, -2}};

int N = 1e3 + 7;

int main() {
  fastIO;
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> red, green, colorless;
  for (int i = 0; i < a; i++) {
    ll tmp;
    cin >> tmp;
    red.push_back(tmp);
  }

  for (int i = 0; i < b; i++) {
    ll tmp;
    cin >> tmp;
    green.push_back(tmp);
  }

  for (int i = 0; i < c; i++) {
    ll tmp;
    cin >> tmp;
    colorless.push_back(tmp);
  }

  sort(red.rbegin(), red.rend());
  sort(green.rbegin(), green.rend());
  sort(colorless.begin(), colorless.end());

  for (int i = 0; i < a - x; i++)
    red.pop_back();
  for (int i = 0; i < b - y; i++)
    red.pop_back();

  ll total = 0;
  for (int i = 0; i < x; i++)
    total += red[i];
  for (int i = 0; i < y; i++)
    total += green[i];

  while ((red.size() || green.size()) && colorless.size()) {
    if (red.size() && green.size()) {
      if (red.back() < colorless.back() && green.back() < colorless.back()) {
        if (red.back() < green.back()) {
          total -= red.back();
          total += colorless.back();
          red.pop_back();
          colorless.pop_back();
        } else {
          total -= green.back();
          total += colorless.back();
          green.pop_back();
          colorless.pop_back();
        }
      } else if (red.back() < colorless.back()) {
        total -= red.back();
        total += colorless.back();
        red.pop_back();
        colorless.pop_back();
      } else if (green.back() < colorless.back()) {
        total -= green.back();
        total += colorless.back();
        green.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    } else if (red.size()) {
      if (red.back() < colorless.back()) {
        total -= red.back();
        total += colorless.back();
        red.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    } else if (green.size()) {
      if (green.back() < colorless.back()) {
        total -= green.back();
        total += colorless.back();
        green.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    }
  }

  cout << total;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define x_ real()
#define y_ imag()
#define cross(a, b) (conj(a) * (b)).imag()
#define dot(a, b) (conj(a) * (b)).real()
#define PI acos(-1)
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
#define fileIO                                                                 \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<ld> point;
typedef tuple<int, int, int> line;
typedef vector<point> polygon;
typedef pair<double, double> pd;
pair<int, int> dirs[] = {{1, 2},  {-1, 2},  {2, 1},  {2, -1},
                         {-2, 1}, {-2, -1}, {1, -2}, {-1, -2}};

int N = 1e3 + 7;

int main() {
  fastIO;
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> red, green, colorless;
  for (int i = 0; i < a; i++) {
    ll tmp;
    cin >> tmp;
    red.push_back(tmp);
  }

  for (int i = 0; i < b; i++) {
    ll tmp;
    cin >> tmp;
    green.push_back(tmp);
  }

  for (int i = 0; i < c; i++) {
    ll tmp;
    cin >> tmp;
    colorless.push_back(tmp);
  }

  sort(red.rbegin(), red.rend());
  sort(green.rbegin(), green.rend());
  sort(colorless.begin(), colorless.end());

  for (int i = 0; i < a - x; i++)
    red.pop_back();
  for (int i = 0; i < b - y; i++)
    green.pop_back();

  ll total = 0;
  for (int i = 0; i < x; i++)
    total += red[i];
  for (int i = 0; i < y; i++)
    total += green[i];

  while ((red.size() || green.size()) && colorless.size()) {
    if (red.size() && green.size()) {
      if (red.back() < colorless.back() && green.back() < colorless.back()) {
        if (red.back() < green.back()) {
          total -= red.back();
          total += colorless.back();
          red.pop_back();
          colorless.pop_back();
        } else {
          total -= green.back();
          total += colorless.back();
          green.pop_back();
          colorless.pop_back();
        }
      } else if (red.back() < colorless.back()) {
        total -= red.back();
        total += colorless.back();
        red.pop_back();
        colorless.pop_back();
      } else if (green.back() < colorless.back()) {
        total -= green.back();
        total += colorless.back();
        green.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    } else if (red.size()) {
      if (red.back() < colorless.back()) {
        total -= red.back();
        total += colorless.back();
        red.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    } else if (green.size()) {
      if (green.back() < colorless.back()) {
        total -= green.back();
        total += colorless.back();
        green.pop_back();
        colorless.pop_back();
      } else {
        break;
      }
    }
  }

  cout << total;
}