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;

#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 2e5 + 5;
const int mod = 1e7 + 7;
const int INF = 1e15;
const int LG = 21;

int arr[N];
int n, m, k;
vector<string> v1, v2;
int cnt[N];

int p[N];
int q[N];
int r[N];

void solve() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  for (int i = 0; i < a; i++)
    cin >> p[i + 1];
  for (int i = 0; i < b; i++)
    cin >> q[i + 1];
  for (int i = 0; i < c; i++)
    cin >> r[i + 1];
  if (x + y > a + b + c or x > a + c or y > b + c) {
    cout << 0 << endl;
    return;
  }
  sort(p + 1, p + a + 1, [](int a, int b) { return a > b; });
  sort(q + 1, q + b + 1, [](int a, int b) { return a > b; });
  sort(r + 1, r + c + 1, [](int a, int b) { return a > b; });
  vector<int> pref(c + 1, 0);
  vector<int> lol(c + 1, 0);
  for (int i = 1; i <= c; i++)
    pref[i] = r[i], pref[i] += pref[i - 1];
  for (int i = 1; i <= a; i++)
    lol[i] = p[i], lol[i] += lol[i - 1];
  priority_queue<int, vector<int>, greater<int>> pq;
  int sum = 0;
  for (int i = 0; i < b; i++)
    pq.push(q[i + 1]), sum += q[i + 1];
  for (int i = x; i < c; i++)
    pq.push(r[i + 1]), sum += r[i + 1];
  while (pq.size() > y) {
    sum -= pq.top();
    pq.pop();
  }
  int curr = 0;
  int ret = 0;
  int ptr = min(x, c);
  for (int i = max(x - c, 0LL); i <= min(x, a); i++, ptr--) {
    int reqd = x - i;
    int temp = lol[i] + pref[reqd] + sum;
    ret = max(ret, temp);
    if (pq.size() and pq.top() < r[ptr]) {
      sum -= pq.top();
      sum += r[ptr];
      pq.pop();
      pq.push(r[ptr]);
    }
  }
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  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;

#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 2e5 + 5;
const int mod = 1e7 + 7;
const int INF = 1e15;
const int LG = 21;

int arr[N];
int n, m, k;
vector<string> v1, v2;
int cnt[N];

int p[N];
int q[N];
int r[N];

void solve() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  for (int i = 0; i < a; i++)
    cin >> p[i + 1];
  for (int i = 0; i < b; i++)
    cin >> q[i + 1];
  for (int i = 0; i < c; i++)
    cin >> r[i + 1];
  if (x + y > a + b + c or x > a + c or y > b + c) {
    cout << 0 << endl;
    return;
  }
  sort(p + 1, p + a + 1, [](int a, int b) { return a > b; });
  sort(q + 1, q + b + 1, [](int a, int b) { return a > b; });
  sort(r + 1, r + c + 1, [](int a, int b) { return a > b; });
  vector<int> pref(c + 1, 0);
  vector<int> lol(a + 1, 0);
  for (int i = 1; i <= c; i++)
    pref[i] = r[i], pref[i] += pref[i - 1];
  for (int i = 1; i <= a; i++)
    lol[i] = p[i], lol[i] += lol[i - 1];
  priority_queue<int, vector<int>, greater<int>> pq;
  int sum = 0;
  for (int i = 0; i < b; i++)
    pq.push(q[i + 1]), sum += q[i + 1];
  for (int i = x; i < c; i++)
    pq.push(r[i + 1]), sum += r[i + 1];
  while (pq.size() > y) {
    sum -= pq.top();
    pq.pop();
  }
  int curr = 0;
  int ret = 0;
  int ptr = min(x, c);
  for (int i = max(x - c, 0LL); i <= min(x, a); i++, ptr--) {
    int reqd = x - i;
    int temp = lol[i] + pref[reqd] + sum;
    ret = max(ret, temp);
    if (pq.size() and pq.top() < r[ptr]) {
      sum -= pq.top();
      sum += r[ptr];
      pq.pop();
      pq.push(r[ptr]);
    }
  }
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}