Problem p03078 (C++)

Buggy submission (status: Runtime Error):
// ~/Remember,remember the 6th of March
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll MOD = 100000000;
const int N = 2e6 + 10;
void fast() { std::ios_base::sync_with_stdio(0); }
int main() {
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          //freopen("output.txt","w",stdout);
  #endif*/
  ll x, y, z, k;
  scanf("%lld%lld%lld%lld", &x, &y, &z, &k);
  vector<ll> arrx(x);
  for (ll &i : arrx)
    scanf("%lld", &i);
  vector<ll> arry(y);
  for (ll &i : arry)
    scanf("%lld", &i);
  vector<ll> arrz(z);
  for (ll &i : arrz)
    scanf("%lld", &i);
  sort(arrx.rbegin(), arrx.rend());
  sort(arry.rbegin(), arry.rend());
  sort(arrz.rbegin(), arrz.rend());
  vector<ll> ans;
  for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++)
      for (int k = 0; k < z; k++)
        ans.push_back(arrx[i] + arry[j] + arrz[k]);
  sort(ans.rbegin(), ans.rend());
  for (int i = 0; i < k; i++)
    printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
// ~/Remember,remember the 6th of March
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll MOD = 100000000;
const int N = 2e6 + 10;
void fast() { std::ios_base::sync_with_stdio(0); }
int main() {
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          //freopen("output.txt","w",stdout);
  #endif*/
  ll x, y, z, k;
  scanf("%lld%lld%lld%lld", &x, &y, &z, &k);
  vector<ll> arrx(x);
  for (ll &i : arrx)
    scanf("%lld", &i);
  vector<ll> arry(y);
  for (ll &i : arry)
    scanf("%lld", &i);
  vector<ll> arrz(z);
  for (ll &i : arrz)
    scanf("%lld", &i);
  sort(arrx.rbegin(), arrx.rend());
  sort(arry.rbegin(), arry.rend());
  sort(arrz.rbegin(), arrz.rend());
  vector<ll> ans;
  for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++)
      for (int kk = 0; kk < z; kk++)
        if ((i + 1) * (j + 1) * (kk + 1) <= k)
          ans.push_back(arrx[i] + arry[j] + arrz[kk]);
        else
          break;
  sort(ans.rbegin(), ans.rend());
  for (int i = 0; i < k; i++)
    printf("%lld\n", ans[i]);
  return 0;
}