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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)

typedef long long i64;
typedef pair<i64, i64> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int x, y, z, k;
i64 a[1010], b[1010], c[1010];
i64 d[1010 * 1010], e[1010 * 1010];

int main() {
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < y; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      d[i * y + j] = a[i] + b[j];
    }
  }
  sort(d, d + x * y, greater<i64>());

  for (int i = 0; i < min(k, x * y); i++) {
    for (int j = 0; j < z; j++) {
      e[i * z + j] = d[i] + c[j];
    }
  }

  sort(e, e + min(k, x * y) * z, greater<i64>());
  for (int i = 0; i < k; i++) {
    cout << e[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)

typedef long long i64;
typedef pair<i64, i64> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int x, y, z, k;
i64 a[1010], b[1010], c[1010];
i64 d[1010 * 1010], e[3030 * 1010];

int main() {
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < y; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      d[i * y + j] = a[i] + b[j];
    }
  }
  sort(d, d + x * y, greater<i64>());

  for (int i = 0; i < min(k, x * y); i++) {
    for (int j = 0; j < z; j++) {
      e[i * z + j] = d[i] + c[j];
    }
  }

  sort(e, e + min(k, x * y) * z, greater<i64>());
  for (int i = 0; i < k; i++) {
    cout << e[i] << endl;
  }

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int X, Y, Z;
  long K;
  long A[1000];
  long B[1000];
  long C[1000];
  multiset<long> ansSet;

  scanf("%d %d %d %ld", &X, &Y, &Z, &K);
  for (int i = 0; i < X; i++)
    scanf("%ld", &A[i]);
  for (int i = 0; i < Y; i++)
    scanf("%ld", &B[i]);
  for (int i = 0; i < Z; i++)
    scanf("%ld", &C[i]);
  for (int i = 0; i < X; i++)
    for (int j = 0; j < Y; j++)
      for (int k = 0; k < Z; k++) {
        ansSet.insert(A[i] + B[j] + C[k]);
      }
  long a = 0;
  for (auto itr = ansSet.rbegin(); itr != ansSet.rend(); itr++) {
    printf("%ld\n", *itr);
    a++;
    if (a >= K)
      break;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int X, Y, Z;
  long K;
  long A[1000];
  long B[1000];
  long C[1000];
  multiset<long> ansSet;

  scanf("%d %d %d %ld", &X, &Y, &Z, &K);
  for (int i = 0; i < X; i++)
    scanf("%ld", &A[i]);
  for (int i = 0; i < Y; i++)
    scanf("%ld", &B[i]);
  for (int i = 0; i < Z; i++)
    scanf("%ld", &C[i]);
  for (int i = 0; i < X; i++)
    for (int j = 0; j < Y; j++)
      for (int k = 0; k < Z; k++) {
        long sum = A[i] + B[j] + C[k];
        if (ansSet.size() < K) {
          ansSet.insert(sum);
        } else {
          if (*ansSet.begin() < sum) {
            ansSet.erase(ansSet.begin());
            ansSet.insert(sum);
          }
        }
      }
  long a = 0;
  for (auto itr = ansSet.rbegin(); itr != ansSet.rend(); itr++) {
    printf("%ld\n", *itr);
    a++;
    if (a >= K)
      break;
  }
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
static ull tenq = 1000000000;
static ull mod = tenq + 7;

using namespace std;

int main() {
  ll K;
  vector<ll> num(3, 0);
  for (auto i = 0; i < 3; i++) {
    cin >> num[i];
  }
  cin >> K;
  vector<vector<ll>> cakes(num.size());
  for (auto i = 0; i < 3; i++) {
    cakes[i] = vector<ll>(num[i]);
    for (auto j = 0; j < num[i]; j++)
      cin >> cakes[i][j];
    // sort(cakes[i].begin(), cakes[i].end(), std::greater<ll>());
  }
  vector<ll> current(num[2], 0);
  current.push_back(0);
  vector<ll> best(K + 1, 0);
  vector<ll> best_new(K + 1, 0);
  for (auto i = 0; i < num[0]; i++)
    for (auto j = 0; j < num[1]; j++) {
      for (auto k = 0; k < num[2]; k++) {
        ll value = cakes[0][i] + cakes[1][j] + cakes[2][k];
        current[k] = value;
      }
      sort(current.begin(), current.end(), greater<ll>());
      for (auto k = 0, i1 = 0, i2 = 0; k < best.size(); k++) {
        if (best[i1] == 0 && current[i2] == 0)
          break;
        if (best[i1] > current[i2]) {
          best_new[k] = best[i1];
          i1++;
        } else {
          best_new[k] = current[i2];
          i2++;
        }
      }
      best = best_new;
    }
  best.pop_back();
  for (auto x : best)
    cout << x << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
static ull tenq = 1000000000;
static ull mod = tenq + 7;

using namespace std;

int main() {
  ll K;
  vector<ll> num(3, 0);
  for (auto i = 0; i < 3; i++) {
    cin >> num[i];
  }
  cin >> K;
  vector<vector<ll>> cakes(num.size());
  for (auto i = 0; i < 3; i++) {
    cakes[i] = vector<ll>(num[i]);
    for (auto j = 0; j < num[i]; j++)
      cin >> cakes[i][j];
    // sort(cakes[i].begin(), cakes[i].end(), std::greater<ll>());
  }
  vector<ll> best;
  vector<ll> bestXY;
  for (auto x : cakes[0])
    for (auto y : cakes[1])
      bestXY.push_back(x + y);
  sort(bestXY.begin(), bestXY.end(), greater<ll>());
  for (auto i = 0; i < min((ll)bestXY.size(), K); i++) {
    for (auto z : cakes[2])
      best.push_back(bestXY[i] + z);
  }
  sort(best.begin(), best.end(), greater<ll>());

  for (auto i = 0; i < min((ll)best.size(), K); i++)
    cout << best[i] << endl;
  return 0;
}
Problem p03078 (Python)

Buggy submission (status: Runtime Error):
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) <= K:
                ans.append(A[x] + B[y] + C[z])
            else:
                break

ans.sort(revere=True)
print(*ans[:K], sep="\n")


Fixed submission:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) <= K:
                ans.append(A[x] + B[y] + C[z])
            else:
                break

