Problem p02727 (C++)

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;

#define pb push_back
#define ff first
#define ss second
#define SIZE 2001
#define INF 2e9 + 9
#define BIG_INF 1e18 + 9
#define acmpIN "input.txt"
#define acmpOUT "output.txt"

// #pragma GCC optimize("Ofast")
//

int x, y, n, m, k, r[SIZE], g[SIZE], c[SIZE];

bool comp(const int &x, const int &y) { return x > y; }

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //    freopen(acmpIN, "r", stdin);
  //    freopen(acmpOUT, "w", stdout);

  cin >> x >> y >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> r[i];

  for (int i = 0; i < m; i++)
    cin >> g[i];

  for (int i = 0; i < k; i++)
    cin >> c[i];

  sort(c, c + k, comp);
  sort(r, r + n, comp);
  sort(g, g + m, comp);

  multiset<int> red, green;
  ll sumRed = 0, sumGreen = 0;

  for (int i = 0; i < x; i++) {
    red.insert(r[i]);
    sumRed += r[i];
  }
  for (int i = 0; i < y; i++) {
    green.insert(g[i]);
    sumGreen += g[i];
  }

  for (int i = 0; i < k; i++) {
    int rb = red.size() ? *red.begin() : 0,
        gb = green.size() ? *green.begin() : 0;
    //        cout << rb << ' ' << gb << endl;
    if (c[i] > rb && c[i] > gb) {
      if (rb < gb) {
        sumRed -= ll(rb);
        if (red.size())
          red.erase(red.begin());
        red.insert(c[i]);
        sumRed += ll(c[i]);
      } else {
        sumGreen -= ll(gb);
        if (green.size())
          green.erase(green.begin());
        green.insert(c[i]);
        sumGreen += ll(c[i]);
      }
    } else if (c[i] > rb) {
      sumRed -= ll(rb);
      if (red.size())
        red.erase(red.begin());
      red.insert(c[i]);
      sumRed += ll(c[i]);

    } else if (c[i] > gb) {
      sumGreen -= ll(gb);
      if (green.size())
        green.erase(green.begin());
      green.insert(c[i]);
      sumGreen += ll(c[i]);
    }
  }

  cout << sumRed + sumGreen;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;

#define pb push_back
#define ff first
#define ss second
#define SIZE 200001
#define INF 2e9 + 9
#define BIG_INF 1e18 + 9
#define acmpIN "input.txt"
#define acmpOUT "output.txt"

// #pragma GCC optimize("Ofast")
//

int x, y, n, m, k, r[SIZE], g[SIZE], c[SIZE];

bool comp(const int &x, const int &y) { return x > y; }

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //    freopen(acmpIN, "r", stdin);
  //    freopen(acmpOUT, "w", stdout);

  cin >> x >> y >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> r[i];

  for (int i = 0; i < m; i++)
    cin >> g[i];

  for (int i = 0; i < k; i++)
    cin >> c[i];

  sort(c, c + k, comp);
  sort(r, r + n, comp);
  sort(g, g + m, comp);

  multiset<int> red, green;
  ll sumRed = 0, sumGreen = 0;

  for (int i = 0; i < x; i++) {
    red.insert(r[i]);
    sumRed += r[i];
  }
  for (int i = 0; i < y; i++) {
    green.insert(g[i]);
    sumGreen += g[i];
  }

  for (int i = 0; i < k; i++) {
    int rb = red.size() ? *red.begin() : 0,
        gb = green.size() ? *green.begin() : 0;
    //        cout << rb << ' ' << gb << endl;
    if (c[i] > rb && c[i] > gb) {
      if (rb < gb) {
        sumRed -= ll(rb);
        if (red.size())
          red.erase(red.begin());
        red.insert(c[i]);
        sumRed += ll(c[i]);
      } else {
        sumGreen -= ll(gb);
        if (green.size())
          green.erase(green.begin());
        green.insert(c[i]);
        sumGreen += ll(c[i]);
      }
    } else if (c[i] > rb) {
      sumRed -= ll(rb);
      if (red.size())
        red.erase(red.begin());
      red.insert(c[i]);
      sumRed += ll(c[i]);

    } else if (c[i] > gb) {
      sumGreen -= ll(gb);
      if (green.size())
        green.erase(green.begin());
      green.insert(c[i]);
      sumGreen += ll(c[i]);
    }
  }

  cout << sumRed + sumGreen;

  return 0;
}