prithivMLmods/Coder-Stat huggingface dataset:
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define dd double
#define oo 1001007000
#define ff float
#define ooo 1000000100000000000
#define iii pair<ll, ll>
#define vii vector<ll>
#define viii vector<iii>
#define sss pair<str, str>
#define str string
#define sii set<ll>
#define siii set<iii>
#define st size_t
#define r0 return 0
#define pb push_back
#define sz size
using namespace std;

const char E = '\n';
const int N = 10005;
const int NN = 1000001;
const ll md = 998244353;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll A[N] = {0};
  ll B[N] = {0};
  ll C[N] = {0};
  for (int i = 1; i <= a; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= b; i++) {
    cin >> B[i];
  }
  for (int i = 1; i <= c; i++) {
    cin >> C[i];
  }
  sort(A + 1, A + a + 1);
  sort(B + 1, B + b + 1);
  sort(C + 1, C + c + 1);

  vii all;

  for (int i = 1; i <= x; i++) {
    all.pb(A[a - i + 1]);
  }
  for (int i = 1; i <= y; i++) {
    all.pb(B[b - i + 1]);
  }
  for (int i = 1; i <= min(c, x + y); i++) {
    all.pb(C[c - i + 1]);
  }
  sort(all.rbegin(), all.rend());
  ll s = 0;
  for (int i = 0; i < x + y; i++) {
    s += all[i];
  }
  cout << s << E;

  r0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define dd double
#define oo 1001007000
#define ff float
#define ooo 1000000100000000000
#define iii pair<ll, ll>
#define vii vector<ll>
#define viii vector<iii>
#define sss pair<str, str>
#define str string
#define sii set<ll>
#define siii set<iii>
#define st size_t
#define r0 return 0
#define pb push_back
#define sz size
using namespace std;