ans.sort(reverse=True)
print(*ans[:K], sep="\n")
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rg register
#define ll long long
using namespace std;
const int N = 1e3 + 5, mod = 1e9 + 7;
int n, m, T, k, ans, len;
ll a[N], b[N], c[N], d[N], t[N];
bool cmp(ll x, ll y) { return x > y; }
int main() {
  ll ans = 0;
  priority_queue<pair<ll, ll>> q;
  int x, y, z, k, temp = 0;
  cin >> x >> y >> z >> k;
  for (rg int i = 1; i <= x; ++i)
    scanf("%lld", &a[i]);
  for (rg int i = 1; i <= y; ++i)
    scanf("%lld", &b[i]);
  sort(a + 1, a + x + 1, cmp);
  sort(b + 1, b + y + 1, cmp);
  for (rg int i = 1; i <= x; i++)
    for (rg int j = 1; j <= y; j++)
      c[++temp] = a[i] + b[j];
  sort(c + 1, c + temp + 1, cmp);
  for (rg int i = 1; i <= z; ++i)
    scanf("%lld", &d[i]);
  sort(d + 1, d + z + 1, cmp);
  for (rg int i = 1; i <= z; ++i) {
    t[i] = 1;
    q.push(pair<ll, ll>(c[1] + d[i], i));
  }
  ll i;
  while (k--) {
    cout << q.top().first << endl;
    i = q.top().second;
    q.pop();
    q.push(pair<ll, ll>(c[++t[i]] + d[i], i));
  }

  // cout<<ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rg register
#define ll long long
using namespace std;
const int N = 1e3 + 5, mod = 1e9 + 7;
int n, m, T, k, ans, len;
ll a[N], b[N], d[N], t[N];
ll c[1000005];
bool cmp(ll x, ll y) { return x > y; }
int main() {
  ll ans = 0;
  priority_queue<pair<ll, ll>> q;
  int x, y, z, k, temp = 0;
  cin >> x >> y >> z >> k;
  for (rg int i = 1; i <= x; ++i)
    scanf("%lld", &a[i]);
  for (rg int i = 1; i <= y; ++i)
    scanf("%lld", &b[i]);
  sort(a + 1, a + x + 1, cmp);
  sort(b + 1, b + y + 1, cmp);
  for (rg int i = 1; i <= x; i++)
    for (rg int j = 1; j <= y; j++)
      c[++temp] = a[i] + b[j];
  sort(c + 1, c + temp + 1, cmp);
  for (rg int i = 1; i <= z; ++i)
    scanf("%lld", &d[i]);
  sort(d + 1, d + z + 1, cmp);
  for (rg int i = 1; i <= z; ++i) {
    t[i] = 1;
    q.push(pair<ll, ll>(c[1] + d[i], i));
  }
  ll i;
  while (k--) {
    cout << q.top().first << endl;
    i = q.top().second;
    q.pop();
    q.push(pair<ll, ll>(c[++t[i]] + d[i], i));
  }

  // cout<<ans;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1000], b[1000], c[1000];
int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];
  sort(a, a + x);
  sort(b, b + y);
  sort(c, c + z);
  reverse(a, a + x);
  reverse(b, b + x);
  reverse(c, c + x);

  vector<ll> ab;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }
  sort(ab.begin(), ab.end());
  reverse(ab.begin(), ab.end());

  vector<ll> abc;
  for (int i = 0; i < ab.size(); i++) {
    for (int j = 0; j < z; j++) {
      abc.push_back(ab[i] + c[j]);
    }
  }
  sort(abc.begin(), abc.end());
  reverse(abc.begin(), abc.end());

  for (int i = 0; i < k; i++)
    cout << abc[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1000], b[1000], c[1000];
