Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  {                                                                            \
    rep(_, v.size()) cout << " " << v[_];                                      \
    cout << endl;                                                              \
  }
#define show2d(v)                                                              \
  { rep(__, v.size()) show1d(v[__]); }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
#define yes                                                                    \
  cout << "YES"                                                                \
       << "\n"
#define no                                                                     \
  cout << "NO"                                                                 \
       << "\n"

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

  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vvi in(3);
  rep(i, x) {
    int a;
    cin >> a;
    in[0].pb(a);
  }
  rep(i, y) {
    int a;
    cin >> a;
    in[1].pb(a);
  }
  rep(i, z) {
    int a;
    cin >> a;
    in[2].pb(a);
  }
  vi t;
  rep(i, x) rep(j, y) t.pb(in[0][i] + in[1][j]);
  sort(all(t));
  vi nt;
  rep(i, min((ll)t.size(), k)) nt.pb(t[t.size() - 1 - i]);
  vi out;
  rep(i, t.size()) rep(j, z) out.pb(t[i] + in[2][j]);
  sort(all(out));
  rep(i, k) cout << out[out.size() - 1 - i] << endl;
}


Fixed submission:
// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  {                                                                            \
    rep(_, v.size()) cout << " " << v[_];                                      \
    cout << endl;                                                              \
  }
#define show2d(v)                                                              \
  { rep(__, v.size()) show1d(v[__]); }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
#define yes                                                                    \
  cout << "YES"                                                                \
       << "\n"
#define no                                                                     \
  cout << "NO"                                                                 \
       << "\n"

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

  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vvi in(3);
  rep(i, x) {
    int a;
    cin >> a;
    in[0].pb(a);
  }
  rep(i, y) {
    int a;
    cin >> a;
    in[1].pb(a);
  }
  rep(i, z) {
    int a;
    cin >> a;
    in[2].pb(a);
  }
  vi t;
  rep(i, x) rep(j, y) t.pb(in[0][i] + in[1][j]);
  sort(all(t));
  vi nt;
  rep(i, min((ll)t.size(), k)) nt.pb(t[t.size() - 1 - i]);
  vi out;
  rep(i, nt.size()) rep(j, z) out.pb(nt[i] + in[2][j]);
  sort(all(out));
  rep(i, k) cout << out[out.size() - 1 - i] << endl;
}