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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;

long long t[100000];
int ti = 0;

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

  for (long long i = 0; i < x; ++i)
    for (long long j = 0; j < y; ++j)
      for (long long l = 0; l < z; ++l) {
        if ((i + 1) * (j + 1) * (l + 1) <= k) {
          t[ti] = a[i] + b[j] + c[l];
          ++ti;
          continue;
        } else
          break;
      }
  sort(t, t + ti, greater<long long>());
  for (long long i = 0; i < k; ++i)
    cout << t[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;

long long t[100000000];
int ti = 0;

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

  for (long long i = 0; i < x; ++i)
    for (long long j = 0; j < y; ++j)
      for (long long l = 0; l < z; ++l) {
        if ((i + 1) * (j + 1) * (l + 1) <= k) {
          t[ti] = a[i] + b[j] + c[l];
          ++ti;
          continue;
        } else
          break;
      }
  sort(t, t + ti, greater<long long>());
  for (long long i = 0; i < k; ++i)
    cout << t[i] << endl;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (lint)(1LL << 56)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
#define fi first
#define se second
typedef long long lint;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
  lint X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  lint A[X], B[Y], C[Z];
  rep(i, X) cin >> A[i];
  rep(i, Y) cin >> B[i];
  rep(i, Z) cin >> C[i];
  lint N = X * Y;
  lint cake[N];
  rep(i, X) rep(j, Y) cake[i * Y + j] = A[i] + B[j];
  sort(cake, cake + N, greater<lint>());
  lint n = min(3000LL, X * Y);
  lint nw[n];
  rep(i, n) nw[i] = cake[i];
  sort(nw, nw + n);
  rep(i, K) {
    lint cnt = 0;
    lint hi = 4e10, lo = 0;
    while (hi - lo > 1) {
      cnt = 0;
      lint mid = (hi + lo) / 2;
      rep(j, Z) {
        auto itr = upper_bound(nw, nw + n, mid - C[j]);
        cnt += n - (itr - nw);
      }
      (cnt > i ? lo : hi) = mid;
    }
    cout << hi << endl;
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (lint)(1LL << 56)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
#define fi first
#define se second
typedef long long lint;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
  lint X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  lint A[X], B[Y], C[Z];
  rep(i, X) cin >> A[i];
  rep(i, Y) cin >> B[i];
  rep(i, Z) cin >> C[i];
  lint N = X * Y;
  lint cake[N];
  rep(i, X) rep(j, Y) cake[i * Y + j] = A[i] + B[j];
  sort(cake, cake + N, greater<lint>());
  lint n = min(3000LL, X * Y);
  lint nw[n];
  rep(i, n) nw[i] = cake[i];
  lint ans[n * Z];
  rep(i, n) rep(j, Z) ans[i * Z + j] = nw[i] + C[j];
  sort(ans, ans + n * Z, greater<lint>());
  rep(i, K) cout << ans[i] << endl;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

#define INF (1 << 21);
typedef long long ll;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  ll A[1000], B[1000], C[1000];
  vector<ll> x;
  vector<ll> y;
  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, greater<>());
  sort(B, B + Y, greater<>());
  sort(C, C + Z, greater<>());
  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      x.push_back(A[i] + B[j]);
    }
  }
  sort(x.begin(), x.end(), greater<>());

  for (int i = 0; i < X * Y; ++i) {
    for (int j = 0; j < Z; ++j) {
      y.push_back(x[i] + C[j]);
    }
  }
  sort(y.begin(), y.end(), greater<>());
  for (int i = 0; i < K; ++i)
    cout << y[i] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

#define INF (1 << 21);
typedef long long ll;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  ll A[1000], B[1000], C[1000];
  vector<ll> x;
  vector<ll> y;
  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, greater<>());
  sort(B, B + Y, greater<>());
  sort(C, C + Z, greater<>());
  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      x.push_back(A[i] + B[j]);
    }
  }
  sort(x.begin(), x.end(), greater<>());

  for (int i = 0; i < K && i < X * Y; ++i) {
    for (int j = 0; j < Z; ++j) {
      y.push_back(x[i] + C[j]);
    }
  }
  sort(y.begin(), y.end(), greater<>());
  for (int i = 0; i < K; ++i)
    cout << y[i] << endl;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

vector<long long> A, B, C, AB, ABC;
int X, Y, Z, K;

