Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
ld EPS = 1e-12;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << '\n'

bool dp[1000][1000][1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> 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(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());
  a.push_back(-1 * LINF);
  b.push_back(-1 * LINF);
  c.push_back(-1 * LINF);
  priority_queue<pair<ll, pair<ll, pair<ll, ll>>>> q;
  ll ma = a[0] + b[0] + c[0];
  q.push(make_pair(ma, make_pair(0, make_pair(0, 0))));
  dp[0][0][0] = true;
  for (ll i = 0; i < k; i++) {
    auto p = q.top();
    q.pop();
    ll res = p.first;
    cout << res << endl;
    ll ai = p.second.first;
    ll bi = p.second.second.first;
    ll ci = p.second.second.second;

    ll nexta = a[ai + 1] + b[bi] + c[ci];
    if (!dp[ai + 1][bi][ci]) {
      q.push(make_pair(nexta, make_pair(ai + 1, make_pair(bi, ci))));
      dp[ai + 1][bi][ci] = true;
    }

    ll nextb = a[ai] + b[bi + 1] + c[ci];
    if (!dp[ai][bi + 1][ci]) {
      q.push(make_pair(nextb, make_pair(ai, make_pair(bi + 1, ci))));
      dp[ai][bi + 1][ci] = true;
    }

    ll nextc = a[ai] + b[bi] + c[ci + 1];
    if (!dp[ai][bi][ci + 1]) {
      q.push(make_pair(nextc, make_pair(ai, make_pair(bi, ci + 1))));
      dp[ai][bi][ci + 1] = true;
    }
  }
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
ld EPS = 1e-12;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << '\n'

bool dp[1005][1005][1005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> 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(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());
  a.push_back(-1 * LINF);
  b.push_back(-1 * LINF);
  c.push_back(-1 * LINF);
  priority_queue<pair<ll, pair<ll, pair<ll, ll>>>> q;
  ll ma = a[0] + b[0] + c[0];
  q.push(make_pair(ma, make_pair(0, make_pair(0, 0))));
  dp[0][0][0] = true;
  for (ll i = 0; i < k; i++) {
    auto p = q.top();
    q.pop();
    ll res = p.first;
    cout << res << endl;
    ll ai = p.second.first;
    ll bi = p.second.second.first;
    ll ci = p.second.second.second;

    ll nexta = a[ai + 1] + b[bi] + c[ci];
    if (!dp[ai + 1][bi][ci]) {
      q.push(make_pair(nexta, make_pair(ai + 1, make_pair(bi, ci))));
      dp[ai + 1][bi][ci] = true;
    }

    ll nextb = a[ai] + b[bi + 1] + c[ci];
    if (!dp[ai][bi + 1][ci]) {
      q.push(make_pair(nextb, make_pair(ai, make_pair(bi + 1, ci))));
      dp[ai][bi + 1][ci] = true;
    }

    ll nextc = a[ai] + b[bi] + c[ci + 1];
    if (!dp[ai][bi][ci + 1]) {
      q.push(make_pair(nextc, make_pair(ai, make_pair(bi, ci + 1))));
      dp[ai][bi][ci + 1] = true;
    }
  }
}