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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP(x, y) make_pair(x, y)
#define ARRAY 65536
#define X first
#define Y second
#define DBG 0
using namespace std;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

class Dice {
  //    1  /  rX     4  /
  //  ___/    ->  _____/
  //  2  | 3       2  |  1
  //  |
  //  v rY
  //
  //   0
  //  1   2
public:
  int value[6];
  int id[6];
  Dice() {
    rep(i, 6) id[i] = i;
    rep(i, 6) value[i] = i + 1;
  }
  int &operator[](int x) { return value[id[x]]; }
  void swp(int a, int b, int c, int d) {
    int tmp = id[a];
    id[a] = id[b];
    id[b] = id[c];
    id[c] = id[d];
    id[d] = tmp;
  }
  void rX() { swp(0, 3, 5, 2); }
  void revX() {
    rX();
    rX();
    rX();
  }
  void rY() { swp(0, 4, 5, 1); }
  void revY() {
    rY();
    rY();
    rY();
  }
  void print() {
    rep(i, 6) cout << value[id[i]] << ",";
    cout << endl;
  }
};

class Stump {
public:
  int x, y, value;
  Stump(int _x, int _y, int _value) : x(_x), y(_y), value(_value){};
  void print() { cout << "(  " << x << " , " << y << " ) " << value << endl; }
};
class iStump {
public:
  int id, val;
  iStump(int _id, int _val) : id(_id), val(_val){};
};

// int mpl[2200][2200] ={0};

// ??§?¨????id 31*15?????§
map<pair<int, int>, int> memo;