int main() {
  cin >> X >> Y >> Z >> K;
  A.resize(X);
  B.resize(Y);
  C.resize(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];
  }
  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<long long>());
  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(), greater<long long>());
  for (int i = 0; i < K; i++) {
    cout << ABC[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

vector<long long> A, B, C, AB, ABC;
int X, Y, Z, K;

int main() {
  cin >> X >> Y >> Z >> K;
  A.resize(X);
  B.resize(Y);
  C.resize(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];
  }
  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<long long>());
  int len = min((int)AB.size(), K);
  for (int i = 0; i < len; i++) {
    for (int j = 0; j < Z; j++) {
      ABC.push_back(AB[i] + C[j]);
    }
  }
  sort(ABC.begin(), ABC.end(), greater<long long>());
  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>
#include <iostream>
using namespace std;

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

  cin >> X >> Y >> Z >> K;

  for (int i = 0; i < X; i++) {
    long long int x;
    cin >> x;
    A.push_back(x);
  }

  for (int i = 0; i < Y; i++) {
    long long int x;
    cin >> x;
    B.push_back(x);
  }

  for (int i = 0; i < Z; i++) {
    long long int x;
    cin >> x;
    C.push_back(x);
  }

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

  long int loopNum = 0;

  for (int i = 0; (i < X); i++) {
    for (int j = 0; (j < Y); j++) {
      for (int k = 0; (k < Z); k++) {
        ansList.push_back(A[i] + B[j] + C[k]);
      }
    }
  }

  sort(ansList.begin(), ansList.end(), greater<long long int>());

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

  return 0;
}

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

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

  cin >> X >> Y >> Z >> K;

  for (int i = 0; i < X; i++) {
    long long int x;
    cin >> x;
    A.push_back(x);
  }

  for (int i = 0; i < Y; i++) {
    long long int x;
    cin >> x;
    B.push_back(x);
  }

  for (int i = 0; i < Z; i++) {
    long long int x;
    cin >> x;
    C.push_back(x);
  }

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

  long int loopNum = 0;

  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) <= 3000)
          ansList.push_back(A[i] + B[j] + C[k]);
        else
          break;
      }
    }
  }

  sort(ansList.begin(), ansList.end(), greater<long long int>());

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

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long llong;

int main(int argc, const char *argv[]) {
  int X, Y, Z, K;

  cin >> X >> Y >> Z >> K;

  vector<llong> A(X);
  vector<llong> B(Y);
  vector<llong> 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];
  }

  vector<llong> 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(), greater<llong>());

  vector<llong> ABC;
  for (int i = 0; i < (int)AB.size(); i++) {
    for (int j = 0; j < Z; j++) {
      ABC.push_back(AB[i] + C[j]);
    }
  }
  sort(ABC.begin(), ABC.end(), greater<llong>());

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long llong;

int main(int argc, const char *argv[]) {
  int X, Y, Z, K;

  cin >> X >> Y >> Z >> K;

  vector<llong> A(X);
  vector<llong> B(Y);
  vector<llong> 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];
  }

  vector<llong> 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(), greater<llong>());

  vector<llong> 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(), greater<llong>());

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

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(a) cout << #a << ": " << a << endl;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  vector<ll> A(X);
  rep(i, X) { cin >> A[i]; }
  sort(A.begin(), A.end(), greater<ll>());
  vector<ll> B(X);
  rep(i, Y) { cin >> B[i]; }
  sort(B.begin(), B.end(), greater<ll>());
  vector<ll> C(X);
  rep(i, Z) { cin >> C[i]; }
  sort(C.begin(), C.end(), greater<ll>());

  vector<ll> AB;
  rep(x, X) rep(y, Y) { AB.push_back(A[x] + B[y]); }
  sort(AB.begin(), AB.end(), greater<ll>());
  vector<ll> ABC;
  rep(xy, (int)AB.size()) rep(z, Z) { ABC.push_back(AB[xy] + C[z]); }
  sort(ABC.begin(), ABC.end(), greater<ll>());

  for (int i = 0; i < (int)ABC.size() && i < K; i++) {
    cout << ABC[i] << endl;
  }
}


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

using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(a) cout << #a << ": " << a << endl;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  vector<ll> A(X);
  rep(i, X) { cin >> A[i]; }
  sort(A.begin(), A.end(), greater<ll>());
  vector<ll> B(X);
  rep(i, Y) { cin >> B[i]; }
  sort(B.begin(), B.end(), greater<ll>());
  vector<ll> C(X);
  rep(i, Z) { cin >> C[i]; }
  sort(C.begin(), C.end(), greater<ll>());

  vector<ll> AB;
  rep(x, X) rep(y, Y) { AB.push_back(A[x] + B[y]); }
  sort(AB.begin(), AB.end(), greater<ll>());
  vector<ll> ABC;
  rep(xy, min(K, (int)AB.size())) rep(z, Z) { ABC.push_back(AB[xy] + C[z]); }
  sort(ABC.begin(), ABC.end(), greater<ll>());

  for (int i = 0; i < (int)ABC.size() && i < K; i++) {
    cout << ABC[i] << endl;
  }
}
Problem p03078 (C++)

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

