Problem p00426 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>

int c[16];
int n, m, i, j, k, x;

int main(void) {
  for (; scanf("%d%d", &n, &m), n; printf("%d\\n", x > m ? -1 : x)) {
    for (i = 3; i--;)
      for (scanf("%d", &j); j--; c[n - k] = i)
        scanf("%d", &k);
    x = 0;
    for (i = n; i--; k += c[i])
      x += pow(3, i) * abs(k * 2 % 4 - c[i]);
    x = fmin(x, pow(3, n) - 1 - x);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
struct st {
  vector<int> a, b, c;
  int num;
};
int tmp;
int ans;
bool flag = false;
queue<st> que;
int main() {
  while (1) {
    flag = false;
    while (!(que.empty())) {
      que.pop();
    }
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    st s;
    for (int i = 0; i < 3; i++) {
      int p;
      cin >> p;
      for (int j = 0; j < p; j++) {
        if (i == 0) {
          int q;
          cin >> q;
          s.a.pb(q);
        } else if (i == 1) {
          int q;
          cin >> q;
          s.b.pb(q);
        } else {
          int q;
          cin >> q;
          s.c.pb(q);
        }
      }
    }
    s.num = 0;
    que.pu(s);
    while (!(que.empty())) {
      st e = que.front();
      que.pop();
      if (e.a.size() == n || e.c.size() == n) {
        ans = e.num;
        break;
      }
      if (e.num > m) {
        flag = true;
        break;
      }
      if (((e.b.size() == 0) && (e.a.size() != 0)) ||
          ((e.b.size() != 0) && (e.a.size() != 0) &&
           (e.a.back() > e.b.back()))) {
        st now;
        now = e;
        tmp = now.a.back();
        now.a.pop_back();
        now.b.pb(tmp);
        now.num++;
        que.pu(now);
      }
      if (((e.a.size() == 0) && (e.b.size() != 0)) ||
          ((e.a.size() != 0) && (e.b.size() != 0) &&
           (e.a.back() < e.b.back()))) {
        st now;
        now = e;
        tmp = now.b.back();
        now.b.pop_back();
        now.a.pb(tmp);
        now.num++;
        que.pu(now);
      }
      if (((e.c.size() == 0) && (e.b.size() != 0)) ||
          ((e.b.size() != 0) && (e.c.size() != 0) &&
           (e.b.back() > e.c.back()))) {
        st now;
        now = e;
        tmp = now.b.back();
        now.b.pop_back();
        now.c.pb(tmp);
        now.num++;
        que.pu(now);
      }
      if (((e.b.size() == 0) && (e.c.size() != 0)) ||
          ((e.b.size() != 0) && (e.c.size() != 0) &&
           (e.b.back() < e.c.back()))) {
        st now;
        now = e;
        tmp = now.c.back();
        now.c.pop_back();
        now.b.pb(tmp);
        now.num++;
        que.pu(now);
      }
    }
    if (flag) {
      cout << -1 << endl;
      continue;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}