Problem p01806 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
short p[4][6] = {{5, 4, 2, 3, 0, 1},
                 {4, 5, 2, 3, 1, 0},
                 {0, 1, 5, 4, 2, 3},
                 {0, 1, 4, 5, 3, 2}};
short n, C[20][6], cx[20], cy[20];
string px[20];
map<pair<short, short>, short> D[20], E[1 << 16];
vector<tuple<short, short, short>> _D[20];
vector<pair<short, short>> _E[1 << 15];
short dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, dp[1 << 15];
void Init1(int I) {
  vector<tuple<short, short, short, short>> __D;
  __D.push_back(make_tuple(cx[I], cy[I], -1, C[I][4]));
  D[I][make_pair(cx[I], cy[I])] = C[I][4];
  for (int j = 0; j < px[I].size(); j++) {
    int A = 3, F[6] = {0, 0, 0, 0, 0, 0};
    if (px[I][j] == 'L')
      A = 0;
    if (px[I][j] == 'R')
      A = 1;
    if (px[I][j] == 'F')
      A = 2;
    if (px[I][j] == 'B')
      A = 3;
    for (int k = 0; k < 6; k++)
      F[k] = C[I][p[A][k]];
    for (int k = 0; k < 6; k++)
      C[I][k] = F[k];
    cx[I] += dx[A];
    cy[I] += dy[A];
    __D.push_back(make_tuple(cx[I], cy[I], j, C[I][4]));
    D[I][make_pair(cx[I], cy[I])] = C[I][4];
  }
  sort(__D.begin(), __D.end());
  for (int i = 0; i < __D.size(); i++) {
    if (i == __D.size() - 1)
      _D[I].push_back(
          make_tuple(get<0>(__D[i]), get<1>(__D[i]), get<3>(__D[i])));
    else if (get<0>(__D[i]) != get<0>(__D[i + 1]) ||
             get<1>(__D[i]) != get<1>(__D[i + 1])) {
      _D[I].push_back(
          make_tuple(get<0>(__D[i]), get<1>(__D[i]), get<3>(__D[i])));
    }
  }
  return;
}
void Init2(int p) {
  for (int j = 0; j < n; j++) {
    if ((p / (1 << j)) % 2 == 0)
      continue;
    for (int k = 0; k < _D[j].size(); k++) {
      pair<short, short> pa = make_pair(get<0>(_D[j][k]), get<1>(_D[j][k]));
      if (E[p][pa] == 0) {
        _E[p].push_back(pa);
      }
      E[p][pa] = 1;
    }
  }
  E[p].clear();
  sort(_E[p].begin(), _E[p].end());
}
int main() {
  while (true) {
    for (int i = 0; i < 15; i++) {
      D[i].clear();
      _D[i].clear();
    }
    for (int i = 0; i < 32768; i++) {
      E[i].clear();
      _E[i].clear();
    }
    for (int i = 0; i < 32768; i++)
      dp[i] = -1;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> cx[i] >> cy[i];
      for (int j = 0; j < 6; j++)
        cin >> C[i][j];
      cin >> px[i];
      Init1(i);
    }
    for (int i = 0; i < (1 << n); i++)
      Init2(i);
    dp[0] = 0;
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if ((i / (1 << j)) % 2 == 1)
          continue;
        short cnt = 0;
        for (int k = 0; k < _D[j].size(); k++) {
          pair<short, short> pa = make_pair(get<0>(_D[j][k]), get<1>(_D[j][k]));
          int pos1 =
              lower_bound(_E[i].begin(), _E[i].end(), pa) - _E[i].begin();
          if (_E[i].size() == pos1 || _E[i][pos1] != pa)
            cnt += get<2>(_D[j][k]);
        }
        dp[i + (1 << j)] = max(dp[i + (1 << j)], (short)(dp[i] + cnt));
      }
    }
    cout << dp[(1 << n) - 1] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