int main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<long long> as(x), bs(y), cs(z);
  for (int i = 0; i < x; i++)
    cin >> as[i];
  for (int i = 0; i < y; i++)
    cin >> bs[i];
  for (int i = 0; i < z; i++)
    cin >> cs[i];

  vector<long long> yz;
  yz.reserve(y * z);
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < z; j++) {
      yz.push_back(bs[i] + cs[j]);
    }
  }
  sort(yz.begin(), yz.end());

  auto num_gex = [&](long long k) {
    long long ret = 0;
    for (int i = 0; i < x; i++) {
      long long rem = k - as[i];
      ret += yz.size() - (lower_bound(yz.begin(), yz.end(), rem) - yz.begin());
    }
    return ret;
  };
  long long l = 0, r = 1e18;
  while (l < r - 1) {
    long long m = (l + r) / 2;
    if (num_gex(m) >= K) {
      l = m;
    } else {
      r = m;
    }
  }
  long long nax = l;
  vector<long long> ans;
  ans.reserve(3000);
  cerr << "nax: " << nax << endl;
  for (int i = 0; i < x; i++) {
    long long rem = nax - 1 - as[i];
    int lt = lower_bound(yz.begin(), yz.end(), rem) - yz.begin();
    for (int j = lt; j < yz.size(); j++) {
      ans.push_back(as[i] + yz[j]);
    }
  }
  assert(ans.size() <= K);
  while (ans.size() < K) {
    ans.push_back(nax);
  }
  sort(ans.rbegin(), ans.rend());
  for (long long a : ans) {
    cout << a << '\n';
  }
}

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

int main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<long long> as(x), bs(y), cs(z);
  for (int i = 0; i < x; i++)
    cin >> as[i];
  for (int i = 0; i < y; i++)
    cin >> bs[i];
  for (int i = 0; i < z; i++)
    cin >> cs[i];

  vector<long long> yz;
  yz.reserve(y * z);
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < z; j++) {
      yz.push_back(bs[i] + cs[j]);
    }
  }
  sort(yz.begin(), yz.end());

  auto num_gex = [&](long long k) {
    long long ret = 0;
    for (int i = 0; i < x; i++) {
      long long rem = k - as[i];
      ret += yz.size() - (lower_bound(yz.begin(), yz.end(), rem) - yz.begin());
    }
    return ret;
  };
  long long l = 0, r = 1e18;
  while (l < r - 1) {
    long long m = (l + r) / 2;
    if (num_gex(m) >= K) {
      l = m;
    } else {
      r = m;
    }
  }
  long long nax = l;
  vector<long long> ans;
  ans.reserve(3000);
  cerr << "nax: " << nax << endl;
  for (int i = 0; i < x; i++) {
    long long rem = nax + 1 - as[i];
    int lt = lower_bound(yz.begin(), yz.end(), rem) - yz.begin();
    for (int j = lt; j < yz.size(); j++) {
      ans.push_back(as[i] + yz[j]);
    }
  }
  assert(ans.size() <= K);
  while (ans.size() < K) {
    ans.push_back(nax);
  }
  sort(ans.rbegin(), ans.rend());
  for (long long a : ans) {
    cout << a << '\n';
  }
}
Problem p03078 (C++)

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

int main(int, char **) {
  int x, y, z, K;
  std::cin >> x >> y >> z >> K;

  std::vector<long long> as(x), bs(y), cs(z);
  for (int i{}; i < x; ++i) {
    std::cin >> as[i];
  }
  for (int i{}; i < y; ++i) {
    std::cin >> bs[i];
  }
  for (int i{}; i < z; ++i) {
    std::cin >> cs[i];
  }

  std::sort(begin(as), end(as), std::greater<long long>());
  std::sort(begin(bs), end(bs), std::greater<long long>());
  std::sort(begin(cs), end(cs), std::greater<long long>());

  std::vector<long long> ans;
  for (int i{}; i < x; ++i) {
    for (int j{}; j < y; ++j) {
      for (int k{}; k < z; ++k) {
        if (i + j + k <= K) {
          ans.push_back(as[i] + bs[j] + cs[k]);
        } else {
          break;
        }
      }
    }
  }
  std::sort(begin(ans), end(ans), std::greater<long long>());
  for (int i{}; i < K; ++i) {
    std::cout << ans[i] << std::endl;
  }

  return 0;
}


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