int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];
  sort(a, a + x);
  sort(b, b + y);
  sort(c, c + z);
  reverse(a, a + x);
  reverse(b, b + x);
  reverse(c, c + x);

  vector<ll> ab;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }
  sort(ab.begin(), ab.end());
  reverse(ab.begin(), ab.end());

  vector<ll> abc;
  for (int i = 0; i < min(k, (int)ab.size()); i++) {
    for (int j = 0; j < z; j++) {
      abc.push_back(ab[i] + c[j]);
    }
  }
  sort(abc.begin(), abc.end());
  reverse(abc.begin(), abc.end());

  for (int i = 0; i < k; i++)
    cout << abc[i] << endl;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll a[1005], b[1005], c[1005];
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];

  sort(a, a + x);
  reverse(a, a + x);
  sort(b, b + y);
  reverse(b, b + y);
  sort(c, c + z);
  reverse(c, c + z);

  vector<ll> abc;

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if ((i + 1) * (j + 1) > k)
        break;
      for (int l = 0; l < z; l++) {
        if ((i + 1) + (j + 1) + (l + 1) > k)
          break;
        abc.push_back(a[i] + b[j] + c[l]);
      }
    }
  }

  sort(abc.begin(), abc.end());
  reverse(abc.begin(), abc.end());

  for (int i = 0; i < k; i++)
    cout << abc[i] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll a[1005], b[1005], c[1005];
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];

  sort(a, a + x);
  reverse(a, a + x);
  sort(b, b + y);
  reverse(b, b + y);
  sort(c, c + z);
  reverse(c, c + z);

  vector<ll> abc;

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if ((i + 1) * (j + 1) > k)
        break;
      for (int l = 0; l < z; l++) {
        if ((i + 1) * (j + 1) * (l + 1) > k)
          break;
        abc.push_back(a[i] + b[j] + c[l]);
      }
    }
  }

  sort(abc.begin(), abc.end());
  reverse(abc.begin(), abc.end());

  for (int i = 0; i < k; i++)
    cout << abc[i] << endl;

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int x, y, z, k;

  cin >> x >> y >> z >> k;

  vector<ll> a(x), b(y), c(z), ab, abc;

  for (int i = 0; i < x; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < y; i++) {
    cin >> b[i];
  }

  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }

  for (auto i : ab) {
    for (auto j : c) {
      abc.push_back(i + j);
    }
  }

  sort(abc.begin(), abc.end(), greater<ll>());

  for (int i = 0; i < k; i++) {
    cout << abc[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int x, y, z, k;

  cin >> x >> y >> z >> k;

  vector<ll> a(x), b(y), c(z), ab, abc;

  for (int i = 0; i < x; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < y; i++) {
    cin >> b[i];
  }

  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }

  sort(ab.begin(), ab.end(), greater<ll>());

  for (int i = 0; i < min(k, x * y); i++) {
    for (int j = 0; j < z; j++) {
      abc.push_back(ab[i] + c[j]);
    }
  }

  sort(abc.begin(), abc.end(), greater<ll>());

  for (int i = 0; i < k; i++) {
    cout << abc[i] << endl;
  }
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
using namespace std;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<long long> xx(X), yy(Y), zz(Z);
  for (int i = 0; i < X; ++i)
    cin >> xx[i];
  sort(all(xx), greater<long long>());
  for (int i = 0; i < Y; ++i)
    cin >> yy[i];
  sort(all(yy), greater<long long>());
  for (int i = 0; i < Z; ++i)
    cin >> zz[i];
  sort(all(zz), greater<long long>());

  vector<long long> res;
  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      for (int k = 0; k < Z; ++k) {
        if ((i + 1) * (j + 1) * (k + 1) >= K)
          break;
        res.push_back(xx[i] + yy[j] + zz[k]);
      }
    }
  }
  sort(all(res), greater<long long>());
  for (int i = 0; i < K; ++i)
    cout << res[i] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