int main(void) {
  int n;
  int ca = 0;
  while (cin >> n, n) {
    vector<char> paths[ARRAY]; //[state]?????¨???????????????????????????
    int dp[ARRAY] = {0};       //[state]?????¨???????????§???
    int xy[1024][16] = {
        0}; //[??§?¨?id]???[????????????id] ??? ??????????????????
    int x, y, v;
    int ans = 0;
    int used_xy = 0;
    memo.clear();

    string r;
    vector<Stump> ts[32];
    vector<iStump> s[32];

    rep(i, n) {
      Dice dice;
      cin >> x >> y;
      x += 1050;
      y += 1050;
      cin >> dice[3] >> dice[2] >> dice[1] >> dice[4] >> dice[5] >> dice[0];
      ts[i].push_back(Stump(x, y, dice[5]));
      if (DBG)
        ts[i].back().print();

      cin >> r;
      rep(j, r.size()) {
        if (r[j] == 'L') {
          dice.revX();
          x--;
        } else if (r[j] == 'R') {
          dice.rX();
          x++;
        } else if (r[j] == 'F') {
          dice.rY();
          y--;
        } else {
          dice.revY();
          y++;
        }
        ts[i].push_back(Stump(x, y, dice[5]));
        if (DBG)
          ts[i].back().print();
      }

      map<pair<int, int>, int> used;
      for (int k = ts[i].size() - 1; k >= 0; --k) {
        pair<int, int> xyz = MP(ts[i][k].x, +ts[i][k].y);
        if (used[xyz] != 0)
          ts[i][k].value = 0;
        if (memo[xyz] == 0) {
          // cout << xyz.X << " " << xyz.Y << " " << used_xy << endl;
          memo[xyz] = ++used_xy;
        }
        used[xyz] = 1;
      }

      int sum = 0;
      // cout << i << endl;
      rep(k, ts[i].size()) {
        if (ts[i][k].value > 0) {
          int cell_id = memo[MP(ts[i][k].x, ts[i][k].y)];
          s[i].push_back(iStump(cell_id, ts[i][k].value));
          xy[cell_id][i] = ts[i][k].value;
          //           cout << cell_id << " -> " << xy[cell_id][i] << endl;
          sum += xy[cell_id][i];
        }
      }
      //      cout << "sum " << sum << endl;
    }
    //     rep(i, used_xy){
    //       rep(j, n){
    //         cout << xy[i][j] << "\\t";
    //       }cout << endl;
    //     }

    // DP
    rep(i, (1 << n) - 1) {
      int state = i;
      if (DBG)
        cout << "baselp " << dp[state] << endl;
      rep(k, n) {
        int mask = 1 << k;
        if ((state | mask) != state) {
          int lp = 0;
          if (DBG)
            cout << "stump(rev) " << k;

          rep(l, s[k].size()) {
            iStump ss = s[k][l];
            int current = 0;

            for (int j = paths[state].size() - 1; j >= 0; --j) {
              if (xy[ss.id][paths[state][j]] > 0) {
                current = xy[ss.id][paths[state][j]];
                break;
              }
            }
            if (current == 0)
              lp += ss.val;
          }
          if (DBG)
            cout << " res = " << lp + dp[state] << endl;
          if (dp[state | mask] < lp + dp[state]) {
            dp[state | mask] = lp + dp[state];
            // paths[state|mask] = paths[state];
            paths[state | mask].clear();
            paths[state | mask].push_back(k);
            rep(j, paths[state].size()) paths[state | mask].push_back(
                paths[state][j]);
          }
        }
      }
    }

    cout << dp[(1 << n) - 1] << endl;
    //     cout << "case" << endl;
    //     rep(i, paths[(1<<n)-1].size()){
    //       cout << paths[(1<<n)-1][i] << endl;
    //     }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define ARRAY 65536
#define DBG 0
#define SHIFT 10000
using namespace std;

class Dice {
  //    1  /  rX     4  /
  //  ___/    ->  _____/
  //  2  | 3       2  |  1
  //  |
  //  v rY
  //
  //   0
  //  1   2
public:
  int value[6];
  int id[6];
  Dice() {
    rep(i, 6) id[i] = i;
    rep(i, 6) value[i] = i + 1;
  }
  int &operator[](int x) { return value[id[x]]; }
  void swp(int a, int b, int c, int d) {
    int tmp = id[a];
    id[a] = id[b];
    id[b] = id[c];
    id[c] = id[d];
    id[d] = tmp;
  }
  void rX() { swp(0, 3, 5, 2); }
  void revX() {
    rX();
    rX();
    rX();
  }
  void rY() { swp(0, 4, 5, 1); }
  void revY() {
    rY();
    rY();
    rY();
  }
  void print() {
    rep(i, 6) cout << value[id[i]] << ",";
    cout << endl;
  }
};

class Stump {
public:
  int x, y, value;
  Stump(int _x, int _y, int _value) : x(_x), y(_y), value(_value){};
};

vector<int> paths[ARRAY];
ll cells[ARRAY];
// int mpl[2200][2200] ={0};

int main(void) {
  int n;
  while (cin >> n) {
    rep(i, ARRAY) {
      paths[i].clear();
      cells[i] = 0;
    }
    int x, y, v;
    int ans = 0;

    string r;
    Dice dice;
    vector<Stump> s[32];

    rep(i, n) {
      cin >> x >> y;
      cin >> dice[3] >> dice[2] >> dice[1] >> dice[4] >> dice[5] >> dice[0];
      x += 1050;
      y += 1050;
      s[i].push_back(Stump(x, y, dice[5]));

      cin >> r;
      rep(j, r.size()) {
        if (r[j] == 'L') {
          dice.revX();
          x--;
        } else if (r[j] == 'R') {
          dice.rX();
          x++;
        } else if (r[j] == 'F') {
          dice.rY();
          y--;
        } else {
          dice.revY();
          y++;
        }
        s[i].push_back(Stump(x, y, dice[5]));
      }
      map<int, int> used;
      for (int k = s[i].size() - 1; k >= 0; --k) {
        if (used[s[i][k].x * SHIFT + s[i][k].y] != 0) {
          s[i][k].value = 0;
        }
        used[s[i][k].x * SHIFT + s[i][k].y] = 1;
      }
      used.clear();
    }

    rep(i, 1 << n) {
      int state = i;
      // int mpl[2200][2200] ={0};
      int mpl[2200][2200] = {0};
      // map<int,int> mpl;
      ll baselp = 0;

      for (int l = paths[state].size() - 1; l >= 0; --l) {
        if (DBG)
          cout << ", " << paths[state][l];
        rep(m, s[paths[state][l]].size()) {
          if (DBG)
            cout << "(" << s[paths[state][l]][m].x << ","
                 << s[paths[state][l]][m].y << ")";
          if (mpl[s[paths[state][l]][m].x][s[paths[state][l]][m].y] == 0) {
            mpl[s[paths[state][l]][m].x][s[paths[state][l]][m].y] =
                s[paths[state][l]][m].value;
            baselp += s[paths[state][l]][m].value;
          }
          // if(mpl[s[paths[state][l]][m].x*SHIFT+s[paths[state][l]][m].y] ==
          // 0){
          //   mpl[s[paths[state][l]][m].x*SHIFT+s[paths[state][l]][m].y] =
          //   s[paths[state][l]][m].value; baselp +=
          //   s[paths[state][l]][m].value;
          // }
        }
      }
      if (DBG)
        cout << "baselp " << baselp << endl;

      rep(k, n) {
        int mask = 1 << k;
        if ((state | mask) != state) {
          ll lp = 0;
          // map<int, int> mpl;
          if (DBG)
            cout << "stump(rev) " << k;

          for (int l = s[k].size() - 1; l >= 0; --l) {
            if (DBG)
              cout << "(" << s[k][l].x << "," << s[k][l].y << ")";
            if (mpl[s[k][l].x][s[k][l].y] > 0) {
              mpl[s[k][l].x][s[k][l].y] = -mpl[s[k][l].x][s[k][l].y];
              lp += mpl[s[k][l].x][s[k][l].y];
              lp += s[k][l].value;
            } else if (mpl[s[k][l].x][s[k][l].y] == 0) {
              lp += s[k][l].value;
            }
            // if( mpl[s[k][l].x*SHIFT+s[k][l].y] > 0){
            //   //mpl[s[k][l].x*SHIFT+s[k][l].y] = s[k][l].value;
            //   mpl[s[k][l].x*SHIFT+s[k][l].y] = -mpl[s[k][l].x*SHIFT+
            //   s[k][l].y]; lp += mpl[s[k][l].x*SHIFT+ s[k][l].y]; lp +=
            //   s[k][l].value; if(DBG) cout << lp << endl;
            //   //s[k][l].value;
            // }else if( mpl[s[k][l].x*SHIFT+s[k][l].y] == 0){
            //   lp += s[k][l].value;
            //   if(DBG) cout << lp << endl;
            // }
          }
          if (DBG)
            cout << " res = " << lp + baselp << endl;
          if (cells[state | mask] < lp + baselp) {
            cells[state | mask] = lp + baselp;
            paths[state | mask] = paths[state];
            paths[state | mask].push_back(k);
          }
          rep(l, s[k].size()) {
            if (DBG)
              cout << "(" << s[k][l].x << "," << s[k][l].y << ")";
            for (int l = s[k].size() - 1; l >= 0; --l) {
              if (mpl[s[k][l].x][s[k][l].y] > 0) {
                mpl[s[k][l].x][s[k][l].y] = -mpl[s[k][l].x][s[k][l].y];
              }
              // if( mpl[s[k][l].x*SHIFT+ s[k][l].y] > 0){
              //   mpl[s[k][l].x*SHIFT+s[k][l].y] =
              //   -mpl[s[k][l].x*SHIFT+s[k][l].y];
              // }
            }
          }
        }
      }
    }
    if (DBG)
      cout << "case ans---" << endl;
    cout << cells[(1 << n) - 1] << endl;
    if (DBG)
      cout << "case end---" << endl;
  }
  return 0;
}