int main(int, char **) {
  int x, y, z, K;
  std::cin >> x >> y >> z >> K;

  std::vector<long long> as(x), bs(y), cs(z);
  for (int i{}; i < x; ++i) {
    std::cin >> as[i];
  }
  for (int i{}; i < y; ++i) {
    std::cin >> bs[i];
  }
  for (int i{}; i < z; ++i) {
    std::cin >> cs[i];
  }

  std::sort(begin(as), end(as), std::greater<long long>());
  std::sort(begin(bs), end(bs), std::greater<long long>());
  std::sort(begin(cs), end(cs), std::greater<long long>());

  std::vector<long long> ans;
  for (int i{}; i < x; ++i) {
    for (int j{}; j < y; ++j) {
      for (int k{}; k < z; ++k) {
        if ((i + 1) * (j + 1) * (k + 1) <= K) {
          ans.push_back(as[i] + bs[j] + cs[k]);
        } else {
          break;
        }
      }
    }
  }
  std::sort(begin(ans), end(ans), std::greater<long long>());
  for (int i{}; i < K; ++i) {
    std::cout << ans[i] << std::endl;
  }

  return 0;
}
Problem p03078 (C++)

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))

#define DEBUG(x) cout << #x << ": " << (x) << endl

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  vl A(X);
  vl B(Y);
  vl C(Z);

  FOR(i, 0, X) { cin >> A[i]; }
  FOR(i, 0, Y) { cin >> B[i]; }
  FOR(i, 0, Z) { cin >> C[i]; }

  sort(A.begin(), A.end());
  reverse(A.begin(), A.end()); // 降順ソート
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end()); // 降順ソート
  sort(C.begin(), C.end());
  reverse(C.begin(), C.end()); // 降順ソート

  int a, b, c;
  ll rep = A[0] + B[0] + C[0];

  priority_queue<ll> PQ;

  FOR(i, 0, min(X, K)) {
    FOR(j, 0, min(Y, K)) {
      FOR(k, 0, min(Z, K)) { PQ.push(A[i] + B[j] + C[k]); }
    }
  }

  FOR(i, 0, K) {
    cout << PQ.top() << endl;
    PQ.pop();
  }
}

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))

#define DEBUG(x) cout << #x << ": " << (x) << endl

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  vl A(X);
  vl B(Y);
  vl C(Z);

  FOR(i, 0, X) { cin >> A[i]; }
  FOR(i, 0, Y) { cin >> B[i]; }
  FOR(i, 0, Z) { cin >> C[i]; }

  sort(A.begin(), A.end());
  reverse(A.begin(), A.end()); // 降順ソート
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end()); // 降順ソート
  sort(C.begin(), C.end());
  reverse(C.begin(), C.end()); // 降順ソート

  int a, b, c;
  ll rep = A[0] + B[0] + C[0];

  priority_queue<ll> PQ;

  FOR(i, 0, min(X, K)) {
    FOR(j, 0, min(Z, Y)) {
      FOR(k, 0, min(Z, K)) {
        if ((i + 1) * (j * 1) * (k * 1) <= K)
          PQ.push(A[i] + B[j] + C[k]);
        else
          break;
      }
    }
  }

  FOR(i, 0, K) {
    cout << PQ.top() << endl;
    PQ.pop();
  }
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;

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

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

  rep(i, x) { cin >> a[i]; }
  rep(j, y) { cin >> b[j]; }
  rep(l, z) { cin >> c[l]; }

  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());

  priority_queue<ll> q;

  rep(i, x) {
    rep(j, y) {
      int tmp_z = min(z, K / (i + 1) / (j + 1) - 1);
      rep(l, tmp_z) { q.push(a[i] + b[j] + c[l]); }
    }
  }

  rep(i, K) {
    cout << q.top() << endl;
    q.pop();
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;

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

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

  rep(i, x) { cin >> a[i]; }
  rep(j, y) { cin >> b[j]; }
  rep(l, z) { cin >> c[l]; }

  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());

  priority_queue<ll> q;

  rep(i, x) {
    rep(j, y) {
      int tmp_z = min(z, K / (i + 1) / (j + 1));
      rep(l, tmp_z) { q.push(a[i] + b[j] + c[l]); }
    }
  }

  rep(i, K) {
    cout << q.top() << endl;
    q.pop();
  }

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int cmp(ll a, ll b) { return a > b; }
ll bc[1000001], ans[1000001];
int main() {
  int x, y, z, k, n = 0;
  ll a[1001], b[1001], c[1001];
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < y; i++)
    scanf("%lld", &b[i]);
  for (int i = 0; i < z; i++)
    scanf("%lld", &c[i]);
  sort(a, a + x, cmp);
  sort(b, b + y, cmp);
  sort(c, c + z, cmp);
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < z; j++) {
      bc[n] = b[i] + c[j];
      n++;
    }
  }
  sort(bc, bc + (y * z), cmp);
  n = 0;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < k; j++) {
      ans[n] = a[i] + bc[j];
      n++;
    }
  }
  sort(ans, ans + (x * k), cmp);
  for (int i = 0; i < k; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int cmp(ll a, ll b) { return a > b; }
ll bc[1000001], ans[3000001];
int main() {
  int x, y, z, k, n = 0;
  ll a[1001], b[1001], c[1001];
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < y; i++)
    scanf("%lld", &b[i]);
  for (int i = 0; i < z; i++)
    scanf("%lld", &c[i]);
  sort(a, a + x, cmp);
  sort(b, b + y, cmp);
  sort(c, c + z, cmp);
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < z; j++) {
      bc[n] = b[i] + c[j];
      n++;
    }
  }
  sort(bc, bc + (y * z), cmp);
  n = 0;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < k; j++) {
      ans[n] = a[i] + bc[j];
      n++;
    }
  }
  sort(ans, ans + (x * k), cmp);
  for (int i = 0; i < k; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

int main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<Int> 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];
  }

  vector<Int> d;
  for (int i = 0; i < x; i++) {
    for (int k = 0; k < y; k++) {
      d.push_back(a[i] + b[k]);
    }
  }
  sort(d.begin(), d.end(), greater<>());
  sort(c.begin(), c.end(), greater<>());

  vector<Int> ans;
  for (int i = 0; i < x * y; i++) {
    for (int k = 0; k < z; k++) {
      ans.push_back(d[i] + c[k]);
    }
  }

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

  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

