Problem p03078 (C++)

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf64 = vector<f64>;
using vstr = vector<string>;

template <typename T, typename S> void amax(T &x, S y) {
  if (x < y)
    x = y;
}
template <typename T, typename S> void amin(T &x, S y) {
  if (y < x)
    x = y;
}

class cake {
public:
  i64 sum;
  i32 a, b, c;
  bool operator<(const cake &that) const {
    if (sum == that.sum) {
      if (a == that.a) {
        if (b == that.b) {
          return c < that.c;
        } else {
          return b < that.b;
        }
      } else {
        return a < that.a;
      }
    } else {
      return sum < that.sum;
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  i32 x, y, z, k;
  cin >> x >> y >> z >> k;
  vi64 a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  sort(all(a), greater<i64>());
  sort(all(b), greater<i64>());
  sort(all(c), greater<i64>());
  priority_queue<cake> que;
  set<cake> s;
  que.push({a[0] + b[0] + c[0], 0, 0, 0});
  rep(_, k) {
    auto q = que.top();
    que.pop();
    if (s.find(q) == s.end()) {
      cout << q.sum << endl;
      s.insert(q);
    } else {
      _--;
    }
    if (q.a < x - 1) {
      que.push({a[q.a + 1] + b[q.b] + c[q.c], q.a + 1, q.b, q.c});
    }
    if (q.b < y - 1) {
      que.push({a[q.a] + b[q.b + 1] + c[q.c], q.a, q.b + 1, q.c});
    }
    if (q.c < z - 1) {
      que.push({a[q.a] + b[q.b] + c[q.c + 1], q.a, q.b, q.c + 1});
    }
  }
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf64 = vector<f64>;
using vstr = vector<string>;

template <typename T, typename S> void amax(T &x, S y) {
  if (x < y)
    x = y;
}
template <typename T, typename S> void amin(T &x, S y) {
  if (y < x)
    x = y;
}

class cake {
public:
  i64 sum;
  i32 a, b, c;
  bool operator<(const cake &that) const {
    if (sum == that.sum) {
      if (a == that.a) {
        if (b == that.b) {
          return c < that.c;
        } else {
          return b < that.b;
        }
      } else {
        return a < that.a;
      }
    } else {
      return sum < that.sum;
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  i32 x, y, z, k;
  cin >> x >> y >> z >> k;
  vi64 a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];
  sort(all(a), greater<i64>());
  sort(all(b), greater<i64>());
  sort(all(c), greater<i64>());
  priority_queue<cake> que;
  set<cake> s;
  que.push({a[0] + b[0] + c[0], 0, 0, 0});
  rep(_, k) {
    auto q = que.top();
    que.pop();
    if (s.find(q) == s.end()) {
      cout << q.sum << endl;
      s.insert(q);
    } else {
      _--;
      continue;
    }
    if (q.a < x - 1) {
      que.push({a[q.a + 1] + b[q.b] + c[q.c], q.a + 1, q.b, q.c});
    }
    if (q.b < y - 1) {
      que.push({a[q.a] + b[q.b + 1] + c[q.c], q.a, q.b + 1, q.c});
    }
    if (q.c < z - 1) {
      que.push({a[q.a] + b[q.b] + c[q.c + 1], q.a, q.b, q.c + 1});
    }
  }
  return 0;
}