const char E = '\n';
const int N = 300005;
const int NN = 1000001;
const ll md = 998244353;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll A[N] = {0};
  ll B[N] = {0};
  ll C[N] = {0};
  for (int i = 1; i <= a; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= b; i++) {
    cin >> B[i];
  }
  for (int i = 1; i <= c; i++) {
    cin >> C[i];
  }
  sort(A + 1, A + a + 1);
  sort(B + 1, B + b + 1);
  sort(C + 1, C + c + 1);

  vii all;

  for (int i = 1; i <= x; i++) {
    all.pb(A[a - i + 1]);
  }
  for (int i = 1; i <= y; i++) {
    all.pb(B[b - i + 1]);
  }
  for (int i = 1; i <= min(c, x + y); i++) {
    all.pb(C[c - i + 1]);
  }
  sort(all.rbegin(), all.rend());
  ll s = 0;
  for (int i = 0; i < x + y; i++) {
    s += all[i];
  }
  cout << s << E;

  r0;
}
Problem p02727 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    X, Y, A, B, C = tuple(map(int, input().split()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    R = tuple(map(int, input().split()))

    P = sorted(P, reverse=True)
    Q = sorted(Q, reverse=True)
    R = sorted(R, reverse=True)

    x = P[:X]
    y = Q[:Y]

    for r_i in R:
        if r_i < min(x[-1], y[-1]):
            break
        else:
            if x[-1] < y[-1]:
                x[-1] = r_i
                # x = sorted(x, reverse=True)
                x.sort(reverse=True)
            else:
                y[-1] = r_i
                # y = sorted(y, reverse=True)
                y.sort(reverse=True)

    print(sum(x) + sum(y))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    X, Y, A, B, C = tuple(map(int, input().split()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    R = tuple(map(int, input().split()))

    P = sorted(P, reverse=True)
    Q = sorted(Q, reverse=True)
    R = sorted(R, reverse=True)

    x = P[:X]
    y = Q[:Y]

    saikyo = sorted(x + y + R, reverse=True)
    print(sum(saikyo[: (X + Y)]))


if __name__ == "__main__":
    main()
Problem p02727 (Python)

Buggy submission (status: Runtime Error):
x, y, a, b, c = map(int, input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))
ps.sort()
ps = ps[-x:]
qs.sort()
qs = qs[-y:]
pqs = ps + qs
pqs.sort()
rs.sort(reverse=True)

answer = sum(pqs)
for i in range(c):
    diff = rs[i] - pqs[i]
    if diff < 0:
        break
    answer += diff

print(answer)


Fixed submission:
x, y, a, b, c = map(int, input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))
ps.sort()
ps = ps[-x:]
qs.sort()
qs = qs[-y:]
pqs = ps + qs
pqs.sort()
rs.sort(reverse=True)

answer = sum(pqs)
for i in range(min(x + y, c)):
    diff = rs[i] - pqs[i]
    if diff < 0:
        break
    answer += diff

print(answer)
Problem p02727 (Python)

Buggy submission (status: Runtime Error):
from collections import deque


def main():
    X, Y, A, B, C = map(int, input().split(" "))
    a_list = list(map(int, input().split(" ")))
    b_list = list(map(int, input().split(" ")))
    c_list = list(map(int, input().split(" ")))
    a_deq = deque(sorted(a_list, reverse=True)[:X])
    b_deq = deque(sorted(b_list, reverse=True)[:Y])
    c_deq = deque(sorted(c_list, reverse=True))
    result = sum(a_deq) + sum(b_deq)
    while len(c_deq) != 0:
        min_ab = 0
        if a_deq[-1] < b_deq[-1]:
            min_ab = a_deq.pop()
        else:
            min_ab = b_deq.pop()
        if min_ab >= c_deq[0]:
            break
        result += c_deq.popleft() - min_ab

    print(result)


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    X, Y, A, B, C = map(int, input().split(" "))
    a_list = list(map(int, input().split(" ")))
    b_list = list(map(int, input().split(" ")))
    c_list = list(map(int, input().split(" ")))
    a_deq = deque(sorted(a_list, reverse=True)[:X])
    b_deq = deque(sorted(b_list, reverse=True)[:Y])
    c_deq = deque(sorted(c_list, reverse=True))
    result = sum(a_deq) + sum(b_deq)
    while len(c_deq) > 0:
        try:
            if a_deq[-1] < b_deq[-1]:
                min_ab = a_deq.pop()
            else:
                min_ab = b_deq.pop()
        except IndexError:
            if len(a_deq) > 0:
                min_ab = a_deq.pop()
            elif len(b_deq) > 0:
                min_ab = b_deq.pop()
            else:
                break

        if min_ab >= c_deq[0]:
            break
        result += c_deq.popleft() - min_ab

    print(result)


if __name__ == "__main__":
    main()
Problem p02727 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

# all_apples = sorted(p + q + r, key=lambda x: -x)

p_rest = sorted(p)
q_rest = sorted(q)
r_rest = sorted(r)

ans = 0
p_count = 0
q_count = 0
r_count = 0
while (p_count + q_count + r_count) < (X + Y):
    if p_count < X and q_count < Y:
        candidates = [p_rest[-1], q_rest[-1], r_rest[-1]]
    elif p_count < X and q_count == Y:
        candidates = [p_rest[-1], 0, r_rest[-1]]
    elif p_count == X and q_count < Y:
        candidates = [0, q_rest[-1], r_rest[-1]]

    wanna_eat = max(candidates)
    eat_idx = candidates.index(wanna_eat)

    if eat_idx == 0:
        p_count += 1
        ans += p_rest.pop()
    elif eat_idx == 1:
        q_count += 1
        ans += q_rest.pop()
    else:
        r_count += 1
        ans += r_rest.pop()
    # print(candidates, eat_idx, wanna_eat, p_count, q_count, r_count)

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

# all_apples = sorted(p + q + r, key=lambda x: -x)

p_rest = sorted(p + [0])
q_rest = sorted(q + [0])
r_rest = sorted(r + [0])

ans = 0
p_count = 0
q_count = 0
r_count = 0
while (p_count + q_count + r_count) < (X + Y):
    if p_count < X and q_count < Y:
        candidates = [p_rest[-1], q_rest[-1], r_rest[-1]]
    elif p_count < X and q_count == Y:
        candidates = [p_rest[-1], 0, r_rest[-1]]
    elif p_count == X and q_count < Y:
        candidates = [0, q_rest[-1], r_rest[-1]]

    wanna_eat = max(candidates)
    eat_idx = candidates.index(wanna_eat)

    if eat_idx == 0:
        p_count += 1
        ans += p_rest.pop()
    elif eat_idx == 1:
        q_count += 1
        ans += q_rest.pop()
    else:
        r_count += 1
        ans += r_rest.pop()
    # print(candidates, eat_idx, wanna_eat, p_count, q_count, r_count)

print(ans)
Problem p02727 (Python)

Buggy submission (status: Time Limit Exceeded):
X, Y, A, B, C = map(int, input().split())
p, pi = sorted(list(map(int, input().split())), reverse=True)[:X] + [-1], 0
q, qi = sorted(list(map(int, input().split())), reverse=True)[:Y] + [-1], 0
r, ri = sorted(list(map(int, input().split())), reverse=True) + [-1], 0
ans, nX, nY = 0, 0, 0
while nX < X or nY < Y:
    if r[ri] >= p[pi] and r[ri] >= q[qi]:
        if nX < X and nY < Y:
            if p[pi] >= q[qi]:
                nX += 1
            else:
                nY += 1
        elif nX < X:
            nX += 1
        elif nY < Y:
            nY += 1
        ans += r[ri]
        ri += 1
    elif p[pi] >= q[qi] and p[pi] >= r[ri]:
        if nX < X:
            nX += 1
            ans += p[pi]
            pi += 1
    elif q[qi] >= r[ri] and q[qi] >= p[pi]:
        if nY < Y:
            nY += 1
            ans += q[qi]
            qi += 1

print(ans)


Fixed submission:
X, Y, A, B, C = map(int, input().split())
p = sorted(list(map(int, input().split())))[-X:]
q = sorted(list(map(int, input().split())))[-Y:]
r = list(map(int, input().split()))
print(sum(sorted(p + q + r)[-(X + Y) :]))
Problem p02727 (Python)

Buggy submission (status: Runtime Error):
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

S = P[:X] + Q[:Y]
S.sort(reverse=True)

S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)
# print(S_wa)
big = S_wa[-1]
ans = S_wa[-1]

R_wa = []
wa = 0
for r in R:
    wa += r
    R_wa.append(wa)
# print(R_wa)

for i in range(len(R_wa)):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)


Fixed submission:
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

S = P[:X] + Q[:Y]
S.sort(reverse=True)

S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)
# print(S_wa)
big = S_wa[-1]
ans = S_wa[-1]

R_wa = []
wa = 0
for r in R:
    wa += r
    R_wa.append(wa)
# print(R_wa)

kazu = min(len(R_wa), len(S_wa))