int main() {
  int x, y, z, K;
  cin >> x >> y >> z >> K;
  vector<Int> 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];
  }

  vector<Int> d;
  for (int i = 0; i < x; i++) {
    for (int k = 0; k < y; k++) {
      d.push_back(a[i] + b[k]);
    }
  }
  sort(d.begin(), d.end(), greater<>());
  sort(c.begin(), c.end(), greater<>());

  vector<Int> ans;
  int ro = min(3000, x * y);
  for (int i = 0; i < ro; i++) {
    for (int k = 0; k < z; k++) {
      ans.push_back(d[i] + c[k]);
    }
  }

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

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map> // sort
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;

// abc123d cake
// 1,2,3のケーキをそれぞれ降順ソート
// 3つを比較して最も大きいものの次を参照
//  Kのmaxが3000だから
// X,Y,Zの使わない下半分くらいは取り除いていい?
// abc > K

int main() {
  int x, y, z, k;
  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.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());

  // a + b の結果
  vector<ll> vec;

  REP(i, x) {
    REP(j, y) { vec.push_back(a[i] + b[j]); }
  }
  vector<ll> res;
  sort(vec.begin(), vec.end(), greater<ll>());

  REP(i, vec.size()) {
    REP(j, z) { res.push_back(vec[i] + c[j]); }
  }
  sort(res.begin(), res.end(), greater<ll>());

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

  return 0;
}


Fixed submission:
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map> // sort
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;

// abc123d cake
// 1,2,3のケーキをそれぞれ降順ソート
// 3つを比較して最も大きいものの次を参照
//  Kのmaxが3000だから
// X,Y,Zの使わない下半分くらいは取り除いていい?
// abc > K

int main() {
  int x, y, z, k;
  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.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());

  // a + b の結果
  vector<ll> vec;

  REP(i, x) {
    REP(j, y) { vec.push_back(a[i] + b[j]); }
  }
  vector<ll> res;
  sort(vec.begin(), vec.end(), greater<ll>());

  int tmp = min(k + 1, (int)vec.size());

  REP(i, tmp) {
    REP(j, z) { res.push_back(vec[i] + c[j]); }
  }
  sort(res.begin(), res.end(), greater<ll>());

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

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define endl '\n'
#define ll long long
#define mod 1000000007
#define NCONS 1000000000
// #define NCONS 100001
// #define NCONS 1001
// #define NCONS 101
#define MCONS 2001
#define LIMIT 1000000000
#define TRUE 1
#define FALSE 0

using namespace std;

