Problem p02727 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -ggdb -fsanitize=address -std=c++14 E.cpp && ./a.out <<< "1 2 2 2 1
2 4
5 1
3"
*/

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename A> string to_string(A v) {
  int cnt = 0;
  string res;
  for (const auto &x : v) {
    if (cnt++)
      res += " ";
    res += to_string(x);
  }
  return "[" + res + "]";
}
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vi CostA(A);
  rep(i, 0, A) cin >> CostA[i];
  vi CostB(B);
  rep(i, 0, B) cin >> CostB[i];
  vi CostC(C);
  rep(i, 0, C) cin >> CostC[i];

  sort(all(CostA), [&](int a, int b) { return a > b; });
  sort(all(CostB), [&](int a, int b) { return a > b; });
  sort(all(CostC), [&](int a, int b) { return a > b; });

  vector<int> RG;
  rep(i, 0, X) RG.push_back(CostA[i]);
  rep(i, 0, Y) RG.push_back(CostB[i]);
  sort(all(RG), [&](int a, int b) { return a > b; });

  vector<ll> PreRG(sz(RG) + 1); // PreRG[i] = sum(RG[0..i))
  PreRG[0] = 0;
  for (int i = 1; i <= sz(RG); i++)
    PreRG[i] = PreRG[i - 1] + RG[i - 1];

  // debug(CostA);
  // debug(CostB);
  // debug(CostC);
  // debug(RG);
  // debug(PreRG);

  ll ans = 0;
  ll sumC = 0;
  for (int c = 0; c <= C; c++) {
    ll cand = sumC + PreRG[X + Y - c];

    // debug(c, sumC, cand);

    ans = max(ans, cand);

    if (c < C)
      sumC += CostC[c];
  }

  cout << ans << endl;
}

Fixed submission:
/*
g++ -ggdb -fsanitize=address -std=c++14 E.cpp && ./a.out <<< "1 2 2 2 1
2 4
5 1
3"
*/

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename A> string to_string(A v) {
  int cnt = 0;
  string res;
  for (const auto &x : v) {
    if (cnt++)
      res += " ";
    res += to_string(x);
  }
  return "[" + res + "]";
}
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vi CostA(A);
  rep(i, 0, A) cin >> CostA[i];
  vi CostB(B);
  rep(i, 0, B) cin >> CostB[i];
  vi CostC(C);
  rep(i, 0, C) cin >> CostC[i];

  sort(all(CostA), [&](int a, int b) { return a > b; });
  sort(all(CostB), [&](int a, int b) { return a > b; });
  sort(all(CostC), [&](int a, int b) { return a > b; });

  vector<int> RG;
  rep(i, 0, X) RG.push_back(CostA[i]);
  rep(i, 0, Y) RG.push_back(CostB[i]);
  sort(all(RG), [&](int a, int b) { return a > b; });

  vector<ll> PreRG(sz(RG) + 1); // PreRG[i] = sum(RG[0..i))
  PreRG[0] = 0;
  for (int i = 1; i <= sz(RG); i++)
    PreRG[i] = PreRG[i - 1] + RG[i - 1];

  // debug(CostA);
  // debug(CostB);
  // debug(CostC);
  // debug(RG);
  // debug(PreRG);

  ll ans = 0;
  ll sumC = 0;
  for (int c = 0; c <= C; c++) {
    int take = X + Y - c;
    if (take < 0 || take >= sz(PreRG))
      continue;

    ll cand = sumC + PreRG[take];

    // debug(c, sumC, cand);

    ans = max(ans, cand);

    if (c < C)
      sumC += CostC[c];
  }

  cout << ans << endl;
}