Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vl a(A), b(B), c(C);
  rep(i, A) cin >> a[i];
  rep(i, B) cin >> b[i];
  rep(i, C) cin >> c[i];
  sort(all(a), greater<ll>()), sort(all(b), greater<ll>()),
      sort(all(c), greater<ll>());

  ll ai = X - 1, bi = Y - 1, ci = 0;
  ll ans = 0;
  rep(i, X) ans += a[i];
  rep(i, Y) ans += b[i];

  while (ci < C) {
    if (c[ci] < a[ai] and c[ci] < b[bi])
      break;
    if (a[ai] < b[bi]) {
      ans += c[ci] - a[ai];
      ai--, ci++;
    } else {
      ans += c[ci] - b[bi];
      bi--, ci++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vl a(A), b(B), c(C);
  rep(i, A) cin >> a[i];
  rep(i, B) cin >> b[i];
  rep(i, C) cin >> c[i];
  sort(all(a), greater<ll>()), sort(all(b), greater<ll>()),
      sort(all(c), greater<ll>());

  ll ai = X - 1, bi = Y - 1, ci = 0;
  ll ans = 0;
  rep(i, X) ans += a[i];
  rep(i, Y) ans += b[i];

  while (ci < C) {
    if (ai < 0 && bi < 0)
      break;
    else if (ai < 0) {
      if (c[ci] <= b[bi])
        break;
      ans += c[ci] - b[bi];
      bi--, ci++;
      continue;
    } else if (bi < 0) {
      if (c[ci] <= a[ai])
        break;
      ans += c[ci] - a[ai];
      ai--, ci++;
      continue;
    }

    if (c[ci] < a[ai] and c[ci] < b[bi])
      break;
    if (a[ai] < b[bi]) {
      ans += c[ci] - a[ai];
      ai--, ci++;
    } else {
      ans += c[ci] - b[bi];
      bi--, ci++;
    }
  }
  cout << ans << endl;
}