int main(void) {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x, 0), b(y, 0), c(z, 0);
  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];
  vector<ll> ab, ret;

  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>());

  while (ab.size() <= k) {
    ab.pop_back();
  }

  for (int i = 0; i < ab.size(); i++) {
    for (int j = 0; j < z; j++) {
      ret.push_back(ab[i] + c[j]);
    }
  }

  sort(ret.begin(), ret.end());

  while (k--) {
    cout << ret.back() << endl;
    ret.pop_back();
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define endl '\n'
#define ll long long
#define mod 1000000007
#define NCONS 1000000000
// #define NCONS 100001
// #define NCONS 1001
// #define NCONS 101
#define MCONS 2001
#define LIMIT 1000000000
#define TRUE 1
#define FALSE 0

using namespace std;

int main(void) {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x, 0), b(y, 0), c(z, 0);
  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];
  vector<ll> ab, ret;

  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>());

  while (ab.size() > k) {
    ab.pop_back();
  }

  for (int i = 0; i < ab.size(); i++) {
    for (int j = 0; j < z; j++) {
      ret.push_back(ab[i] + c[j]);
    }
  }

  sort(ret.begin(), ret.end());

  while (k--) {
    cout << ret.back() << endl;
    ret.pop_back();
  }

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long x, y, z, k;
long long cnt, cnt2;
long long a[1005], b[1005], c[1005], w[1000005], p[1000005];
bool cmp(long long x, long long y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> x >> y >> z >> k;
  for (long long i = 1; i <= x; ++i)
    cin >> a[i];
  for (long long i = 1; i <= y; ++i)
    cin >> b[i];
  for (long long i = 1; i <= z; ++i)
    cin >> c[i];
  for (long long i = 1; i <= x; ++i) {
    for (long long j = 1; j <= y; ++j) {
      w[++cnt] = a[i] + b[j];
    }
  }
  sort(w + 1, w + cnt + 1, cmp);
  for (long long i = 1; i <= min(k, cnt); ++i) {
    for (long long j = 1; j <= min(k, z); ++j) {
      p[++cnt2] = w[i] + c[j];
    }
  }
  sort(p + 1, p + cnt2 + 1, cmp);
  for (long long i = 1; i <= k; ++i)
    cout << p[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long x, y, z, k;
long long cnt, cnt2;
long long a[10005], b[10005], c[10005], w[10000005], p[10000005];
bool cmp(long long x, long long y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> x >> y >> z >> k;
  for (long long i = 1; i <= x; ++i)
    cin >> a[i];
  for (long long i = 1; i <= y; ++i)
    cin >> b[i];
  for (long long i = 1; i <= z; ++i)
    cin >> c[i];
  for (long long i = 1; i <= x; ++i) {
    for (long long j = 1; j <= y; ++j) {
      w[++cnt] = a[i] + b[j];
    }
  }
  sort(w + 1, w + cnt + 1, cmp);
  for (long long i = 1; i <= min(k, cnt); ++i) {
    for (long long j = 1; j <= min(k, z); ++j) {
      p[++cnt2] = w[i] + c[j];
    }
  }
  sort(p + 1, p + cnt2 + 1, cmp);
  for (long long i = 1; i <= k; ++i)
    cout << p[i] << endl;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// sort(v.rbegin(), v.rend(), [](auto& x, auto& y){return x[1] < y[1];});
// cout << fixed << setprecision(10) << ans << defaultfloat << endl;
typedef long long ll;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef vector<double> Vd;
typedef vector<string> Vs;

ll mod = 1e9 + 7;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  Vl A(X), B(Y), C(Z), tmp(X * Y), ans;
  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());
  REP(i, X) {
    REP(j, Y) { tmp[i * Y + j] = A[i] + B[j]; }
  }
  sort(tmp.rbegin(), tmp.rend());

  ll m = 0, count = 0;
  REP(i, Z) {
    REP(j, X * Y) {
      ll t = C[i] + tmp[j];
      if (count >= K && m > t) {
        break;
      }
      count++;
      m = min(t, m);
      ans.push_back(t);
    }
  }
  sort(ans.rbegin(), ans.rend());
  REP(i, K) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// sort(v.rbegin(), v.rend(), [](auto& x, auto& y){return x[1] < y[1];});
// cout << fixed << setprecision(10) << ans << defaultfloat << endl;
typedef long long ll;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef vector<double> Vd;
typedef vector<string> Vs;

ll mod = 1e9 + 7;

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  Vl A(X), B(Y), C(Z), tmp(X * Y), ans;
  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());
  REP(i, X) {
    REP(j, Y) { tmp[i * Y + j] = A[i] + B[j]; }
  }
  sort(tmp.rbegin(), tmp.rend());

  ll m = 0, count = 0;
  REP(i, Z) {
    REP(j, min(K, X * Y)) {
      ll t = C[i] + tmp[j];
      if (count >= K && m > t) {
        break;
      }
      count++;
      m = min(t, m);
      ans.push_back(t);
    }
  }
  sort(ans.rbegin(), ans.rend());
  REP(i, K) cout << ans[i] << endl;
}
Problem p03078 (C++)

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

