Problem p02727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int X, Y, a, b, c;
  cin >> X >> Y >> a >> b >> c;
  multiset<int> st;
  multiset<int> st2;
  rep(i, a) {
    int x;
    cin >> x;
    st.insert(x);
  }
  while (st.size() > X)
    st.erase(st.begin());
  rep(i, b) {
    int x;
    cin >> x;
    st2.insert(x);
  }
  while (st2.size() > Y)
    st2.erase(st2.begin());
  vector<int> d(c);
  rep(i, c) { cin >> d[i]; }
  sort(d.rbegin(), d.rend());
  for (auto x : d) {
    if (*st.begin() > x and *st2.begin() > x)
      break;
    if (*st.begin() < *st2.begin()) {
      st.erase(st.begin());
      st.insert(x);
    } else {
      st2.erase(st2.begin());
      st.insert(x);
    }
  }
  ll ans = 0;
  for (auto x : st)
    ans += x;
  for (auto x : st2)
    ans += x;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int X, Y, a, b, c;
  cin >> X >> Y >> a >> b >> c;
  multiset<int> st;
  multiset<int> st2;
  rep(i, a) {
    int x;
    cin >> x;
    st.insert(x);
  }
  while (st.size() > X)
    st.erase(st.begin());
  rep(i, b) {
    int x;
    cin >> x;
    st2.insert(x);
  }
  while (st2.size() > Y)
    st2.erase(st2.begin());
  vector<int> d(c);
  rep(i, c) { cin >> d[i]; }
  sort(d.rbegin(), d.rend());
  for (auto x : d) {
    if (*st.begin() > x and *st2.begin() > x)
      break;
    if (*st.begin() < *st2.begin()) {
      st.erase(st.begin());
      st.insert(x);
    } else {
      st2.erase(st2.begin());
      st2.insert(x);
    }
  }
  ll ans = 0;
  for (auto x : st)
    ans += x;
  for (auto x : st2)
    ans += x;
  cout << ans << endl;
}