Problem p02727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define MOD 1000000007

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()

#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\n'; }

// typedef long long ll;
#define TENNINE 1000000005
#define TENFIVE 100005
// #define int long long

int x, y, a, b, c;

signed main() {
  cin >> x >> y >> a >> b >> c;
  vector<int> p(a), q(b), r(c);
  rep(i, a) { cin >> p[i]; }
  rep(i, b) { cin >> q[i]; }
  rep(i, c) { cin >> r[i]; }

  sort(rall(p));
  sort(rall(q));
  sort(rall(r));

  deque<int> a2, b2;

  rep(i, x) { a2.pb(p[i]); }

  rep(i, y) { b2.pb(q[i]); }

  int k = 0;

  while (1) {
    int ta = a2.back();
    int tb = b2.back();
    if (ta <= tb) {
      if (ta < r[k]) {
        a2.push_front(r[k]);
        a2.pop_back();
        k++;
      } else {
        break;
      }
    } else {
      if (tb < r[k]) {
        b2.push_front(r[k]);
        b2.pop_back();
        k++;
      }
    }
  }

  int ans = 0;

  for (auto temp : a2) {
    ans += temp;
  }
  for (auto temp : b2) {
    ans += temp;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define MOD 1000000007

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()

#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\n'; }

// typedef long long ll;
#define TENNINE 1000000005
#define TENFIVE 100005
// #define int long long

int x, y, a, b, c;

signed main() {
  cin >> x >> y >> a >> b >> c;
  vector<int> p(a), q(b), r(c);
  rep(i, a) { cin >> p[i]; }
  rep(i, b) { cin >> q[i]; }
  rep(i, c) { cin >> r[i]; }

  sort(rall(p));
  sort(rall(q));
  sort(rall(r));

  deque<int> a2, b2;

  rep(i, x) { a2.pb(p[i]); }

  rep(i, y) { b2.pb(q[i]); }

  int k = 0;

  while (1) {
    int ta = a2.back();
    int tb = b2.back();
    if (ta <= tb) {
      if (ta < r[k]) {
        a2.push_front(r[k]);
        a2.pop_back();
        k++;
      } else {
        break;
      }
    } else {
      if (tb < r[k]) {
        b2.push_front(r[k]);
        b2.pop_back();
        k++;
      } else {
        break;
      }
    }
  }

  int ans = 0;

  for (auto temp : a2) {
    ans += temp;
  }
  for (auto temp : b2) {
    ans += temp;
  }

  cout << ans << endl;

  return 0;
}