Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vs64 A(a), B(b), C(c);
  rep(i, a) cin >> A[i];
  rep(i, b) cin >> B[i];
  rep(i, c) cin >> C[i];

  sort(A.begin(), A.end(), greater<s64>());
  sort(B.begin(), B.end(), greater<s64>());
  sort(C.begin(), C.end(), greater<s64>());

  s64 ans = 0;
  rep(i, x) ans += A[i];
  rep(i, y) ans += B[i];

  int aidx = x - 1;
  int bidx = y - 1;
  rep(i, c) {
    if (aidx >= 0 && bidx >= 0 && C[i] > A[aidx] && C[i] > B[bidx]) {
      if (A[aidx] > B[bidx]) {
        ans += C[i] - B[bidx];
        --bidx;
      } else if (A[aidx] <= B[bidx]) {
        ans += C[i] - A[aidx];
        --aidx;
      }
    } else if (C[i] > A[aidx] && aidx >= 0) {
      ans += C[i] - A[aidx];
      --aidx;
    } else if (C[i] > B[bidx]) {
      ans += C[i] - B[bidx];
      --bidx;
    } else {
      break;
    }
  }

  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vs64 A(a), B(b), C(c);
  rep(i, a) cin >> A[i];
  rep(i, b) cin >> B[i];
  rep(i, c) cin >> C[i];

  sort(A.begin(), A.end(), greater<s64>());
  sort(B.begin(), B.end(), greater<s64>());
  sort(C.begin(), C.end(), greater<s64>());

  s64 ans = 0;
  rep(i, x) ans += A[i];
  rep(i, y) ans += B[i];

  int aidx = x - 1;
  int bidx = y - 1;
  rep(i, c) {
    if (aidx >= 0 && bidx >= 0 && C[i] > A[aidx] && C[i] > B[bidx]) {
      if (A[aidx] > B[bidx]) {
        ans += C[i] - B[bidx];
        --bidx;
      } else if (A[aidx] <= B[bidx]) {
        ans += C[i] - A[aidx];
        --aidx;
      }
    } else if (C[i] > A[aidx] && aidx >= 0) {
      ans += C[i] - A[aidx];
      --aidx;
    } else if (C[i] > B[bidx] && bidx >= 0) {
      ans += C[i] - B[bidx];
      --bidx;
    } else {
      break;
    }
  }

  cout << ans << "\n";
  return 0;
}