using namespace std;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<long long> xx(X), yy(Y), zz(Z);
  for (int i = 0; i < X; ++i)
    cin >> xx[i];
  sort(all(xx), greater<long long>());
  for (int i = 0; i < Y; ++i)
    cin >> yy[i];
  sort(all(yy), greater<long long>());
  for (int i = 0; i < Z; ++i)
    cin >> zz[i];
  sort(all(zz), greater<long long>());

  vector<long long> res;
  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      for (int k = 0; k < Z; ++k) {
        if ((i + 1) * (j + 1) * (k + 1) > K)
          break;
        res.push_back(xx[i] + yy[j] + zz[k]);
      }
    }
  }
  sort(all(res), greater<long long>());
  for (int i = 0; i < K; ++i)
    cout << res[i] << "\n";
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, e) for (int i = s; i < e; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (int i = s - 1; i >= s; i--)

int main() {

  int x, y, z, k;
  cin >> x >> y >> z >> k;

  ll a[x], b[y], c[z];
  priority_queue<ll> pq;
  rep(i, x) { cin >> a[i]; }
  rep(i, y) { cin >> b[i]; }
  rep(i, z) { cin >> c[i]; }

  sort(a, a + x, greater<ll>());
  sort(b, b + y, greater<ll>());
  sort(c, c + z, greater<ll>());

  rep(ia, min(x, 500)) {
    rep(ib, min(y, 500)) {
      rep(ic, min(z, 500)) { pq.push(a[ia] + b[ib] + c[ic]); }
    }
  }

  rep(i, k) {
    cout << pq.top() << endl;
    pq.pop();
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, e) for (int i = s; i < e; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (int i = s - 1; i >= s; i--)

int main() {

  int x, y, z, k;
  cin >> x >> y >> z >> k;

  ll a[x], b[y], c[z];
  priority_queue<ll> pq;
  rep(i, x) { cin >> a[i]; }
  rep(i, y) { cin >> b[i]; }
  rep(i, z) { cin >> c[i]; }

  sort(a, a + x, greater<ll>());
  sort(b, b + y, greater<ll>());
  sort(c, c + z, greater<ll>());

  vector<ll> vab;
  rep(ia, x) {
    rep(ib, y) { vab.push_back(a[ia] + b[ib]); }
  }

  sort(vab.begin(), vab.end(), greater<ll>());

  rep(iab, min(k, (int)vab.size())) {
    rep(ic, z) { pq.push(vab[iab] + c[ic]); }
  }

  rep(i, k) {
    cout << pq.top() << endl;
    pq.pop();
  }

  return 0;
}
Problem p03078 (C++)

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

using namespace std;
int main(void) {
  // Your code here!
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<long long> vecx(x, 0), vecy(y, 0), vecz(z, 0), vectemp(x * y, 0),
      ans(max(x * y, 3000) * z, 0);
  for (int i = 0; i < x; i++) {
    cin >> vecx[i];
  }
  for (int i = 0; i < y; i++) {
    cin >> vecy[i];
  }
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      vectemp[i + x * j] = vecx[i] + vecy[j];
    }
  }
  sort(vectemp.rbegin(), vectemp.rend());

  for (int i = 0; i < y; i++) {
    cin >> vecz[i];
  }

  for (int i = 0; i < min(x * y, 3000); i++) {
    for (int j = 0; j < z; j++) {
      ans[i + min(x * y, 3000) * j] = vectemp[i] + vecz[j];
    }
  }

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

  for (int i = 0; i < k; i++) {
    cout << ans[i] << endl;
  }
}


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

using namespace std;
int main(void) {
  // Your code here!
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<long long> vecx(x, 0), vecy(y, 0), vecz(z, 0), vectemp(x * y, 0),
      ans(min(x * y, 3000) * z, 0);
  for (int i = 0; i < x; i++) {
    cin >> vecx[i];
  }
  for (int i = 0; i < y; i++) {
    cin >> vecy[i];
  }
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      vectemp[i + x * j] = vecx[i] + vecy[j];
    }
  }
  sort(vectemp.rbegin(), vectemp.rend());

  for (int i = 0; i < y; i++) {
    cin >> vecz[i];
  }

  for (int i = 0; i < min(x * y, 3000); i++) {
    for (int j = 0; j < z; j++) {
      ans[i + min(x * y, 3000) * j] = vectemp[i] + vecz[j];
    }
  }

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

  for (int i = 0; i < k; i++) {
    cout << ans[i] << endl;
  }
}
Problem p03078 (C++)

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

long long x[1005], y[1005], z[1005], xyz[3010], X, Y, Z, K;

