Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  vector<P> apple;
  for (int i = 0; i < a; i++) {
    cin >> p[i];
  }
  p.push_back(0);
  for (int i = 0; i < b; i++) {
    cin >> q[i];
  }
  q.push_back(0);
  for (int i = 0; i < c; i++) {
    cin >> r[i];
  }
  r.push_back(0);

  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  sort(q.begin(), q.end());
  reverse(q.begin(), q.end());
  sort(r.begin(), r.end());
  reverse(r.begin(), r.end());

  ll num = 0;
  ll pSum = 0, qSum = 0, rSum = 0;
  for (int i = 0; i < x; i++) {
    pSum += p[i];
    num++;
  }
  ll qNext = 0, rNext = 0;
  ll greenNum = 0;
  while (num < x + y) {
    if (q[qNext] < r[rNext]) {
      rSum += r[rNext++];
      greenNum++;
    } else {
      qSum += q[qNext++];
    }
    num++;
  }
  ll res = pSum + qSum + rSum;

  ll aLast = x - 1;
  while (aLast >= 0) {
    pSum -= p[aLast--];
    if (q[qNext] < r[rNext] || greenNum == 0) {
      rSum += r[rNext++];
    } else {
      greenNum--;
      qSum += q[qNext++];
    }
    res = max(res, pSum + qSum + rSum);
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  vector<P> apple;
  for (int i = 0; i < a; i++) {
    cin >> p[i];
  }
  p.push_back(0);
  for (int i = 0; i < b; i++) {
    cin >> q[i];
  }
  q.push_back(0);
  for (int i = 0; i < c; i++) {
    cin >> r[i];
  }
  r.push_back(0);

  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  sort(q.begin(), q.end());
  reverse(q.begin(), q.end());
  sort(r.begin(), r.end());
  reverse(r.begin(), r.end());

  ll num = 0;
  ll pSum = 0, qSum = 0, rSum = 0;
  for (int i = 0; i < x; i++) {
    pSum += p[i];
    num++;
  }
  ll qNext = 0, rNext = 0;
  ll greenNum = 0;
  while (num < x + y) {
    if (q[qNext] < r[rNext]) {
      rSum += r[rNext++];
      greenNum++;
    } else {
      qSum += q[qNext++];
    }
    num++;
  }
  ll res = pSum + qSum + rSum;

  ll aLast = x - 1;
  while (aLast >= 0) {
    if (greenNum == 0 && rNext == c) {
      break;
    }
    pSum -= p[aLast--];
    if (q[qNext] < r[rNext] || greenNum == 0) {
      rSum += r[rNext++];
    } else {
      greenNum--;
      qSum += q[qNext++];
    }
    res = max(res, pSum + qSum + rSum);
  }

  cout << res << endl;

  return 0;
}