Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define RSORT(x) sort((x).rbegin(), (x).rend())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
#define SP << " " <<
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<ll> p(A), ps(A + 1);
  vector<ll> q(B), qs(B + 1);
  vector<ll> r(C), rs(C + 1);
  rep(i, A) cin >> p[i];
  rep(i, B) cin >> q[i];
  rep(i, C) cin >> r[i];
  RSORT(p);
  RSORT(q);
  RSORT(r);
  rep(i, A) ps[i + 1] += ps[i] + p[i];
  rep(i, B) qs[i + 1] += qs[i] + q[i];
  rep(i, C) rs[i + 1] += rs[i] + r[i];
  ll ans = 0;
  rep(i, X + 1) {
    ll c = X - i;
    if (c > C)
      continue;
    /*bis*/ ll low = 0, high = C - c + 1; // min-1, max+1
    while (abs(high - low) > 1) {
      ll mid = (high + low) / 2;
      bool ok = false;
      if (r[mid + c - 1] > q[Y - mid])
        ok = true;
      (ok ? low : high) = mid;
    } // okdir : ngdir
    ll buf = ps[i] + qs[Y - low] + rs[c + low];
    maxi(ans, buf);
  }
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------


Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define RSORT(x) sort((x).rbegin(), (x).rend())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
#define SP << " " <<
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<ll> p(A), ps(A + 1);
  vector<ll> q(B), qs(B + 1);
  vector<ll> r(C), rs(C + 1);
  rep(i, A) cin >> p[i];
  rep(i, B) cin >> q[i];
  rep(i, C) cin >> r[i];
  RSORT(p);
  RSORT(q);
  RSORT(r);
  rep(i, A) ps[i + 1] += ps[i] + p[i];
  rep(i, B) qs[i + 1] += qs[i] + q[i];
  rep(i, C) rs[i + 1] += rs[i] + r[i];
  ll ans = 0;
  rep(i, X + 1) {
    ll c = X - i;
    if (c > C)
      continue;
    /*bis*/ ll low = 0, high = C - c + 1; // min-1, max+1
    while (abs(high - low) > 1) {
      ll mid = (high + low) / 2;
      bool ok = false;
      if (Y - mid >= 0 and r[mid + c - 1] > q[Y - mid])
        ok = true;
      (ok ? low : high) = mid;
    } // okdir : ngdir
    ll buf = ps[i] + qs[Y - low] + rs[c + low];
    maxi(ans, buf);
  }
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------