bool cmp(const long long &x, const long long &y) { return x > y; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> X >> Y >> Z >> K;
  for (int i = 1; i <= X; i++)
    cin >> x[i];
  for (int i = 1; i <= Y; i++)
    cin >> y[i];
  for (int i = 1; i <= Z; i++)
    cin >> z[i];
  sort(x + 1, x + X + 1, cmp);
  sort(y + 1, y + Y + 1, cmp);
  sort(z + 1, z + Z + 1, cmp);
  int cnt = 0;
  for (int i = 1; i <= X; i++)
    for (int j = 1; j <= Y; j++)
      for (int k = 1; k <= Z; k++) {
        if (i * j * k > K)
          break;
        cnt++;
        xyz[cnt] = x[i] + y[j] + z[k];
      }
  sort(xyz + 1, xyz + cnt + 1, cmp);
  for (int l = 1; l <= K; l++)
    cout << xyz[l] << "\n";

  return 0;
}

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

long long x[1005], y[1005], z[1005], xyz[3000010], X, Y, Z, K;

bool cmp(const long long &x, const long long &y) { return x > y; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> X >> Y >> Z >> K;
  for (int i = 1; i <= X; i++)
    cin >> x[i];
  for (int i = 1; i <= Y; i++)
    cin >> y[i];
  for (int i = 1; i <= Z; i++)
    cin >> z[i];
  sort(x + 1, x + X + 1, cmp);
  sort(y + 1, y + Y + 1, cmp);
  sort(z + 1, z + Z + 1, cmp);
  int cnt = 0;
  for (int i = 1; i <= X; i++)
    for (int j = 1; j <= Y; j++)
      for (int k = 1; k <= Z; k++) {
        if (i * j * k > K)
          break;
        cnt++;
        xyz[cnt] = x[i] + y[j] + z[k];
      }
  sort(xyz + 1, xyz + cnt + 1, cmp);
  for (int l = 1; l <= K; l++)
    cout << xyz[l] << "\n";

  return 0;
}
Problem p03078 (C++)

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

// Define
using ll = int64_t;
using ul = uint64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ul MAX = ULONG_MAX;
const char nl = '\n';

#define elif else if
#define def inline auto
#define run __attribute__((constructor)) def _

// Debug
#define debug(x) cerr << (x) << " (L:" << __LINE__ << ")" << '\n'

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i < _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i > _##i; --i)
#define rep(i, n) inc(i, 0, n)
#define be(c) begin(c), end(c)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

// Speed
run() { fasten, fout(10); }
#pragma GCC optimize("-O3")
#pragma GCC target("avx")

ll x(ll a) {
  if (a % 4 == 0)
    return a;
  if (a % 4 == 1)
    return 1;
  if (a % 4 == 2)
    return a + 1;
  return 0;
}

signed main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<ll> as(x), bs(y), cs(z);
  rep(i, x) cin >> as[i];
  rep(i, y) cin >> bs[i];
  rep(i, z) cin >> cs[i];
  sort(be(as), greater<ll>());
  sort(be(bs), greater<ll>());
  sort(be(cs), greater<ll>());

  vector<ll> ans;
  rep(i, x) {
    rep(j, y) {
      rep(k, z) {
        if (i + j + k > K)
          break;
        ans.push_back(as[i] + bs[j] + cs[k]);
      }
    }
  }
  sort(be(ans), greater<ll>());
  ans.resize(K);
  for (auto e : ans) {
    cout << e << nl;
  }
}


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

// Define
using ll = int64_t;
using ul = uint64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ul MAX = ULONG_MAX;
const char nl = '\n';

#define elif else if
#define def inline auto
#define run __attribute__((constructor)) def _

// Debug
#define debug(x) cerr << (x) << " (L:" << __LINE__ << ")" << '\n'

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i < _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i > _##i; --i)
#define rep(i, n) inc(i, 0, n)
#define be(c) begin(c), end(c)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

// Speed
run() { fasten, fout(10); }
#pragma GCC optimize("-O3")
#pragma GCC target("avx")

ll x(ll a) {
  if (a % 4 == 0)
    return a;
  if (a % 4 == 1)
    return 1;
  if (a % 4 == 2)
    return a + 1;
  return 0;
}

signed main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<ll> as(x), bs(y), cs(z);
  rep(i, x) cin >> as[i];
  rep(i, y) cin >> bs[i];
  rep(i, z) cin >> cs[i];
  sort(be(as), greater<ll>());
  sort(be(bs), greater<ll>());
  sort(be(cs), greater<ll>());

  vector<ll> ans;
  rep(i, x) {
    rep(j, y) {
      rep(k, z) {
        if ((i + 1) * (j + 1) * (k + 1) > K)
          break;
        ans.push_back(as[i] + bs[j] + cs[k]);
      }
    }
  }
  sort(be(ans), greater<ll>());
  ans.resize(K);
  for (auto e : ans) {
    cout << e << nl;
  }
}
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

