Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())
// vvintを作る　マクロで　 #define vvint(N,M) vector<vector<int>>
int MIN(int a, int b) { return a < b ? a : b; }

using namespace std;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  ll xyz[3], K;
  cin >> xyz[0] >> xyz[1] >> xyz[2] >> K;
  vector<ll> A(xyz[0], 0), B(xyz[1], 0), C(xyz[2], 0);
  ll tmp;
  for (int i = 0; i < xyz[0]; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < xyz[1]; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < xyz[2]; i++) {
    cin >> C[i];
  }
  vector<ll> ans(K), AB(xyz[0] * xyz[1]);
  ll wa = 0;

  for (int i = 0; i < xyz[0]; i++) {
    for (int j = 0; j < xyz[1]; j++) {
      AB[wa] = A[i] + B[j];
      wa++;
    }
  }
  VSORT(AB);
  reverse(ALL(AB));
  wa = 0;
  for (int i = 0; i < MIN(K, xyz[0] * xyz[1]); i++) {
    for (int j = 0; j < xyz[2]; j++) {
      ans[wa] = AB[i] + C[j];
      wa++;
    }
  }
  VSORT(ans);
  reverse(ALL(ans));

  for (int i = 0; i < K; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())
// vvintを作る　マクロで　 #define vvint(N,M) vector<vector<int>>
int MIN(int a, int b) { return a < b ? a : b; }

using namespace std;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  ll xyz[3], K;
  cin >> xyz[0] >> xyz[1] >> xyz[2] >> K;
  vector<ll> A(xyz[0], 0), B(xyz[1], 0), C(xyz[2], 0);
  ll tmp;
  for (int i = 0; i < xyz[0]; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < xyz[1]; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < xyz[2]; i++) {
    cin >> C[i];
  }
  vector<ll> ans(MIN(K, xyz[0] * xyz[1]) * xyz[2], 0), AB(xyz[0] * xyz[1], 0);
  ll wa = 0;

  for (int i = 0; i < xyz[0]; i++) {
    for (int j = 0; j < xyz[1]; j++) {
      AB[wa] = A[i] + B[j];
      wa++;
    }
  }
  VSORT(AB);
  reverse(ALL(AB));
  wa = 0;
  for (int i = 0; i < MIN(K, xyz[0] * xyz[1]); i++) {
    for (int j = 0; j < xyz[2]; j++) {
      ans[wa] = AB[i] + C[j];
      wa++;
    }
  }
  VSORT(ans);
  reverse(ALL(ans));

  for (int i = 0; i < K; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}