Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
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.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  int pa = x - 1, pb = y - 1, pc = z - 1, ca = 1, cb = 1, cc = 1;
  while (ca * cb * cc < k) {
    if (pa >= 1 && pb >= 1 && pc >= 1) {
      if (a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb - 1] + c[pc] &&
          a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pa--, ca++;
      else if (a[pa] + b[pb - 1] + c[pc] >= a[pa - 1] + b[pb] + c[pc] &&
               a[pa] + b[pb - 1] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pb--, cb++;
      else if (a[pa] + b[pb] + c[pc - 1] >= a[pa - 1] + b[pb] + c[pc] &&
               a[pa] + b[pb] + c[pc - 1] >= a[pa] + b[pb - 1] + c[pc])
        pc--, cc++;
    } else if (pa >= 1 && pb >= 1) {
      if (a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb - 1] + c[pc])
        pa--, ca++;
      else
        pb--, cb++;
    } else if (pb >= 1 && pc >= 1) {
      if (a[pa] + b[pb - 1] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pb--, cb++;
      else
        pc--, cc++;
    } else if (pc >= 1 && pa >= 1) {
      if (a[pa] + b[pb] + c[pc - 1] >= a[pa - 1] + b[pb] + c[pc])
        pc--, cc++;
      else
        pa--, ca++;
    } else if (pa >= 1) {
      pa--, ca++;
    } else if (pb >= 1) {
      pb--, cb++;
    } else if (pc >= 1) {
      pc--, cc++;
    }
  }
  ca = min(x, ca + 150);
  cb = min(y, cb + 150);
  cc = min(z, cc + 150);
  vector<ll> sum;
  for (ll i = 1; i <= ca; i++) {
    for (ll j = 1; j <= cb; j++) {
      for (ll k = 1; k <= cc; k++) {
        sum.push_back(a[x - i] + b[y - j] + c[z - k]);
      }
    }
  }
  sort(sum.begin(), sum.end());
  ll cnt = 0;
  for (ll i = sum.size() - 1;; i--) {
    cout << sum[i] << endl;
    cnt++;
    if (cnt == k)
      break;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
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.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  int pa = x - 1, pb = y - 1, pc = z - 1, ca = 1, cb = 1, cc = 1;
  while (ca * cb * cc < k) {
    if (pa >= 1 && pb >= 1 && pc >= 1) {
      if (a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb - 1] + c[pc] &&
          a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pa--, ca++;
      else if (a[pa] + b[pb - 1] + c[pc] >= a[pa - 1] + b[pb] + c[pc] &&
               a[pa] + b[pb - 1] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pb--, cb++;
      else if (a[pa] + b[pb] + c[pc - 1] >= a[pa - 1] + b[pb] + c[pc] &&
               a[pa] + b[pb] + c[pc - 1] >= a[pa] + b[pb - 1] + c[pc])
        pc--, cc++;
    } else if (pa >= 1 && pb >= 1) {
      if (a[pa - 1] + b[pb] + c[pc] >= a[pa] + b[pb - 1] + c[pc])
        pa--, ca++;
      else
        pb--, cb++;
    } else if (pb >= 1 && pc >= 1) {
      if (a[pa] + b[pb - 1] + c[pc] >= a[pa] + b[pb] + c[pc - 1])
        pb--, cb++;
      else
        pc--, cc++;
    } else if (pc >= 1 && pa >= 1) {
      if (a[pa] + b[pb] + c[pc - 1] >= a[pa - 1] + b[pb] + c[pc])
        pc--, cc++;
      else
        pa--, ca++;
    } else if (pa >= 1) {
      pa--, ca++;
    } else if (pb >= 1) {
      pb--, cb++;
    } else if (pc >= 1) {
      pc--, cc++;
    }
  }
  ca = min(x, ca + 100);
  cb = min(y, cb + 100);
  cc = min(z, cc + 100);
  vector<ll> sum;
  for (ll i = 1; i <= ca; i++) {
    for (ll j = 1; j <= cb; j++) {
      for (ll k = 1; k <= cc; k++) {
        sum.push_back(a[x - i] + b[y - j] + c[z - k]);
      }
    }
  }
  sort(sum.begin(), sum.end());
  ll cnt = 0;
  for (ll i = sum.size() - 1;; i--) {
    cout << sum[i] << endl;
    cnt++;
    if (cnt == k)
      break;
  }
  return 0;
}