Language: C++

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void red_green(deque<ll>p, deque<ll> q, deque<ll>r, ll X, ll Y)
{
    if (X > 0)
    {
      X -= 1;
      deal_red(p, r, sum);
    }
    if (Y > 0)
    {
      Y -= 1;
      deal_green(q, r, sum);
    }
}
void green_red(deque<ll>p, deque<ll> q, deque<ll>r, ll X, ll Y)
{
    if (Y > 0)
    {
      Y -= 1;
      deal_green(q, r, sum);
    }

    if (X > 0)
    {
      X -= 1;
      deal_red(p, r, sum);
    }
}

void deal_red(deque<ll>& p, deque<ll>& r, ll& sum)
{
  if (r.empty())
  {
    ll p_max = p.front();
    // cout << "r is empty, " << p_max << " was added. ";
    sum += p_max;
    p.pop_front();
    return;
  }
  if (p.empty())
  {
    ll r_max = r.front();
    // cout << "r is empty, " << p_max << " was added. ";
    sum += r_max;
    r.pop_front();
    return;
  }
  ll p_max = p.front();
  ll r_max = r.front();
  if (p_max < r_max)
  {
    sum += r_max;
    r.pop_front();
  }
  else
  {
    sum += p_max;
    p.pop_front();
  }
}

void deal_green(deque<ll>& q, deque<ll>& r, ll& sum)
{
  if (r.empty())
  {
    ll q_max = q.front();
    // cout << "r is empty, " << q_max << " was  elected. ";
    sum += q_max;
    q.pop_front();
    return;
  }
  if (q.empty())
  {
    ll r_max = r.front();
    // cout << "r is empty, " << p_max << " was added. ";
    sum += r_max;
    r.pop_front();
    return;
  }
  ll q_max = q.front();
  ll r_max = r.front();
  if (q_max < r_max)
  {
    sum += r_max;
    r.pop_front();
  }
  else
  {
    sum += q_max;
    q.pop_front();
  }
}

int main()
{
  ll X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;

  deque<ll> p(A), q(B), r(C);

  for (size_t i{0}; i < A; ++i)
  {
    ll input;
    cin >> input;
    p.push_back(input);
  }
  for (size_t i{0}; i < B; ++i)
  {
    ll input;
    cin >> input;
    q.push_back(input);
  }
  for (size_t i{0}; i < C; ++i)
  {
    ll input;
    cin >> input;
    r.push_back(input);
  }

  sort(begin(p), end(p), greater<ll>());
  sort(begin(q), end(q), greater<ll>());
  sort(begin(r), end(r), greater<ll>());

  ll sum{0l};

  while (X > 0 || Y > 0)
  {
    if (p.front() > q.front())
    {
      green_red(p, q,r, X, Y);
    }
    else
    {
      red_green(p, q,r, X, Y);

    }
  }
  cout << sum << endl;

  return 0;
}


Compiler output:
a.cc: In function 'void red_green(std::deque<long long int>, std::deque<long long int>, std::deque<long long int>, ll, ll)':
a.cc:11:22: error: 'sum' was not declared in this scope
   11 |       deal_red(p, r, sum);
      |                      ^~~
a.cc:11:7: error: 'deal_red' was not declared in this scope
   11 |       deal_red(p, r, sum);
      |       ^~~~~~~~
a.cc:16:24: error: 'sum' was not declared in this scope
   16 |       deal_green(q, r, sum);
      |                        ^~~
a.cc:16:7: error: 'deal_green' was not declared in this scope; did you mean 'red_green'?
   16 |       deal_green(q, r, sum);
      |       ^~~~~~~~~~
      |       red_green
a.cc: In function 'void green_red(std::deque<long long int>, std::deque<long long int>, std::deque<long long int>, ll, ll)':
a.cc:24:24: error: 'sum' was not declared in this scope
   24 |       deal_green(q, r, sum);
      |                        ^~~
a.cc:24:7: error: 'deal_green' was not declared in this scope; did you mean 'red_green'?
   24 |       deal_green(q, r, sum);
      |       ^~~~~~~~~~
      |       red_green
a.cc:30:22: error: 'sum' was not declared in this scope
   30 |       deal_red(p, r, sum);
      |                      ^~~
a.cc:30:7: error: 'deal_red' was not declared in this scope
   30 |       deal_red(p, r, sum);
      |       ^~~~~~~~