print(*abclst[:K], sep="\n")


Fixed submission:
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

a.sort(reverse=True)
b.sort(reverse=True)

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

print(*abclst[:K], sep="\n")
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
import itertools


X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB = sorted(AB)[::-1]
ans = [ab + c for (ab, c) in list(itertools.product(AB[:3000], C))]
ans = sorted(ans)[::-1]
for i in range(K):
    print(ans[i])


Fixed submission:
import itertools


X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[: min(3000, X * Y * Z)], C))]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import product, chain

x, y, z, k = list(map(int, input().split(" ")))
a = sorted(list(map(int, input().split(" "))))
b = sorted(list(map(int, input().split(" "))))
c = sorted(list(map(int, input().split(" "))))

apb_sorted = sorted([a + b for a, b in product(a, b)])
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))


Fixed submission:
from itertools import product, chain

x, y, z, k = list(map(int, input().split(" ")))
a = sorted(list(map(int, input().split(" "))))
b = sorted(list(map(int, input().split(" "))))
c = sorted(list(map(int, input().split(" "))))

apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import product

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

p = [i + k for i, k in product(a, b)]
p.sort(reverse=True)
q = [i + k for i, k in product(p[:k], c)]
q.sort(reverse=True)

print("\n".join(map(str, q[:k])))


Fixed submission:
from itertools import product

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

p = [i + k for i, k in product(a, b)]
p.sort(reverse=True)
q = [i + k for i, k in product(p[:k], c)]
q.sort(reverse=True)
print("\n".join(map(str, q[:k])))
Problem p03078 (Python)

Buggy submission (status: Runtime Error):
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

d = []
for i in range(x):
    for j in range(y):
        d.append(a[i] + b[j])
d.sort(reverse=True)

e = []
for i in range(k):
    for j in range(z):
        e.append(d[i] + c[j])
e.sort(reverse=True)

for i in range(k):
    print(e[i])


Fixed submission:
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

d = []
for i in range(x):
    for j in range(y):
        d.append(a[i] + b[j])
d.sort(reverse=True)

e = []
for i in range(min(len(d), k)):
    for j in range(z):
        e.append(d[i] + c[j])
e.sort(reverse=True)

for i in range(k):
    print(e[i])
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)
i = 0
while i != k:
    print(e[i])
    i += 1


Fixed submission:
x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)[:k]
print(*e)
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    ret = sorted(
        (
            a + b + c
            for p, a in enumerate(A)
            for q, b in enumerate(B)
            for r, c in enumerate(C)
            if p * q * r <= k
        ),
        reverse=True,
    )[:k]

    print(*ret, sep="\n")


main()

# O(Klog^3K)
# https://img.atcoder.jp/abc123/editorial.pdf


Fixed submission:
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    ret = []
    for p, a in enumerate(A):
        for q, b in enumerate(B):
            if p * q > k:
                break
            for r, c in enumerate(C):
                if p * q * r > k:
                    break
                ret.append(a + b + c)
    ret.sort(reverse=True)
    print(*ret[:k], sep="\n")


main()

# O(Klog^3K)
# https://img.atcoder.jp/abc123/editorial.pdf
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

ABSum = sorted([i + j for i in ABCList[0] for j in ABCList[1]], reverse=True)[:K]
for ans in sorted([i + j for i in ABSum for j in ABCList[2]], reverse=True)[:K]:
    print(ans)


Fixed submission:
X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

ansList = []
for i, A in enumerate(ABCList[0]):
    for j, B in enumerate(ABCList[1]):
        for k, C in enumerate(ABCList[2]):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ansList.append(A + B + C)
            else:
                break

