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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
// top front right left back bottom
// front
void S(vector<int> &in) {
  int tmp = in[1];
  in[1] = in[0];
  in[0] = in[4];
  in[4] = in[5];
  in[5] = tmp;
}
// right
void E(vector<int> &in) {
  int tmp = in[2];
  in[2] = in[0];
  in[0] = in[3];
  in[3] = in[5];
  in[5] = tmp;
}
// turn
void T(vector<int> &in) {
  int tmp = in[1];
  in[1] = in[2];
  in[2] = in[4];
  in[4] = in[3];
  in[3] = tmp;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<vip> in(n);
    rep(i, n) {
      int x, y;
      cin >> x >> y;
      vi t(6);
      // top front right left back bottom
      cin >> t[3] >> t[2] >> t[1] >> t[4] >> t[5] >> t[0];
      string s;
      cin >> s;
      in[i].pb({t[5], {x, y}});
      rep(j, s.size()) {
        if (s[j] == 'R')
          E(t), x++;
        if (s[j] == 'L')
          E(t), E(t), E(t), x--;
        if (s[j] == 'F')
          S(t), y--;
        if (s[j] == 'B')
          S(t), S(t), S(t), y++;
        rep(k, in[i].size()) if (in[i][k].second == pii(x, y)) {
          in[i][k].first = t[5];
          goto end;
        }
        in[i].pb({t[5], {x, y}});
      end:;
      }
    }
    int N = 1 << n;
    vi dp(N, -inf);
    dp[0] = 0;
    rep(i, N) {
      map<pii, int> ma;
      rep(j, n) if (i & 1 << j) rep(k, in[j].size()) ma[in[j][k].second] = 1;
      rep(j, n) if ((i & 1 << j) == 0) {
        int sum = 0;
        rep(k, in[j].size()) if (ma.count(in[j][k].second) == 0) sum +=
            in[j][k].first;
        cmax(dp[i | 1 << j], dp[i] + sum);
      }
    }
    cout << dp[N - 1] << endl;
  }
}


Error: TLE

Fixed submission:

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
// top front right left back bottom
// front
void S(vector<int> &in) {
  int tmp = in[1];
  in[1] = in[0];
  in[0] = in[4];
  in[4] = in[5];
  in[5] = tmp;
}
// right
void E(vector<int> &in) {
  int tmp = in[2];
  in[2] = in[0];
  in[0] = in[3];
  in[3] = in[5];
  in[5] = tmp;
}
// turn
void T(vector<int> &in) {
  int tmp = in[1];
  in[1] = in[2];
  in[2] = in[4];
  in[4] = in[3];
  in[3] = tmp;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<vip> in(n);
    rep(i, n) {
      int x, y;
      cin >> x >> y;
      vi t(6);
      // top front right left back bottom
      cin >> t[3] >> t[2] >> t[1] >> t[4] >> t[5] >> t[0];
      string s;
      cin >> s;
      in[i].pb({t[5], {x, y}});
      rep(j, s.size()) {
        if (s[j] == 'R')
          E(t), x++;
        if (s[j] == 'L')
          E(t), E(t), E(t), x--;
        if (s[j] == 'F')
          S(t), y--;
        if (s[j] == 'B')
          S(t), S(t), S(t), y++;
        rep(k, in[i].size()) if (in[i][k].second == pii(x, y)) {
          in[i][k].first = t[5];
          goto end;
        }
        in[i].pb({t[5], {x, y}});
      end:;
      }
    }
    int N = 1 << n;
    vi dp(N, -inf);
    dp[0] = 0;
    rep(i, N) rep(j, n) if ((i & 1 << j) == 0) {
      map<pii, int> ma;
      rep(k, n) if (i & 1 << k) rep(l, in[k].size()) ma[in[k][l].second] = 1;
      int sum = 0;
      rep(k, in[j].size()) if (ma[in[j][k].second] == 0) sum += in[j][k].first;
      cmax(dp[i | 1 << j], dp[i] + sum);
    }
    cout << dp[N - 1] << endl;
  }
}