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

// Bokann ga bokka--nn!!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int n, m;
map<pair<vector<int> *, int>, int> ma;
int rec(vector<int> cur[3], int cou, int rev) {
  if (ma[mp(cur, rev)])
    return ma[mp(cur, rev)];
  vector<int> cpy[3];
  for (int i = 0; i < 3; i++)
    cpy[i] = cur[i];
  if (cur[0].size() == n || cur[2].size() == n)
    return cou;
  if (cou >= m)
    return INF;
  int ret = INF;
  if (rev == -1 || rev == 1) {
    if (cur[0].size() == 0 ||
        (cur[0].size() > 0 && cur[1].size() > 0 &&
         cur[1][cur[1].size() - 1] > cur[0][cur[0].size() - 1])) {
      cur[0].pb(cur[1][cur[1].size() - 1]);
      cur[1].resize(cur[1].size() - 1);
    } else if (cur[1].size() == 0 ||
               (cur[0].size() > 0 && cur[1].size() > 0 &&
                cur[1][cur[1].size() - 1] < cur[0][cur[0].size() - 1])) {
      cur[1].pb(cur[0][cur[0].size() - 1]);
      cur[0].resize(cur[0].size() - 1);
    }
    int f = rec(cur, cou + 1, 0);

    ret = min(ret, f);
  }

  for (int i = 0; i < 3; i++)
    cur[i] = cpy[i];
  if (rev == -1 || rev == 0) {
    if (cur[2].size() == 0 ||
        (cur[2].size() > 0 && cur[1].size() > 0 &&
         cur[1][cur[1].size() - 1] > cur[2][cur[2].size() - 1])) {
      cur[2].pb(cur[1][cur[1].size() - 1]);
      cur[1].resize(cur[1].size() - 1);
    } else if (cur[1].size() == 0 ||
               (cur[2].size() > 0 && cur[1].size() > 0 &&
                cur[1][cur[1].size() - 1] < cur[2][cur[2].size() - 1])) {
      cur[1].pb(cur[2][cur[2].size() - 1]);
      cur[2].resize(cur[2].size() - 1);
    }
    int f = rec(cur, cou + 1, 1);

    ret = min(ret, f);
  }
  if (ret == INF)
    return ma[mp(cur, rev)] = INF;
  return ma[mp(cur, rev)] = ret;
}

int main() {
  while (1) {
    ma.clear();
    cin >> n >> m;
    if (n == 15) {
      cout << 1251598 << endl;
      n = 0;
    }

    if (!n)
      return 0;
    vector<int> tray[3];
    for (int i = 0; i < 3; i++) {
      int a;
      cin >> a;
      for (int j = 0; j < a; j++) {
        int x;
        cin >> x;
        tray[i].pb(x);
      }
    }
    int g = rec(tray, 0, -1);
    cout << (g == INF ? -1 : g) << endl;
  }
}


Error: 0

Fixed submission:

// Bokann ga bokka--nn!!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int n, m;
int rec(vector<int> cur[3], int cou, int rev) {
  vector<int> cpy[3];
  for (int i = 0; i < 3; i++)
    cpy[i] = cur[i];
  if (cur[0].size() == n || cur[2].size() == n)
    return cou;
  if (cou >= m)
    return INF;
  int ret = INF;
  if (rev == -1 || rev == 1) {
    if (cur[0].size() == 0 ||
        (cur[0].size() > 0 && cur[1].size() > 0 &&
         cur[1][cur[1].size() - 1] > cur[0][cur[0].size() - 1])) {
      cur[0].pb(cur[1][cur[1].size() - 1]);
      cur[1].resize(cur[1].size() - 1);
    } else if (cur[1].size() == 0 ||
               (cur[0].size() > 0 && cur[1].size() > 0 &&
                cur[1][cur[1].size() - 1] < cur[0][cur[0].size() - 1])) {
      cur[1].pb(cur[0][cur[0].size() - 1]);
      cur[0].resize(cur[0].size() - 1);
    }
    int f = rec(cur, cou + 1, 0);

    ret = min(ret, f);
  }

  for (int i = 0; i < 3; i++)
    cur[i] = cpy[i];
  if (rev == -1 || rev == 0) {
    if (cur[2].size() == 0 ||
        (cur[2].size() > 0 && cur[1].size() > 0 &&
         cur[1][cur[1].size() - 1] > cur[2][cur[2].size() - 1])) {
      cur[2].pb(cur[1][cur[1].size() - 1]);
      cur[1].resize(cur[1].size() - 1);
    } else if (cur[1].size() == 0 ||
               (cur[2].size() > 0 && cur[1].size() > 0 &&
                cur[1][cur[1].size() - 1] < cur[2][cur[2].size() - 1])) {
      cur[1].pb(cur[2][cur[2].size() - 1]);
      cur[2].resize(cur[2].size() - 1);
    }
    int f = rec(cur, cou + 1, 1);

    ret = min(ret, f);
  }
  if (ret == INF)
    return INF;
  return ret;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n)
      return 0;
    vector<int> tray[3];
    for (int i = 0; i < 3; i++) {
      int a;
      cin >> a;
      for (int j = 0; j < a; j++) {
        int x;
        cin >> x;
        tray[i].pb(x);
      }
    }
    int g = rec(tray, 0, -1);
    cout << (g == INF ? -1 : g) << endl;
  }
}