Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  multiset<ll> st;
  rep(i, x) {
    rep(j, y) { st.insert(a[i] + b[j]); }
    if (st.size() > k) {
      st.erase(st.begin());
    }
  }
  multiset<ll> st2;
  rep(i, z) {
    for (auto x : st) {
      st2.insert(x + c[i]);
      if (st2.size() > k) {
        st2.erase(st2.begin());
      }
    }
  }
  auto it = st2.end();
  rep(i, st2.size()) {
    it--;
    cout << *it << "\n";
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  multiset<ll> st;
  rep(i, x) {
    rep(j, y) {
      st.insert(a[i] + b[j]);
      if (st.size() > k) {
        st.erase(st.begin());
      }
    }
  }
  multiset<ll> st2;
  rep(i, z) {
    for (auto x : st) {
      st2.insert(x + c[i]);
      if (st2.size() > k) {
        st2.erase(st2.begin());
      }
    }
  }
  auto it = st2.end();
  rep(i, st2.size()) {
    it--;
    cout << *it << "\n";
  }
}