Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int test = 1; test <= t; test++)
#define pb push_back
#define nl cout << "\n"
#define F first
#define S second
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

const ll MOD = 1000000007;
ll mod(ll n, ll m = MOD) {
  n %= m, n += m, n %= m;
  return n;
}

const int MAXN = 1e5 + 5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class T1, class T2, class T3> void add(T1 &x, T2 y, T3 m = MOD) {
  x += y;
  if (x >= m)
    x -= m;
}

template <class T1, class T2, class T3> void sub(T1 &x, T2 y, T3 m = MOD) {
  x -= y;
  if (x < 0)
    x += m;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio();

  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  for (int i = 0; i < a; i++)
    cin >> p[i];
  for (int i = 0; i < b; i++)
    cin >> q[i];
  for (int i = 0; i < c; i++)
    cin >> r[i];

  sort(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());

  int i = 0, j = 0, k = 0, red = 0, green = 0, ambiguous = 0;
  ll ans = 0;
  while (red < x || green < y) {
    ll red_apple = ((i < a && red < x) ? p[i] : 0);
    ll green_apple = ((j < b && green < y) ? q[j] : 0);
    ll other = (k < c ? r[k] : 0);

    // cout<<red<<" "<<green<<" "<<ambiguous,nl;
    // cout<<red_apple<<" "<<green_apple<<" "<<other,nl;nl;
    if (red + green + ambiguous >= x + y)
      break;

    if (red < x && red_apple >= green_apple && red_apple >= other) {
      ans += red_apple;
      i++;
      red++;
    } else if (green < y && green_apple >= red_apple && green_apple >= other) {
      ans += green_apple;
      j++;
      green++;
    } else if (red + green + ambiguous < x + y) {
      ans += other;
      k++;
      ambiguous++;
    }
  }
  cout << ans, nl;

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int test = 1; test <= t; test++)
#define pb push_back
#define nl cout << "\n"
#define F first
#define S second
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

const ll MOD = 1000000007;
ll mod(ll n, ll m = MOD) {
  n %= m, n += m, n %= m;
  return n;
}

const int MAXN = 1e5 + 5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class T1, class T2, class T3> void add(T1 &x, T2 y, T3 m = MOD) {
  x += y;
  if (x >= m)
    x -= m;
}

template <class T1, class T2, class T3> void sub(T1 &x, T2 y, T3 m = MOD) {
  x -= y;
  if (x < 0)
    x += m;
}

int main() {
  fastio();

  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  for (int i = 0; i < a; i++)
    cin >> p[i];
  for (int i = 0; i < b; i++)
    cin >> q[i];
  for (int i = 0; i < c; i++)
    cin >> r[i];

  sort(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());

  int i = 0, j = 0, k = 0, red = 0, green = 0, ambiguous = 0;
  ll ans = 0;
  while (red < x || green < y) {
    ll red_apple = ((i < a && red < x) ? p[i] : 0);
    ll green_apple = ((j < b && green < y) ? q[j] : 0);
    ll other = (k < c ? r[k] : 0);

    // cout<<red<<" "<<green<<" "<<ambiguous,nl;
    // cout<<red_apple<<" "<<green_apple<<" "<<other,nl;nl;
    if (red + green + ambiguous >= x + y)
      break;

    if (red < x && red_apple >= green_apple && red_apple >= other) {
      ans += red_apple;
      i++;
      red++;
    } else if (green < y && green_apple >= red_apple && green_apple >= other) {
      ans += green_apple;
      j++;
      green++;
    } else if (red + green + ambiguous < x + y) {
      ans += other;
      k++;
      ambiguous++;
    }
  }
  cout << ans, nl;

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}