for i in range(kazu):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 10010001;

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  pair<long long, int> apple[100009];
  for (int i = 0; i < a; ++i) {
    cin >> apple[i].first;
    apple[i].second = 1;
  }
  for (int i = 0; i < b; ++i) {
    cin >> apple[a + i].first;
    apple[a + i].second = 2;
  }
  for (int i = 0; i < c; ++i) {
    cin >> apple[a + b + i].first;
    apple[a + b + i].second = 0;
  }
  sort(apple, apple + a + b + c, greater<pair<long long, int>>());
  // for(int i=0;i<a+b+c;++i) cout<<apple[i].first<<" "<<apple[i].second<<endl;

  long long ans = 0;
  bool endx = false, endy = false;
  vector<int> count(4, 0);
  for (int i = 0; i < a + b + c; ++i) {
    if (apple[i].second == 1 && endx)
      continue;
    if (apple[i].second == 2 && endy)
      continue;
    ans += apple[i].first;
    count[apple[i].second]++;
    count[3]++;
    if (count[1] == x)
      endx = true;
    if (count[2] == y)
      endy = true;
    if (count[3] == x + y)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 10010001;

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  pair<long long, int> apple[300009];
  for (int i = 0; i < a; ++i) {
    cin >> apple[i].first;
    apple[i].second = 1;
  }
  for (int i = 0; i < b; ++i) {
    cin >> apple[a + i].first;
    apple[a + i].second = 2;
  }
  for (int i = 0; i < c; ++i) {
    cin >> apple[a + b + i].first;
    apple[a + b + i].second = 0;
  }
  sort(apple, apple + a + b + c, greater<pair<long long, int>>());
  // for(int i=0;i<a+b+c;++i) cout<<apple[i].first<<" "<<apple[i].second<<endl;

  long long ans = 0;
  bool endx = false, endy = false;
  vector<int> count(4, 0);
  for (int i = 0; i < a + b + c; ++i) {
    if (apple[i].second == 1 && endx)
      continue;
    if (apple[i].second == 2 && endy)
      continue;
    ans += apple[i].first;
    count[apple[i].second]++;
    count[3]++;
    if (count[1] == x)
      endx = true;
    if (count[2] == y)
      endy = true;
    if (count[3] == x + y)
      break;
  }
  cout << ans << endl;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()), (a.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
  int x, y, a, b, c;
  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(ALL(p), greater<int>());
  sort(ALL(q), greater<int>());
  sort(ALL(r), greater<int>());

  vector<int> ans;
  rep(i, x) ans.push_back(p[i]);
  rep(i, y) ans.push_back(q[i]);
  sort(ALL(ans));
  int rcnt = 0;
  rep(i, c) {
    if (r[rcnt] >= ans[i]) {
      ans[i] = r[rcnt];
      rcnt++;
    } else
      break;
  }
  ll res = 0;
  for (auto x : ans)
    res += x;
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()), (a.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
  int x, y, a, b, c;
  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(ALL(p), greater<int>());
  sort(ALL(q), greater<int>());
  sort(ALL(r), greater<int>());

  vector<int> ans;
  rep(i, x) ans.push_back(p[i]);
  rep(i, y) ans.push_back(q[i]);
  sort(ALL(ans));
  int rcnt = 0;
  int n = min(c, (int)ans.size());
  rep(i, n) {
    if (r[rcnt] >= ans[i]) {
      ans[i] = r[rcnt];
      rcnt++;
    } else
      break;
  }
  ll res = 0;
  for (auto x : ans)
    res += x;
  cout << res << endl;

  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 20;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
struct node {
  ll val, id;
  bool operator<(const node &a) const { return val > a.val; }
} a[maxn];
int main() {
  int x, y, n, m, k;
  scanf("%d %d %d %d %d", &x, &y, &n, &m, &k);
  int tot = 0;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 1;
  for (int i = 1; i <= m; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 2;
  for (int i = 1; i <= k; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 3;
  sort(a + 1, a + 1 + tot);
  int sum = x + y;
  ll ans = 0;
  for (int i = 1; i <= tot; i++) {
    if (sum == 0)
      break;
    if (a[i].id == 3)
      ans += a[i].val, sum--;
    else if (a[i].id == 2 && y)
      y--, sum--, ans += a[i].val;
    else if (a[i].id == 1 && x)
      x--, sum--, ans += a[i].val;
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 20;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
struct node {
  ll val, id;
  bool operator<(const node &a) const { return val > a.val; }
} a[maxn];
int main() {
  int x, y, n, m, k;
  scanf("%d %d %d %d %d", &x, &y, &n, &m, &k);
  int tot = 0;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 1;
  for (int i = 1; i <= m; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 2;
  for (int i = 1; i <= k; i++)
    scanf("%lld", &a[++tot].val), a[tot].id = 3;
  sort(a + 1, a + 1 + tot);
  int sum = x + y;
  ll ans = 0;
  for (int i = 1; i <= tot; i++) {
    if (sum == 0)
      break;
    if (a[i].id == 3)
      ans += a[i].val, sum--;
    else if (a[i].id == 2 && y)
      y--, sum--, ans += a[i].val;
    else if (a[i].id == 1 && x)
      x--, sum--, ans += a[i].val;
  }
  cout << ans << "\n";
}
Problem p02727 (Python)

Buggy submission (status: Runtime Error):
X, Y, A, B, C = map(int, input().split())
P = sorted(map(int, input().split()))
Q = sorted(map(int, input().split()))
R = sorted(map(int, input().split()))

ans = 0
for _ in range(X):
    if P[-1] > R[-1]:
        ans += P.pop()
    else:
        ans += R.pop()

for _ in range(Y):
    if Q[-1] > R[-1]:
        ans += Q.pop()
    else:
        ans += R.pop()

print(ans)


Fixed submission:
X, Y, A, B, C = map(int, input().split())
P = sorted(map(int, input().split()))
Q = sorted(map(int, input().split()))
R = sorted(map(int, input().split()))

cand = sorted(P[-X:] + Q[-Y:] + R)
print(sum(cand[-X - Y :]))
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC diagnostic error "-std=c++11"
#include <bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define gcd(a, b) __gcd(a, b)
#define MEM(a, x) memset(a, x, sizeof(a))
#define VI vector<int>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 5;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
int x, y, a, b, c;
ll p[N], q[N], r[N];
bool cmp(int a, int b) { return a > b; }
vector<ll> v;
int main() {
  cin >> x >> y >> a >> b >> c;
  rep(i, 1, a) read(p[i]);
  rep(i, 1, b) read(q[i]);
  rep(i, 1, c) read(r[i]);
  sort(p + 1, p + 1 + a, cmp);
  sort(q + 1, q + 1 + b, cmp);
  sort(r + 1, r + 1 + c, cmp);
  ll ans = 0;
  rep(i, 1, x) v.pb(p[i]);
  rep(i, 1, y) v.pb(q[i]);
  sort(v.begin(), v.end());
  int now = 0;
  rep(i, 1, c) {
    if (r[i] > v[now])
      v[now] = r[i];
    now++;
  }
  rep(i, 0, v.size() - 1) ans += v[i];
  cout << ans << '\n';
  return 0;
}


Fixed submission:
// #pragma GCC diagnostic error "-std=c++11"
#include <bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define gcd(a, b) __gcd(a, b)
#define MEM(a, x) memset(a, x, sizeof(a))
#define VI vector<int>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 5;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
int x, y, a, b, c;
ll p[N], q[N], r[N];
bool cmp(int a, int b) { return a > b; }
vector<ll> v;
int main() {
  cin >> x >> y >> a >> b >> c;
  rep(i, 1, a) read(p[i]);
  rep(i, 1, b) read(q[i]);
  rep(i, 1, c) read(r[i]);
  sort(p + 1, p + 1 + a, cmp);
  sort(q + 1, q + 1 + b, cmp);
  sort(r + 1, r + 1 + c, cmp);
  ll ans = 0;
  rep(i, 1, x) v.pb(p[i]);
  rep(i, 1, y) v.pb(q[i]);
  sort(v.begin(), v.end());
  int now = 0;
  rep(i, 1, c) {
    if (now < v.size() && r[i] > v[now])
      v[now] = r[i];
    now++;
  }
  rep(i, 0, v.size() - 1) ans += v[i];
  cout << ans << '\n';
  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll x, y, a, b, c, ans = 0;
  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.begin(), p.end(), greater<ll>());
  sort(q.begin(), q.end(), greater<ll>());
  sort(r.begin(), r.end());
  while (p.size() != x)
    p.pop_back();
  while (q.size() != y)
    q.pop_back();
  for (int i = 0; i < p.size(); i++)
    ans += p[i];
  for (int i = 0; i < q.size(); i++)
    ans += q[i];
  while (1) {
    int pcom = r.back() - p.back();
    int qcom = r.back() - q.back();
    if (pcom <= 0 && qcom <= 0)
      break;
    if (pcom > qcom) {
      ans -= p.back();
      p.pop_back();
    } else {
      ans -= q.back();
      q.pop_back();
    }
    ans += r.back();
    r.pop_back();
  }
  cout << ans << '\n';
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll x, y, a, b, c, ans = 0;
  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.begin(), p.end(), greater<ll>());
  sort(q.begin(), q.end(), greater<ll>());
  sort(r.begin(), r.end());
  while (p.size() != x)
    p.pop_back();
  while (q.size() != y)
    q.pop_back();
  for (int i = 0; i < p.size(); i++)
    ans += p[i];
  for (int i = 0; i < q.size(); i++)
    ans += q[i];
  while (1) {
    if (r.empty())
      break;
    int pcom = (p.size() ? r.back() - p.back() : -1);
    int qcom = (q.size() ? r.back() - q.back() : -1);
    if (pcom <= 0 && qcom <= 0)
      break;
    if (pcom > qcom) {
      ans -= p.back();
      p.pop_back();
    } else {
      ans -= q.back();
      q.pop_back();
    }
    ans += r.back();
    r.pop_back();
  }
  cout << ans << '\n';
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (int i = f; i < t; i++)
#define pr(i, f, t) for (int i = t - 1; i >= f; i--)
#define ca(n, a)                                                               \
  rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\n" : " ")
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll p[a], q[b], r[c];
  rp(i, 0, a) cin >> p[i];
  rp(i, 0, b) cin >> q[i];
  rp(i, 0, c) cin >> r[i];
  sort(p, p + a);
  sort(q, q + b);
  sort(r, r + c);
  reverse(p, p + a);
  reverse(q, q + b);
  reverse(r, r + c);
  vl ans;
  rp(i, 0, x) ans.push_back(p[i]);
  rp(i, 0, y) ans.push_back(q[i]);
  sort(be(ans));
  // reverse(be(ans));
  ll co = 0;
  rp(i, 0, c) {
    if (r[i] > ans[i])
      co++;
  }
  ll aa = 0;
  rp(i, 0, co) { aa += r[i]; }
  rp(i, co, x + y) { aa += ans[i]; }
  cout << aa << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (int i = f; i < t; i++)
#define pr(i, f, t) for (int i = t - 1; i >= f; i--)
#define ca(n, a)                                                               \
  rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\n" : " ")
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  ll p[a], q[b], r[c];
  rp(i, 0, a) cin >> p[i];
  rp(i, 0, b) cin >> q[i];
  rp(i, 0, c) cin >> r[i];
  sort(p, p + a);
  sort(q, q + b);
  sort(r, r + c);
  reverse(p, p + a);
  reverse(q, q + b);
  reverse(r, r + c);
  vl ans;
  rp(i, 0, x) ans.push_back(p[i]);
  rp(i, 0, y) ans.push_back(q[i]);
  sort(be(ans));
  // reverse(be(ans));
  ll co = 0;
  rp(i, 0, c) {
    if (r[i] > ans[i])
      co++;
    else
      break;
  }
  ll aa = 0;
  rp(i, 0, co) { aa += r[i]; }
  rp(i, co, x + y) { aa += ans[i]; }
  cout << aa << endl;
}
Problem p02727 (C++)

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

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  auto p = vector<int>(a);
  for (auto &p_i : p)
    cin >> p_i;
  auto q = vector<int>(b);
  for (auto &q_i : q)
    cin >> q_i;
  auto r = vector<int>(c);
  for (auto &r_i : r)
    cin >> r_i;

  sort(p.begin(), p.end());
  if (p.size() > x) {
    auto discard = p.size() - x;
    p.erase(p.begin(), p.begin() + discard);
  }

  sort(q.begin(), q.end());
  if (q.size() > y) {
    auto discard = q.size() - y;
    q.erase(q.begin(), q.begin() + discard);
  }

  q.insert(q.end(), p.begin(), p.end());
  sort(q.begin(), q.end());

  sort(r.begin(), r.end());
  if (r.size() > x + y) {
    auto discard = r.size() - (x + y);
    r.erase(r.begin(), q.begin() + discard);
  }

  for (int i = 0; i < q.size(); ++i) {
    if (r.empty())
      break;
    if (q[i] >= r.back())
      break;
    q[i] = r.back();
    r.pop_back();
  }

  cout << accumulate(q.begin(), q.end(), 0LL);
  return 0;
}


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

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  auto p = vector<int>(a);
  for (auto &p_i : p)
    cin >> p_i;
  auto q = vector<int>(b);
  for (auto &q_i : q)
    cin >> q_i;
  auto r = vector<int>(c);
  for (auto &r_i : r)
    cin >> r_i;

  sort(p.begin(), p.end());
  if (p.size() > x) {
    auto discard = p.size() - x;
    p.erase(p.begin(), p.begin() + discard);
  }

  sort(q.begin(), q.end());
  if (q.size() > y) {
    auto discard = q.size() - y;
    q.erase(q.begin(), q.begin() + discard);
  }

  q.insert(q.end(), p.begin(), p.end());
  sort(q.begin(), q.end());

  sort(r.begin(), r.end());
  if (r.size() > x + y) {
    auto discard = r.size() - (x + y);
    r.erase(r.begin(), r.begin() + discard);
  }

  for (int i = 0; i < q.size(); ++i) {
    if (r.empty())
      break;
    if (q[i] >= r.back())
      break;
    q[i] = r.back();
    r.pop_back();
  }

  cout << accumulate(q.begin(), q.end(), 0LL);
  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vector<int> p(a), q(b), r(c);
  for (auto &i : p) {
    cin >> i;
  }
  for (auto &i : q) {
    cin >> i;
  }
  for (auto &i : r) {
    cin >> i;
  }

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

  vector<int> m;
  copy(p.begin(), p.begin() + x, back_inserter(m));
  copy(q.begin(), q.begin() + y, back_inserter(m));
  sort(m.begin(), m.end());

  for (auto i = 0; i != c; ++i) {
    m[i] = max(r[i], m[i]);
  }

  cout << accumulate(m.begin(), m.end(), 0ll) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;

  vector<int> p(a), q(b), r(c);
  for (auto &i : p) {
    cin >> i;
  }
  for (auto &i : q) {
    cin >> i;
  }
  for (auto &i : r) {
    cin >> i;
  }

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

  vector<int> m;
  copy(p.begin(), p.begin() + x, back_inserter(m));
  copy(q.begin(), q.begin() + y, back_inserter(m));
  sort(m.begin(), m.end());

  auto n = min(c, (int)m.size());
  for (auto i = 0; i != n; ++i) {
    m[i] = max(r[i], m[i]);
  }

  cout << accumulate(m.begin(), m.end(), 0ll) << endl;

  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define N 112345
ll p[N] = {}, q[N] = {}, r[N] = {}, s[2 * N];

int main() {
  int x, y, a, b, c;
  scanf("%d%d%d%d%d", &x, &y, &a, &b, &c);
  REP(i, a) scanf("%lld", p + i + 1);
  REP(i, b) scanf("%lld", q + i + 1);
  REP(i, c) scanf("%lld", r + i + 1);

  sort(p + 1, p + a + 1, greater<ll>());
  sort(q + 1, q + b + 1, greater<ll>());
  sort(r + 1, r + c + 1, greater<ll>());

  REP(i, y) p[1 + x + i] = q[1 + i];
  sort(p + 1, p + x + y + 1, greater<ll>());

  REP(i, x + y) DEBUG("%lld ", p[i + 1]);
  DEBUG("\n");
  REP(i, c) DEBUG("%lld ", r[i + 1]);
  DEBUG("\n");

  REP(i, x + y) p[i + 1] += p[i];
  REP(i, c) r[i + 1] += r[i];

  ll ans = 0;
  REP(i, min(x + y, c) + 1) { LAR(ans, p[x + y - i] + r[i]); }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define N 112345
ll p[2 * N] = {}, q[N] = {}, r[N] = {};

int main() {
  int x, y, a, b, c;
  scanf("%d%d%d%d%d", &x, &y, &a, &b, &c);
  REP(i, a) scanf("%lld", p + i + 1);
  REP(i, b) scanf("%lld", q + i + 1);
  REP(i, c) scanf("%lld", r + i + 1);

  sort(p + 1, p + a + 1, greater<ll>());
  sort(q + 1, q + b + 1, greater<ll>());
  sort(r + 1, r + c + 1, greater<ll>());

  REP(i, y) p[1 + x + i] = q[1 + i];
  sort(p + 1, p + x + y + 1, greater<ll>());

  REP(i, x + y) DEBUG("%lld ", p[i + 1]);
  DEBUG("\n");
  REP(i, c) DEBUG("%lld ", r[i + 1]);
  DEBUG("\n");

  REP(i, x + y) p[i + 1] += p[i];
  REP(i, c) r[i + 1] += r[i];

  ll ans = 0;
  REP(i, min(x + y, c) + 1) { LAR(ans, p[x + y - i] + r[i]); }
  printf("%lld\n", ans);
}
Problem p02727 (C++)

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

#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

const int INF = 1e9;
const int MAXN = 2e3 + 5;
const int MOD = 1e9 + 7;

ll n, m, k, x, y;
ll A[MAXN], B[MAXN], C[MAXN], sum;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> x >> y >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  for (int i = 1; i <= m; i++)
    cin >> B[i];
  for (int i = 1; i <= k; i++)
    cin >> C[i];
  sort(A + 1, A + 1 + n, greater<ll>());
  sort(B + 1, B + 1 + m, greater<ll>());
  sort(C + 1, C + 1 + k, greater<ll>());

  for (int i = 1; i <= x; i++)
    sum += A[i];
  for (int i = 1; i <= y; i++)
    sum += B[i];

  for (int i = 1; i <= k; i++) {
    if (x && y) {
      if (A[x] < B[y]) {
        if (A[x] < C[i])
          sum -= A[x--], sum += C[i];
      } else {
        if (B[y] < C[i])
          sum -= B[y--], sum += C[i];
      }
    } else if (x) {
      if (A[x] < C[i])
        sum -= A[x--], sum += C[i];
    } else if (y) {
      if (B[y] < C[i])
        sum -= B[y--], sum += C[i];
    }
  }
  cout << sum;
}

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

#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

const int INF = 1e9;
const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;

ll n, m, k, x, y;
ll A[MAXN], B[MAXN], C[MAXN], sum;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> x >> y >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  for (int i = 1; i <= m; i++)
    cin >> B[i];
  for (int i = 1; i <= k; i++)
    cin >> C[i];
  sort(A + 1, A + 1 + n, greater<ll>());
  sort(B + 1, B + 1 + m, greater<ll>());
  sort(C + 1, C + 1 + k, greater<ll>());

  for (int i = 1; i <= x; i++)
    sum += A[i];
  for (int i = 1; i <= y; i++)
    sum += B[i];

  for (int i = 1; i <= k; i++) {
    if (x && y) {
      if (A[x] < B[y]) {
        if (A[x] < C[i])
          sum -= A[x--], sum += C[i];
      } else {
        if (B[y] < C[i])
          sum -= B[y--], sum += C[i];
      }
    } else if (x) {
      if (A[x] < C[i])
        sum -= A[x--], sum += C[i];
    } else if (y) {
      if (B[y] < C[i])
        sum -= B[y--], sum += C[i];
    }
  }
  cout << sum;
}
Problem p02727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100000 + 10;
int x, y, a, b, c;
int p[N], q[N], r[N];
int main() {
  scanf("%d%d%d%d%d", &x, &y, &a, &b, &c);
  for (int i = 1; i <= a; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= b; i++)
    scanf("%d", &q[i]);
  for (int i = 1; i <= c; i++)
    scanf("%d", &r[i]);
  multiset<LL> s1, s2, s3;
  sort(p + 1, p + 1 + a);
  LL ans = 0;
  for (int i = a; i > a - x; i--) {
    s1.insert(p[i]);
    ans += p[i];
  }
  sort(q + 1, q + 1 + b);
  for (int i = b; i > b - y; i--) {
    s2.insert(q[i]);
    ans += q[i];
  }

  for (int i = 1; i <= c; i++)
    s3.insert(r[i]);
  // printf("%lld\n", ans);
  while (s3.size() && (s1.size() > 0 || s2.size() > 0)) {
    int v1 = s1.size() ? (*s1.begin()) : 0;
    int v2 = s2.size() ? (*s2.begin()) : 0;
    int v3 = *s3.rbegin();
    if (v3 < min(v1, v2))
      break;
    // printf("# %d %d\n", v3,min(v1,v2));
    if (v1 < v2) {
      s1.erase(s1.begin());
      ans -= v1;
    } else {
      s2.erase(s2.begin());
      ans -= v2;
    }
    s3.erase(s3.find(v3));
    ans += v3;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100000 + 10;
int x, y, a, b, c;
int p[N], q[N], r[N];
int main() {
  scanf("%d%d%d%d%d", &x, &y, &a, &b, &c);
  for (int i = 1; i <= a; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= b; i++)
    scanf("%d", &q[i]);
  for (int i = 1; i <= c; i++)
    scanf("%d", &r[i]);
  multiset<LL> s1, s2, s3;
  sort(p + 1, p + 1 + a);
  LL ans = 0;
  for (int i = a; i > a - x; i--) {
    s1.insert(p[i]);
    ans += p[i];
  }
  sort(q + 1, q + 1 + b);
  for (int i = b; i > b - y; i--) {
    s2.insert(q[i]);
    ans += q[i];
  }

  for (int i = 1; i <= c; i++)
    s3.insert(r[i]);
  // printf("%lld\n", ans);
  while ((s3.size() > 0) && (s1.size() > 0 || s2.size() > 0)) {
    int v1 = s1.size() ? (*s1.begin()) : 1e9 + 1;
    int v2 = s2.size() ? (*s2.begin()) : 1e9 + 1;
    int v3 = *s3.rbegin();
    if (v3 < min(v1, v2))
      break;
    // printf("# %d %d\n", v3,min(v1,v2));
    if (v1 < v2) {
      s1.erase(s1.begin());
      ans -= v1;
    } else {
      s2.erase(s2.begin());
      ans -= v2;
    }
    s3.erase(s3.find(v3));
    ans += v3;
  }
  cout << ans << endl;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e3 + 5;
int a1[N], a2[N], a3[N];
priority_queue<int, vector<int>, greater<int>> q;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  for (int i = 0; i < a; i++)
    cin >> a1[i];
  for (int i = 0; i < b; i++)
    cin >> a2[i];
  for (int i = 0; i < c; i++)
    cin >> a3[i];
  sort(a1, a1 + a);
  sort(a2, a2 + b);
  sort(a3, a3 + c);
  ll sum = 0;
  if (a >= x) {
    for (int i = a - 1; i >= a - x; i--) {
      q.push(a1[i]);
    }
    x = 0;
  } else {
    for (int i = 0; i < a; i++) {
      q.push(a1[i]);
    }
    x -= a;
  }
  if (b >= y) {
    for (int i = b - 1; i >= b - y; i--) {
      q.push(a2[i]);
    }
    y = 0;
  } else {
    for (int i = 0; i < b; i++) {
      q.push(a2[i]);
    }
    y -= b;
  }
  int idx = c - 1;
  int minn = 0x3f3f3f3f;

  while (x > 0) {
    q.push(a3[idx--]);
    x--;
  }
  while (y > 0) {
    q.push(a3[idx--]);
    y--;
  }
  for (; idx >= 0; idx--) {
    int t = q.top();
    if (t < a3[idx]) {
      q.pop();
      q.push(a3[idx]);
    } else
      break;
  }
  while (!q.empty()) {
    sum += q.top();
    q.pop();
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int a1[N], a2[N], a3[N];
priority_queue<int, vector<int>, greater<int>> q;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  for (int i = 0; i < a; i++)
    cin >> a1[i];
  for (int i = 0; i < b; i++)
    cin >> a2[i];
  for (int i = 0; i < c; i++)
    cin >> a3[i];
  sort(a1, a1 + a);
  sort(a2, a2 + b);
  sort(a3, a3 + c);
  ll sum = 0;
  if (a >= x) {
    for (int i = a - 1; i >= a - x; i--) {
      q.push(a1[i]);
    }
    x = 0;
  } else {
    for (int i = 0; i < a; i++) {
      q.push(a1[i]);
    }
    x -= a;
  }
  if (b >= y) {
    for (int i = b - 1; i >= b - y; i--) {
      q.push(a2[i]);
    }
    y = 0;
  } else {
    for (int i = 0; i < b; i++) {
      q.push(a2[i]);
    }
    y -= b;
  }
  int idx = c - 1;
  int minn = 0x3f3f3f3f;

  while (x > 0) {
    q.push(a3[idx--]);
    x--;
  }
  while (y > 0) {
    q.push(a3[idx--]);
    y--;
  }
  for (; idx >= 0; idx--) {
    int t = q.top();
    if (t < a3[idx]) {
      q.pop();
      q.push(a3[idx]);
    } else
      break;
  }
  while (!q.empty()) {
    sum += q.top();
    q.pop();
  }
  cout << sum << endl;
  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  rep(i, a) cin >> p.at(i);
  rep(i, b) cin >> q.at(i);
  rep(i, c) cin >> r.at(i);
  sort(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());

  vector<ll> applelist(0);
  rep(i, x) { applelist.push_back(p.at(i)); }
  rep(i, y) { applelist.push_back(q.at(i)); }
  sort(applelist.begin(), applelist.end());

  for (int i = 0; i < c; i++) {
    if (r.at(i) > applelist.at(i)) {
      applelist.at(i) = r.at(i);
    }
  }

  ll ans = 0;
  rep(i, x + y) { ans += applelist.at(i); }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c);
  rep(i, a) cin >> p.at(i);
  rep(i, b) cin >> q.at(i);
  rep(i, c) cin >> r.at(i);
  sort(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());

  vector<ll> applelist(0);
  rep(i, x) { applelist.push_back(p.at(i)); }
  rep(i, y) { applelist.push_back(q.at(i)); }
  sort(applelist.begin(), applelist.end());

  for (int i = 0; i < c; i++) {
    if (x + y <= i)
      break;
    if (r.at(i) > applelist.at(i)) {
      applelist.at(i) = r.at(i);
    }
  }

  ll ans = 0;
  rep(i, x + y) { ans += applelist.at(i); }
  cout << ans << endl;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, n) for (ll i = 1; i <= (n); i++)
typedef long long ll;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> 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(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());
  ll ans = 0;
  rep(i, x) ans += p[i];
  rep(i, y) ans += q[i];
  ll z = 0;
  while (1) {
    if (p[x - 1] >= q[y - 1]) {
      if (q[y - 1] < r[z]) {
        ans -= q[y - 1];
        ans += r[z];
        y--;
        z++;
      } else {
        break;
      }
    } else {
      if (p[x - 1] < r[z]) {
        ans -= p[x - 1];
        ans += r[z];
        x--;
        z++;
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, n) for (ll i = 1; i <= (n); i++)
typedef long long ll;

int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> 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(p.rbegin(), p.rend());
  sort(q.rbegin(), q.rend());
  sort(r.rbegin(), r.rend());
  ll ans = 0;
  rep(i, x) ans += p[i];
  rep(i, y) ans += q[i];
  ll z = 0;
  while (1) {
    if (x == 0 && y == 0) {
      break;
    } else if (x == 0) {
      if (q[y - 1] < r[z]) {
        ans -= q[y - 1];
        ans += r[z];
        y--;
        z++;
        continue;
      } else {
        break;
      }
    } else if (y == 0) {
      if (p[x - 1] < r[z]) {
        ans -= p[x - 1];
        ans += r[z];
        x--;
        z++;
        continue;
      } else {
        break;
      }
    }
    if (z == c)
      break;
    if (p[x - 1] >= q[y - 1]) {
      if (q[y - 1] < r[z]) {
        ans -= q[y - 1];
        ans += r[z];
        y--;
        z++;
      } else {
        break;
      }
    } else {
      if (p[x - 1] < r[z]) {
        ans -= p[x - 1];
        ans += r[z];
        x--;
        z++;
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02727 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
int64_t X, Y, A, B, C, ans = 0LL;
int main(void) {
  cin >> X >> Y >> A >> B >> C;
  int 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, p + A, std::greater<int64_t>());
  sort(q, q + B, std::greater<int64_t>());
  sort(r, r + C, std::greater<int64_t>());
  stack<int64_t> red, blue;
  for (int i = 0; i < X; i++)
    ans += p[i], red.push(p[i]);
  for (int i = 0; i < Y; i++)
    ans += q[i], blue.push(q[i]);
  for (int i = 0; i < C; i++) {
    if (min(red.top(), blue.top()) < r[i]) {
      if (red.top() < blue.top())
        ans += r[i] - red.top(), red.pop();
      else
        ans += r[i] - blue.top(), blue.pop();
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
int64_t X, Y, A, B, C, ans = 0LL;
int main(void) {
  cin >> X >> Y >> A >> B >> C;
  int 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, p + A, std::greater<int64_t>());
  sort(q, q + B, std::greater<int64_t>());
  sort(r, r + C, std::greater<int64_t>());
  stack<int64_t> red, blue;
  for (int i = 0; i < X; i++)
    ans += p[i], red.push(p[i]);
  for (int i = 0; i < Y; i++)
    ans += q[i], blue.push(q[i]);
  for (int i = 0; i < C; i++) {
    if (min((red.empty() ? 1000000001 : red.top()),
            (blue.empty() ? 1000000001 : blue.top())) < r[i]) {
      if ((red.empty() ? 1000000001 : red.top()) <
          (blue.empty() ? 1000000001 : blue.top()))
        ans += r[i] - red.top(), red.pop();
      else
        ans += r[i] - blue.top(), blue.pop();
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02727 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//  ProgrammingContestChallengeBook2
//
//  Created by Hisaki Kobayashi on 2020/04/04.
//  Copyright © 2020 Hisaki Kobayashi. All rights reserved.
//

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
// #include "mylibrary.hpp"

using namespace std;

int main() {
  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<int> p(A);
  for (int i = 0; i < A; i++)
    cin >> p[i];
  vector<int> q(B);
  for (int i = 0; i < B; i++)
    cin >> q[i];
  vector<int> r(C);
  for (int i = 0; i < C; i++)
    cin >> r[i];
  for (int i = 0; i < A; i++)
    sort(p.rbegin(), p.rend());
  for (int i = 0; i < B; i++)
    sort(q.rbegin(), q.rend());

  vector<int> d;
  for (int i = 0; i < X; i++)
    d.push_back(p[i]);
  for (int i = 0; i < Y; i++)
    d.push_back(q[i]);
  for (int i = 0; i < C; i++)
    d.push_back(r[i]);

  sort(d.rbegin(), d.rend());

  long long sum = 0;
  for (int i = 0; i < X + Y; i++) {
    sum += d[i];
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
//
//  main.cpp
//  ProgrammingContestChallengeBook2
//
//  Created by Hisaki Kobayashi on 2020/04/04.
//  Copyright © 2020 Hisaki Kobayashi. All rights reserved.
//

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
// #include "mylibrary.hpp"

using namespace std;

int main() {
  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<int> p(A);
  for (int i = 0; i < A; i++)
    cin >> p[i];
  vector<int> q(B);
  for (int i = 0; i < B; i++)
    cin >> q[i];
  vector<int> r(C);
  for (int i = 0; i < C; i++)
    cin >> r[i];

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

  vector<int> d;
  for (int i = 0; i < X; i++)
    d.push_back(p[i]);
  for (int i = 0; i < Y; i++)
    d.push_back(q[i]);
  for (int i = 0; i < C; i++)
    d.push_back(r[i]);

  sort(d.rbegin(), d.rend());

  long long sum = 0;
  for (int i = 0; i < X + Y; i++) {
    sum += d[i];
  }
  cout << sum << endl;
  return 0;
}