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 __gnu_pbds;

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    super_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    multi_set;
#define x1 dfds
#define y1 dsfdsfe
#define pb push_back
#define forn(i, n) for (ll i = 1; i <= n; ++i)
#define fi first
#define sc second
#define endl '\n'
#define po(x) (1ll << x)
const ll DIM = 2E3 + 7;
ll A[DIM], B[DIM], C[DIM];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll x, a, y, b;
  cin >> x >> y >> a >> b;
  ll c;
  cin >> c;
  forn(i, a) cin >> A[i];
  forn(i, b) cin >> B[i];
  forn(i, c) cin >> C[i];
  priority_queue<ll, vector<ll>, greater<ll>> R, G;
  ll rsum = 0, gsum = 0;
  forn(i, a) {
    R.push(A[i]);
    rsum += A[i];
  }
  forn(i, b) {
    G.push(B[i]);
    gsum += B[i];
  }
  while (R.size() > x) {
    rsum -= R.top();
    R.pop();
  }
  while (G.size() > y) {
    gsum -= G.top();
    G.pop();
  }
  sort(C + 1, C + 1 + c, greater<ll>());
  for (ll i = c; i >= 1; --i) {
    if (R.top() < G.top()) {
      ll nl = max(R.top(), C[i]);
      rsum -= R.top();
      R.pop();
      R.push(nl);
      rsum += nl;
    } else {
      ll nl = max(G.top(), C[i]);
      gsum -= G.top();
      G.pop();
      G.push(nl);
      gsum += nl;
    }
  }
  cout << rsum + gsum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    super_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    multi_set;
#define x1 dfds
#define y1 dsfdsfe
#define pb push_back
#define forn(i, n) for (ll i = 1; i <= n; ++i)
#define fi first
#define sc second
#define endl '\n'
#define po(x) (1ll << x)
const ll DIM = 1E5 + 7;
ll A[DIM], B[DIM], C[DIM];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll x, a, y, b;
  cin >> x >> y >> a >> b;
  ll c;
  cin >> c;
  forn(i, a) cin >> A[i];
  forn(i, b) cin >> B[i];
  forn(i, c) cin >> C[i];
  priority_queue<ll, vector<ll>, greater<ll>> R, G;
  ll rsum = 0, gsum = 0;
  forn(i, a) {
    R.push(A[i]);
    rsum += A[i];
  }
  forn(i, b) {
    G.push(B[i]);
    gsum += B[i];
  }
  while (R.size() > x) {
    rsum -= R.top();
    R.pop();
  }
  while (G.size() > y) {
    gsum -= G.top();
    G.pop();
  }
  sort(C + 1, C + 1 + c, greater<ll>());
  for (ll i = c; i >= 1; --i) {
    if (R.top() < G.top()) {
      ll nl = max(R.top(), C[i]);
      rsum -= R.top();
      R.pop();
      R.push(nl);
      rsum += nl;
    } else {
      ll nl = max(G.top(), C[i]);
      gsum -= G.top();
      G.pop();
      G.push(nl);
      gsum += nl;
    }
  }
  cout << rsum + gsum << endl;
  return 0;
}