Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T &a) {
  int t = 0;
  for (auto itr : a) {
    if (t++)
      cout << " ";
    cout << itr;
  }
  cout << endl;
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
signed main() {
  //    std::ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<int> vx(x), vy(y), vz(z);
  for (auto &e : vx)
    cin >> e;
  for (auto &e : vy)
    cin >> e;
  for (auto &e : vz)
    cin >> e;
  sort(all(vx), greater<>());
  sort(all(vy), greater<>());
  sort(all(vz), greater<>());
  //    vector<int>nx,ny,nz;
  //    nx.push_back(vx.front());
  //    ny.push_back(vy.front());
  //    nz.push_back(vz.front());

  //    priority_queue<int,vector<int>>xque,yque,zque;
  //    rep(i,x)if(i)xque.push(vx[i]);
  //    rep(i,y)if(i)yque.push(vy[i]);
  //    rep(i,z)if(i)zque.push(vz[i]);
  //    xque.push(-10000000000);
  //    yque.push(-10000000000);
  //    zque.push(-10000000000);
  //
  //    while((int)nx.size() * (int)ny.size() * (int)nz.size() < k){
  //        int xdif = nx.back() - xque.top();
  //        int ydif = ny.back() - yque.top();
  //        int zdif = nz.back() - zque.top();
  //        if(xdif <= ydif and xdif <= zdif and !xque.empty()){
  //            nx.push_back(xque.top());
  //            xque.pop();
  //        } else if(ydif <= xdif and ydif <= zdif and !yque.empty()){
  //            ny.push_back(yque.top());
  //            yque.pop();
  //        } else {
  //            nz.push_back(zque.top());
  //            zque.pop();
  //        }
  //    }
  vector<int> ans;
  //    rep(i,nx.size())rep(j,ny.size())rep(k,nz.size()){
  //        ans.push_back(nx[i] + ny[j] + nz[k]);
  //    }
  vector<int> sum;
  rep(i, vy.size()) rep(j, vz.size()) sum.push_back(vy[i] + vz[j]);
  sort(all(sum));
  rep(i, vx.size()) rep(j, (int)sum.size()) ans.push_back(vx[i] + sum[j]);
  sort(all(ans), greater<>());
  rep(i, k) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T &a) {
  int t = 0;
  for (auto itr : a) {
    if (t++)
      cout << " ";
    cout << itr;
  }
  cout << endl;
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
signed main() {
  //    std::ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<int> vx(x), vy(y), vz(z);
  for (auto &e : vx)
    cin >> e;
  for (auto &e : vy)
    cin >> e;
  for (auto &e : vz)
    cin >> e;
  sort(all(vx), greater<>());
  sort(all(vy), greater<>());
  sort(all(vz), greater<>());
  //    vector<int>nx,ny,nz;
  //    nx.push_back(vx.front());
  //    ny.push_back(vy.front());
  //    nz.push_back(vz.front());

  //    priority_queue<int,vector<int>>xque,yque,zque;
  //    rep(i,x)if(i)xque.push(vx[i]);
  //    rep(i,y)if(i)yque.push(vy[i]);
  //    rep(i,z)if(i)zque.push(vz[i]);
  //    xque.push(-10000000000);
  //    yque.push(-10000000000);
  //    zque.push(-10000000000);
  //
  //    while((int)nx.size() * (int)ny.size() * (int)nz.size() < k){
  //        int xdif = nx.back() - xque.top();
  //        int ydif = ny.back() - yque.top();
  //        int zdif = nz.back() - zque.top();
  //        if(xdif <= ydif and xdif <= zdif and !xque.empty()){
  //            nx.push_back(xque.top());
  //            xque.pop();
  //        } else if(ydif <= xdif and ydif <= zdif and !yque.empty()){
  //            ny.push_back(yque.top());
  //            yque.pop();
  //        } else {
  //            nz.push_back(zque.top());
  //            zque.pop();
  //        }
  //    }
  vector<int> ans;
  //    rep(i,nx.size())rep(j,ny.size())rep(k,nz.size()){
  //        ans.push_back(nx[i] + ny[j] + nz[k]);
  //    }
  vector<int> sum;
  rep(i, vy.size()) rep(j, vz.size()) sum.push_back(vy[i] + vz[j]);
  sort(all(sum), greater<>());
  rep(i, vx.size()) rep(j, min(3000LL, (int)sum.size()))
      ans.push_back(vx[i] + sum[j]);
  sort(all(ans), greater<>());
  rep(i, k) cout << ans[i] << endl;
}