Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
inline char tc() {
  static char fl[100], *A = fl, *B = fl;
  return A == B && (B = (A = fl) + fread(fl, 1, 100, stdin), A == B) ? EOF
                                                                     : *A++;
}
inline int intread() {
  int a = 0;
  static char c;
  c = tc();
  while (c & 16)
    a = a * 10 + c - 48, c = tc();
  return a;
}
inline ll llread() {
  ll a = 0;
  static char c;
  c = tc();
  while (c & 16)
    a = a * 10 + c - 48, c = tc();
  return a;
}

static const int buf_size = 4096;
static int write_pos = 0;
static char write_buf[buf_size];

inline void writeChar(ll x) {
  if (write_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  write_buf[write_pos++] = x;
}
inline void write(ll x, char end) {
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}
struct Flusher {
  ~Flusher() {
    if (write_pos)
      fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  }
} flusher;

int X, Y, Z, K;
ll A[1000], B[1000], C[1000], ABC[10000];
int main() {
  X = intread(), Y = intread(), Z = intread(), K = intread();
  rep(i, X) A[i] = llread();
  rep(i, Y) B[i] = llread();
  rep(i, Z) C[i] = llread();
  sort(A, A + X, greater<ll>());
  sort(B, B + Y, greater<ll>());
  sort(C, C + Z, greater<ll>());
  int cnt = 0;
  rep(a, X) {
    rep(b, Y) {
      if ((a + 1) * (b + 1) > K)
        break;
      rep(c, Z) {
        if ((a + 1) * (b + 1) * (c + 1) > K)
          break;
        ABC[cnt++] = (A[a] + B[b] + C[c]);
      }
    }
  }
  sort(ABC, ABC + cnt, greater<ll>());
  rep(i, K) { write(ABC[i], '\n'); }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
inline char tc() {
  static char fl[100], *A = fl, *B = fl;
  return A == B && (B = (A = fl) + fread(fl, 1, 100, stdin), A == B) ? EOF
                                                                     : *A++;
}
inline int intread() {
  int a = 0;
  static char c;
  c = tc();
  while (c & 16)
    a = a * 10 + c - 48, c = tc();
  return a;
}
inline ll llread() {
  ll a = 0;
  static char c;
  c = tc();
  while (c & 16)
    a = a * 10 + c - 48, c = tc();
  return a;
}

static const int buf_size = 4096;
static int write_pos = 0;
static char write_buf[buf_size];

inline void writeChar(ll x) {
  if (write_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  write_buf[write_pos++] = x;
}
inline void write(ll x, char end) {
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}
struct Flusher {
  ~Flusher() {
    if (write_pos)
      fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  }
} flusher;

int X, Y, Z, K;
ll A[1000], B[1000], C[1000], ABC[1000000];
int main() {
  X = intread(), Y = intread(), Z = intread(), K = intread();
  rep(i, X) A[i] = llread();
  rep(i, Y) B[i] = llread();
  rep(i, Z) C[i] = llread();
  sort(A, A + X, greater<ll>());
  sort(B, B + Y, greater<ll>());
  sort(C, C + Z, greater<ll>());
  int cnt = 0;
  rep(a, X) {
    rep(b, Y) {
      if ((a + 1) * (b + 1) > K)
        break;
      rep(c, Z) {
        if ((a + 1) * (b + 1) * (c + 1) > K)
          break;
        ABC[cnt++] = (A[a] + B[b] + C[c]);
      }
    }
  }
  sort(ABC, ABC + cnt, greater<ll>());
  rep(i, K) { write(ABC[i], '\n'); }
  return 0;
}