Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long f(vector<vector<long long>> cake, vector<int> pos) {
  long long sum = 0;
  for (int i = 0; i < 3; i++) {
    sum += cake[i][pos[i]];
  }
  return sum;
}

bool fflag(set<vector<int>> flag, vector<int> pos) {
  if (flag.find(pos) == flag.end()) {
    return false;
  }
  return true;
}
int main() {
  vector<int> size(3);
  for (int i = 0; i < 3; i++) {
    cin >> size[i];
  }

  set<vector<int>> flag;

  int K;
  cin >> K;
  vector<vector<long long>> cake;
  cake.resize(3);
  for (int i = 0; i < 3; i++) {
    cake[i].resize(size[i]);
    for (int j = 0; j < size[i]; j++) {
      cin >> cake[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    sort(cake[i].begin(), cake[i].end(), greater<long long>());
  }
  priority_queue<pair<long long, vector<int>>> q;
  vector<int> initial{0, 0, 0};
  q.push(make_pair(f(cake, initial), initial));
  flag.insert(initial);
  for (int k = 0; k < K; k++) {

    cout << q.top().first << endl;
    vector<int> list = q.top().second;

    q.pop();
    if (k == K - 1) {
      break;
    }

    vector<vector<int>> next(3);
    next[0] = {list[0] + 1, list[1], list[2]};
    next[1] = {list[0], list[1] + 1, list[2]};
    next[2] = {list[0], list[1], list[2] + 1};

    for (int i = 0; i < 3; i++) {
      if (list[i] + 1 >= size[i] || flag.find(next[i]) != flag.end() ||
          *flag.end() == next[i]) {
        continue;
      }
      q.push(make_pair(f(cake, next[i]), next[i]));
      flag.insert(next[i]);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long f(vector<vector<long long>> cake, vector<int> pos) {
  long long sum = 0;
  for (int i = 0; i < 3; i++) {
    sum += cake[i][pos[i]];
  }
  return sum;
}

bool fflag(set<vector<int>> flag, vector<int> pos) {
  if (flag.find(pos) == flag.end()) {
    return false;
  }
  return true;
}
int main() {
  vector<int> size(3);
  for (int i = 0; i < 3; i++) {
    cin >> size[i];
  }

  set<vector<int>> flag;

  int K;
  cin >> K;
  vector<vector<long long>> cake;
  cake.resize(3);
  for (int i = 0; i < 3; i++) {
    cake[i].resize(size[i]);
    for (int j = 0; j < size[i]; j++) {
      cin >> cake[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    sort(cake[i].begin(), cake[i].end(), greater<long long>());
  }
  priority_queue<pair<long long, vector<int>>> q;
  vector<int> initial{0, 0, 0};
  q.push(make_pair(f(cake, initial), initial));
  flag.insert(initial);
  for (int k = 0; k < K; k++) {

    cout << q.top().first << endl;
    vector<int> list = q.top().second;

    q.pop();
    if (k == K - 1) {
      break;
    }

    vector<vector<int>> next(3);
    next[0] = {list[0] + 1, list[1], list[2]};
    next[1] = {list[0], list[1] + 1, list[2]};
    next[2] = {list[0], list[1], list[2] + 1};

    for (int i = 0; i < 3; i++) {
      if (list[i] + 1 >= size[i] || flag.find(next[i]) != flag.end()) {
        continue;
      }
      q.push(make_pair(f(cake, next[i]), next[i]));
      flag.insert(next[i]);
    }
  }
  return 0;
}