for ans in sorted(ansList, reverse=True)[:K]:
    print(ans)
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int X, Y, Z, K;
  vector<long long> A(1001, 0);
  vector<long long> B(1001, 0);
  vector<long long> C(1001, 0);

  vector<long long> ret;
  vector<long long> ret2;

  cin >> X >> Y >> Z >> K;
  for (int i = 0; i < X; i++)
    cin >> A[i];
  for (int i = 0; i < Y; i++)
    cin >> B[i];
  for (int i = 0; i < Z; i++)
    cin >> C[i];

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

  for (int a = 0; a < X; a++) {
    for (int b = 0; b < Y; b++) {
      ret.push_back(A[a] + B[b]);
    }
  }
  sort(ret.begin(), ret.end(), greater<long long>());

  for (int k = 0; k < X * Y; k++) {
    for (int c = 0; c < Z; c++) {
      ret2.push_back(ret[k] + C[c]);
    }
  }
  sort(ret2.begin(), ret2.end(), greater<long long>());

  for (int i = 0; i < K; i++) {
    cout << ret2[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int X, Y, Z, K;
  vector<long long> A(1001, 0);
  vector<long long> B(1001, 0);
  vector<long long> C(1001, 0);

  vector<long long> ret;
  vector<long long> ret2;

  cin >> X >> Y >> Z >> K;
  for (int i = 0; i < X; i++)
    cin >> A[i];
  for (int i = 0; i < Y; i++)
    cin >> B[i];
  for (int i = 0; i < Z; i++)
    cin >> C[i];

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

  for (int a = 0; a < X; a++) {
    for (int b = 0; b < Y; b++) {
      ret.push_back(A[a] + B[b]);
    }
  }
  sort(ret.begin(), ret.end(), greater<long long>());

  for (int k = 0; k < min(K, X * Y); k++) {
    for (int c = 0; c < Z; c++) {
      ret2.push_back(ret[k] + C[c]);
    }
  }
  sort(ret2.begin(), ret2.end(), greater<long long>());

  for (int i = 0; i < K; i++) {
    cout << ret2[i] << endl;
  }
}
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
X, Y, Z, K = map(int, input().split())
(*A,) = sorted(map(int, input().split()), reverse=True)
(*B,) = sorted(map(int, input().split()), reverse=True)
(*C,) = sorted(map(int, input().split()), reverse=True)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy] + C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])


Fixed submission:
X, Y, Z, K = map(int, input().split())
(*A,) = sorted(map(int, input().split()), reverse=True)
(*B,) = sorted(map(int, input().split()), reverse=True)
(*C,) = sorted(map(int, input().split()), reverse=True)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy] + C[z] for z in range(Z) for xy in range(XY)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
goukei = []
for i in range(x):
    for j in range(y):
        goukei.append(A[i] + B[j])
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")


Fixed submission:
x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")
Problem p03078 (Python)

Buggy submission (status: Runtime Error):
x, y, z, k = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = []
for a in A:
    for b in B:
        AB.append(a + b)

AB.sort(reverse=True)

ABC = []
for i in range(min(a * b, k)):
    for c in C:
        ABC.append(AB[i] + c)

ABC.sort(reverse=True)

for i in range(k):
    print(ABC[i])


Fixed submission:
x, y, z, k = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = []
for a in A:
    for b in B:
        AB.append(a + b)

AB.sort(reverse=True)

ABC = []
for i in range(min(x * y, k)):
    for c in C:
        ABC.append(AB[i] + c)

ABC.sort(reverse=True)

for i in range(k):
    print(ABC[i])
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)


def solver1():
    ab_array = [x + y for x in a_array for y in b_array]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)


solver1()


Fixed submission:
x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)


def solver1():
    ab_array = sorted([x + y for x in a_array for y in b_array], reverse=True)[:3000]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)


solver1()
Problem p03078 (C++)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF 1000000000000000000
#define MOD 10007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<ll> A(X), B(Y), C(Z);
  for (int i = 0; i < X; i++)
    cin >> A[i];
  for (int i = 0; i < Y; i++)
    cin >> B[i];
  for (int i = 0; i < Z; i++)
    cin >> C[i];
  sort(rrng(A));
  sort(rrng(B));
  sort(rrng(C));

  vector<ll> AB;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      AB.push_back(A[i] + B[j]);
    }
  }
  sort(rrng(AB));
  vector<ll> ABC;
  for (int i = 0; i < Z; i++) {
    for (int j = 0; j < K && j < ABC.size(); j++) {
      ABC.push_back(AB[j] + C[i]);
    }
  }
  sort(rrng(ABC));
  for (int i = 0; i < K; i++)
    cout << ABC[i] << endl;
  return 0;
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF 1000000000000000000
#define MOD 10007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<ll> A(X), B(Y), C(Z);
  for (int i = 0; i < X; i++)
    cin >> A[i];
  for (int i = 0; i < Y; i++)
    cin >> B[i];
  for (int i = 0; i < Z; i++)
    cin >> C[i];
  sort(rrng(A));
  sort(rrng(B));
  sort(rrng(C));

  vector<ll> AB;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      AB.push_back(A[i] + B[j]);
    }
  }
  sort(rrng(AB));
  vector<ll> ABC;
  for (int i = 0; i < Z; i++) {
    for (int j = 0; j < K && j < AB.size(); j++) {
      ABC.push_back(AB[j] + C[i]);
    }
  }
  sort(rrng(ABC));
  for (int i = 0; i < K; i++)
    cout << ABC[i] << endl;
  return 0;
}
Problem p03078 (Python)