short p[4][6] = {{5, 4, 2, 3, 0, 1},
                 {4, 5, 2, 3, 1, 0},
                 {0, 1, 5, 4, 2, 3},
                 {0, 1, 4, 5, 3, 2}};
short n, C[20][6], cx[20], cy[20];
string px[20];
map<pair<short, short>, short> D[20], E[1 << 16];
vector<tuple<short, short, short>> _D[20];
vector<pair<short, short>> _E[1 << 15];
short dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, dp[1 << 15];
void Init1(int I) {
  vector<tuple<short, short, short, short>> __D;
  __D.push_back(make_tuple(cx[I], cy[I], -1, C[I][4]));
  D[I][make_pair(cx[I], cy[I])] = C[I][4];
  for (int j = 0; j < px[I].size(); j++) {
    int A = 3, F[6] = {0, 0, 0, 0, 0, 0};
    if (px[I][j] == 'L')
      A = 0;
    if (px[I][j] == 'R')
      A = 1;
    if (px[I][j] == 'F')
      A = 2;
    if (px[I][j] == 'B')
      A = 3;
    for (int k = 0; k < 6; k++)
      F[k] = C[I][p[A][k]];
    for (int k = 0; k < 6; k++)
      C[I][k] = F[k];
    cx[I] += dx[A];
    cy[I] += dy[A];
    __D.push_back(make_tuple(cx[I], cy[I], j, C[I][4]));
    D[I][make_pair(cx[I], cy[I])] = C[I][4];
  }
  sort(__D.begin(), __D.end());
  for (int i = 0; i < __D.size(); i++) {
    if (i == __D.size() - 1)
      _D[I].push_back(
          make_tuple(get<0>(__D[i]), get<1>(__D[i]), get<3>(__D[i])));
    else if (get<0>(__D[i]) != get<0>(__D[i + 1]) ||
             get<1>(__D[i]) != get<1>(__D[i + 1])) {
      _D[I].push_back(
          make_tuple(get<0>(__D[i]), get<1>(__D[i]), get<3>(__D[i])));
    }
  }
  return;
}
void Init2(int p) {
  for (int j = 0; j < n; j++) {
    if ((p / (1 << j)) % 2 == 0)
      continue;
    for (int k = 0; k < _D[j].size(); k++) {
      pair<short, short> pa = make_pair(get<0>(_D[j][k]), get<1>(_D[j][k]));
      if (E[p][pa] == 0) {
        _E[p].push_back(pa);
      }
      E[p][pa] = 1;
    }
  }
  sort(_E[p].begin(), _E[p].end());
}
int main() {
  while (true) {
    for (int i = 0; i < 15; i++) {
      D[i].clear();
      _D[i].clear();
    }
    for (int i = 0; i < 32768; i++) {
      E[i].clear();
      _E[i].clear();
    }
    for (int i = 0; i < 32768; i++)
      dp[i] = -1;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> cx[i] >> cy[i];
      for (int j = 0; j < 6; j++)
        cin >> C[i][j];
      cin >> px[i];
      Init1(i);
    }
    for (int i = 0; i < (1 << n); i++)
      Init2(i);
    dp[0] = 0;
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if ((i / (1 << j)) % 2 == 1)
          continue;
        short cnt = 0;
        for (int k = 0; k < _D[j].size(); k++) {
          pair<short, short> pa = make_pair(get<0>(_D[j][k]), get<1>(_D[j][k]));
          int pos1 =
              lower_bound(_E[i].begin(), _E[i].end(), pa) - _E[i].begin();
          if (_E[i].size() == pos1 || _E[i][pos1] != pa)
            cnt += get<2>(_D[j][k]);
        }
        dp[i + (1 << j)] = max(dp[i + (1 << j)], (short)(dp[i] + cnt));
      }
    }
    cout << dp[(1 << n) - 1] << endl;
  }
  return 0;
}