Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#define ll long long
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
ll pi = acos(-1);
ll z = 1000000007;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i) + 1); }
ll right(ll i) { return (2 * (i + 1)); }
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;

void solve() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll p[a], q[b], r[c];
  fo(i, a) { cin >> p[i]; }
  fo(i, b) { cin >> q[i]; }
  fo(i, c) { cin >> r[i]; }
  sort(p, p + a);
  sort(q, q + b);
  sort(r, r + c);
  reverse(p, p + a);
  reverse(q, q + b);
  reverse(r, r + c);

  /*fo(i , a)
  {
          cout << p[i] << ' ';
  }
  cout << endl ;*/

  multiset<ll> red, green;
  fo(i, x) { red.insert(p[i]); }
  fo(i, y) { green.insert(q[i]); }
  fo(i, c) {
    ll curr = r[i];
    ll curr_r = (*(red.begin()));
    ll curr_g = (*(green.begin()));

    if (curr_r < curr_g) {
      if (curr_r < curr) {
        red.erase(red.begin());
        red.insert(curr);
      }

    } else {
      if (curr_g < curr) {
        green.erase(green.begin());
        green.insert(curr);
      }
    }
  }
  ll ans = 0;
  auto itr = red.begin();
  for (; itr != red.end(); itr++) {
    ans += (*itr);
  }
  for (auto itrg = green.begin(); itrg != green.end(); itrg++) {
    ans += (*itrg);
  }
  cout << ans << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("inputf.txt", "r", stdin);
  freopen("outputf.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  ll t;
  // cin >> t ;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#define ll long long
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
ll pi = acos(-1);
ll z = 1000000007;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i) + 1); }
ll right(ll i) { return (2 * (i + 1)); }
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;

void solve() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll p[a], q[b], r[c];
  fo(i, a) { cin >> p[i]; }
  fo(i, b) { cin >> q[i]; }
  fo(i, c) { cin >> r[i]; }
  sort(p, p + a);
  sort(q, q + b);
  sort(r, r + c);
  reverse(p, p + a);
  reverse(q, q + b);
  reverse(r, r + c);

  /*fo(i , a)
  {
          cout << p[i] << ' ';
  }
  cout << endl ;*/

  multiset<ll> red, green;
  fo(i, x) { red.insert(p[i]); }
  fo(i, y) { green.insert(q[i]); }
  fo(i, c) {
    ll curr = r[i];
    ll curr_r = (*(red.begin()));
    ll curr_g = (*(green.begin()));

    if (curr_r < curr_g) {
      if (curr_r < curr) {
        red.erase(red.begin());
        red.insert(curr);
      }

    } else {
      if (curr_g < curr) {
        green.erase(green.begin());
        green.insert(curr);
      }
    }
  }
  ll ans = 0;
  auto itr = red.begin();
  for (; itr != red.end(); itr++) {
    ans += (*itr);
  }
  for (auto itrg = green.begin(); itrg != green.end(); itrg++) {
    ans += (*itrg);
  }
  cout << ans << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
  freopen("inputf.txt" , "r" , stdin) ;
  freopen("outputf.txt" , "w" , stdout) ;
  freopen("error.txt" , "w" , stderr) ;
  #endif*/

  ll t;
  // cin >> t ;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}