Buggy submission (status: Time Limit Exceeded):
import heapq


def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt


X, Y, Z, K = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
ans = []
heap_queue = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            heappush_max(heap_queue, A[i] + B[j] + C[k])
for k in range(K):
    print(heappop_max(heap_queue))


Fixed submission:
import heapq


def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt


X, Y, Z, K = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
ans = []
heap_queue = [(A[0] + B[0] + C[0], 0, 0, 0)]
arg_hash = {}

for i in range(K):
    maxim, a, b, c = heappop_max(heap_queue)
    print(maxim)
    arg_a = (a + 1, b, c)
    arg_b = (a, b + 1, c)
    arg_c = (a, b, c + 1)

    if a + 1 < X and arg_a not in arg_hash:
        heappush_max(heap_queue, (A[a + 1] + B[b] + C[c], a + 1, b, c))
        arg_hash[arg_a] = 1
    if b + 1 < Y and arg_b not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b + 1] + C[c], a, b + 1, c))
        arg_hash[arg_b] = 1
    if c + 1 < Z and arg_c not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b] + C[c + 1], a, b, c + 1))
        arg_hash[arg_c] = 1
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define cs                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;

using namespace std;
int main() {
  cs;
  int x, y, z, K, t = 0;
  cin >> x >> y >> z >> K;
  vector<ll> a(x);
  vector<ll> b(y);
  vector<ll> c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  vector<ll> ans(100000);
  rep(i, x) {
    rep(j, y) {
      rep(k, z) {
        if ((i + 1) * (j + 1) * (k + 1) <= K) {
          ans[t] = a[i] + b[j] + c[k];
          t++;
        }
      }
    }
  }
  sort(ans.begin(), ans.begin() + t, greater<>());
  rep(i, K) cout << ans[i] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define cs                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;

using namespace std;
int main() {
  cs;
  int x, y, z, K, t = 0;
  cin >> x >> y >> z >> K;
  vector<ll> a(x);
  vector<ll> b(y);
  vector<ll> c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  vector<ll> ans(1000000);
  rep(i, x) {
    rep(j, y) {
      rep(k, z) {
        if ((i + 1) * (j + 1) * (k + 1) <= K) {
          ans[t] = a[i] + b[j] + c[k];
          t++;
        }
      }
    }
  }
  sort(ans.begin(), ans.begin() + t, greater<>());
  rep(i, K) cout << ans[i] << '\n';
}
Problem p03078 (C++)

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

using namespace std;

long long x, y, z, K, a[1005], b[1005], c[1005];
vector<long long> vec;

int main() {
  cin >> x >> y >> z >> K;

  for (int i = 0; i < x; ++i)
    cin >> a[i];
  sort(a, a + x, greater<long long>());

  for (int i = 0; i < y; ++i)
    cin >> b[i];
  sort(b, b + y, greater<long long>());

  for (int i = 0; i < z; ++i)
    cin >> c[i];
  sort(c, c + z, greater<long long>());

  for (int i = 0; i < x; ++i) {
    for (int j = 0; j < y; ++j) {
      for (int k = 1; k < z; ++k) {
        if ((i + 1) * (j + 1) * (k + 1) <= K)
          vec.push_back(a[i] + b[j] + c[k]);
        else
          break;
      }
    }
  }

  sort(vec.begin(), vec.end(), greater<long long>());
  for (int i = 0; i < K; ++i)
    cout << vec[i] << endl;
}

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

using namespace std;

long long x, y, z, K, a[1005], b[1005], c[1005];
vector<long long> vec;

int main() {
  cin >> x >> y >> z >> K;

  for (int i = 0; i < x; ++i)
    cin >> a[i];
  sort(a, a + x, greater<long long>());

  for (int i = 0; i < y; ++i)
    cin >> b[i];
  sort(b, b + y, greater<long long>());

  for (int i = 0; i < z; ++i)
    cin >> c[i];
  sort(c, c + z, greater<long long>());

  for (int i = 0; i < x; ++i) {
    for (int j = 0; j < y; ++j) {
      for (int k = 0; k < z; ++k) {
        if ((i + 1) * (j + 1) * (k + 1) <= K)
          vec.push_back(a[i] + b[j] + c[k]);
        else
          break;
      }
    }
  }

  sort(vec.begin(), vec.end(), greater<long long>());
  for (int i = 0; i < K; ++i)
    cout << vec[i] << endl;
}