using namespace std;

long a[3333], d[1111111];
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++) {
    long b;
    cin >> b;
    for (int j = 0; j < x; j++)
      d[i * x + j] = a[j] + b;
  }
  int l = min(k, x * y);
  nth_element(d, d + l, d + x * y, greater<long>());
  copy(d, d + l, a);
  for (int i = 0; i < z; i++) {
    long b;
    cin >> b;
    for (int j = 0; j < l; j++)
      d[i * l + j] = a[j] + b;
  }
  nth_element(d, d + k, d + l * z, greater<long>());
  sort(d, d + k, greater<long>());
  for (int i = 0; i < k; i++)
    cout << d[i] << '\n';
  cout << flush;
  return 0;
}


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

using namespace std;

long a[3333], d[3333333];
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++) {
    long b;
    cin >> b;
    for (int j = 0; j < x; j++)
      d[i * x + j] = a[j] + b;
  }
  int l = min(k, x * y);
  nth_element(d, d + l, d + x * y, greater<long>());
  copy(d, d + l, a);
  for (int i = 0; i < z; i++) {
    long b;
    cin >> b;
    for (int j = 0; j < l; j++)
      d[i * l + j] = a[j] + b;
  }
  nth_element(d, d + k, d + l * z, greater<long>());
  sort(d, d + k, greater<long>());
  for (int i = 0; i < k; i++)
    cout << d[i] << '\n';
  cout << flush;
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
int x, y, z, k;
ll a[2000], b[2000], c[2000];
priority_queue<pair<ll, ll>> temp;
priority_queue<ll> q;
int main() {
  cin >> x >> y >> z >> k;
  for (int i = 1; i <= x; i++)
    cin >> a[i];
  for (int i = 1; i <= y; i++)
    cin >> b[i];
  for (int i = 1; i <= z; i++)
    cin >> c[i];

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

  int A, B, C;
  A = 1;
  B = 1;
  C = 1;
  while (A * B * C <= 90000000) {
    if (A == x && B == y && C == z) {
      break;
    } else {
      if (A < x) {
        temp.push(make_pair(-1 * (a[A] - a[A + 1]), 1));
      }
      if (B < y) {
        temp.push(make_pair(-1 * (b[B] - b[B + 1]), 2));
      }
      if (C < z) {
        temp.push(make_pair(-1 * (c[C] - c[C + 1]), 3));
      }
    }
    ll top = temp.top().second;
    if (top == 1) {
      A++;
    } else if (top == 2) {
      B++;
    } else {
      C++;
    }
    while (!temp.empty())
      temp.pop();
  }
  for (int i = 1; i <= A; i++) {
    for (int j = 1; j <= B; j++) {
      for (int l = 1; l <= C; l++) {
        q.push(a[i] + b[j] + c[l]);
      }
    }
  }
  for (int i = 0; i < k; i++) {
    cout << q.top() << endl;
    q.pop();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
int x, y, z, k;
ll a[2000], b[2000], c[2000];
priority_queue<pair<ll, ll>> temp;
priority_queue<ll> q;
int main() {
  cin >> x >> y >> z >> k;
  for (int i = 1; i <= x; i++)
    cin >> a[i];
  for (int i = 1; i <= y; i++)
    cin >> b[i];
  for (int i = 1; i <= z; i++)
    cin >> c[i];

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

  int A, B, C;
  A = 1;
  B = 1;
  C = 1;
  while (A * B * C <= 10000000) {
    if (A == x && B == y && C == z) {
      break;
    } else {
      if (A < x) {
        temp.push(make_pair(-1 * (a[A] - a[A + 1]), 1));
      }
      if (B < y) {
        temp.push(make_pair(-1 * (b[B] - b[B + 1]), 2));
      }
      if (C < z) {
        temp.push(make_pair(-1 * (c[C] - c[C + 1]), 3));
      }
    }
    ll top = temp.top().second;
    if (top == 1) {
      A++;
    } else if (top == 2) {
      B++;
    } else {
      C++;
    }
    while (!temp.empty())
      temp.pop();
  }
  for (int i = 1; i <= A; i++) {
    for (int j = 1; j <= B; j++) {
      for (int l = 1; l <= C; l++) {
        q.push(a[i] + b[j] + c[l]);
      }
    }
  }
  for (int i = 0; i < k; i++) {
    cout << q.top() << endl;
    q.pop();
  }
}
Problem p03078 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll X, Y, Z, K;
ll A[1005];
ll B[1005];
ll C[1005];

vector<ll> v;

int main() {
  cin >> X >> Y >> Z >> K;
  REP(i, X) { cin >> A[i]; }
  REP(i, Y) { cin >> B[i]; }
  REP(i, Z) { cin >> C[i]; }
  sort(A, A + X, [](ll a, ll b) { return a > b; });
  sort(B, B + Y, [](ll a, ll b) { return a > b; });
  sort(C, C + Z, [](ll a, ll b) { return a > b; });

  ll m = INF;
  REP(a, X) {
    REP(b, Y) {
      REP(c, Z) {
        ll s = A[a] + B[b] + C[c];
        if (v.size() >= K && s <= m)
          break;
        m = std::min(m, s);
        v.push_back(s);
      }
    }
  }
  sort(v.begin(), v.end(), [](ll a, ll b) { return a > b; });
  REP(i, K) { cout << v[i] << endl; }
  return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll X, Y, Z, K;
ll A[1005];
ll B[1005];
ll C[1005];

vector<ll> v;

int main() {
  cin >> X >> Y >> Z >> K;
  REP(i, X) { cin >> A[i]; }
  REP(i, Y) { cin >> B[i]; }
  REP(i, Z) { cin >> C[i]; }
  sort(A, A + X, [](ll a, ll b) { return a > b; });
  sort(B, B + Y, [](ll a, ll b) { return a > b; });
  sort(C, C + Z, [](ll a, ll b) { return a > b; });

  vector<ll> vv;
  REP(i, X) {
    REP(j, Y) { vv.push_back(A[i] + B[j]); }
  }
  sort(vv.begin(), vv.end(), [](ll a, ll b) { return a > b; });
  REP(i, std::min(K, (ll)vv.size())) {
    REP(j, Z) { v.push_back(vv[i] + C[j]); }
  }
  sort(v.begin(), v.end(), [](ll a, ll b) { return a > b; });
  REP(i, K) { cout << v[i] << endl; }
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define P pair<int, int>
#define LL long long
#define LD long double
#define PLL pair<LL, LL>
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI acos(-1.0)
#define random(x) rand() % x
using namespace std;
const int inf = 0x3f3f3f3f;
const LL __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 5;
const int Mod = 1e9 + 7;

LL E[1005 * 1005];
LL D[1005 * 1005];
LL cnt;
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(A.rbegin(), A.rend());
  sort(B.rbegin(), B.rend());
  sort(C.rbegin(), C.rend());
  rep(i, x) { rep(j, y) D[cnt++] = A[i] + B[j]; }
  sort(D, D + cnt, greater<LL>());

  cnt = 0;
  rep(i, k) {
    rep(j, z) { E[cnt++] = D[i] + C[j]; }
  }
  sort(E, E + cnt, greater<LL>());
  rep(i, k) cout << E[i] << "\n";
  return 0;
};

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define P pair<int, int>
#define LL long long
#define LD long double
#define PLL pair<LL, LL>
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI acos(-1.0)
#define random(x) rand() % x
using namespace std;
const int inf = 0x3f3f3f3f;
const LL __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 5;
const int Mod = 1e9 + 7;

LL E[3005 * 1005];
LL D[1005 * 1005];
LL cnt;
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(A.rbegin(), A.rend());
  sort(B.rbegin(), B.rend());
  sort(C.rbegin(), C.rend());
  rep(i, x) { rep(j, y) D[cnt++] = A[i] + B[j]; }
  sort(D, D + cnt, greater<LL>());

  cnt = 0;
  rep(i, k) {
    rep(j, z) { E[cnt++] = D[i] + C[j]; }
  }
  sort(E, E + cnt, greater<LL>());
  rep(i, k) cout << E[i] << "\n";
  return 0;
};
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MOD 1000000007
int main() {
  fast;
  ll x, y, z, k, flag = 0;
  cin >> x >> y >> z >> k;
  vector<ll> a(x);
  vector<ll> b(y);
  vector<ll> 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(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());
  vector<ll> ans;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      for (int m = 0; m < z; m++) {
        ans.push_back(a[i] + b[j] + c[m]);
      }
    }
  }
  sort(ans.begin(), ans.end(), greater<ll>());
  for (int i = 0; i < k; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MOD 1000000007
int main() {
  fast;
  ll x, y, z, k, flag = 0;
  cin >> x >> y >> z >> k;
  vector<ll> a(x);
  vector<ll> b(y);
  vector<ll> 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(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());
  vector<ll> ans;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      for (int m = 0; m < z; m++) {
        if ((i + 1) * (j + 1) * (m + 1) <= k)
          ans.push_back(a[i] + b[j] + c[m]);
        else
          break;
      }
    }
  }
  sort(ans.begin(), ans.end(), greater<ll>());
  for (int i = 0; i < k; i++)
    cout << ans[i] << endl;
  return 0;
}