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

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define PB push_back
#define MP make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 105;
constexpr int dx[] = {0, 1, 0, -1};
constexpr int dy[] = {1, 0, -1, 0};

int n, d[MAX_N], a[MAX_N], mem[MAX_N][40330];
vector<vector<int>> perm;
map<vector<int>, int> dic;

int Solve(int i, int j) {
  if (i == n)
    return 0;
  if (mem[i][j] != -1)
    return mem[i][j];

  int res = 0, ai = 0;
  vector<int> cur = perm[j];
  int er = 0;
  for (int k = 0; k < cur.size(); ++k) {
    cur[k]--;
    if (cur[k] == 0)
      er = k;
  }
  for (int k = 0; k < er; ++k) {
    if (n < i + cur[k] || d[i + cur[k]] < cur[k])
      continue;
    ai += a[i + cur[k]];
  }
  for (int k = er + 1; k < cur.size(); ++k) {
    if (d[i] < cur[k] || n < i + cur[k])
      continue;
    ai += a[i];
  }
  cur.erase(cur.begin() + er);
  for (int k = 0; k <= 8; ++k) {
    vector<int> tmp = cur;
    tmp.insert(tmp.begin() + k - (er < k ? 1 : 0), 8);
    res = max(res, Solve(i + 1, dic[tmp]) + ai);
  }

  return mem[i][j] = res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> d[i];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  vector<int> hoge(8);
  for (int i = 1; i <= 8; ++i)
    hoge[i - 1] = i;
  int idx = 0;
  do {
    perm.PB(hoge);
    dic[hoge] = idx++;
  } while (next_permutation(ALL(hoge)));

  memset(mem, -1, sizeof(mem));
  int ans = 0;
  for (int i = 0; i < idx; ++i) {
    ans = max(ans, Solve(1, i));
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define PB push_back
#define MP make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 105;
constexpr int dx[] = {0, 1, 0, -1};
constexpr int dy[] = {1, 0, -1, 0};

int n, d[MAX_N], a[MAX_N], mem[MAX_N][40330];
vector<vector<int>> perm;
map<vector<int>, int> dic;

int Solve(int i, int j) {
  if (i == n)
    return 0;
  if (mem[i][j] != -1)
    return mem[i][j];

  int res = 0, ai = 0;
  vector<int> cur = perm[j];
  int er = 0;
  for (int k = 0; k < cur.size(); ++k) {
    cur[k]--;
    if (cur[k] == 0)
      er = k;
  }
  for (int k = 0; k < er; ++k) {
    if (n < i + cur[k] || d[i + cur[k]] < cur[k])
      continue;
    ai += a[i + cur[k]];
  }
  for (int k = er + 1; k < cur.size(); ++k) {
    if (d[i] < cur[k] || n < i + cur[k])
      continue;
    ai += a[i];
  }
  for (int k = 0; k <= 7; ++k) {
    vector<int> tmp = cur;
    tmp.insert(tmp.begin() + k, 8);
    tmp.erase(tmp.begin() + er + (k <= er ? 1 : 0));
    res = max(res, Solve(i + 1, dic[tmp]) + ai);
  }

  return mem[i][j] = res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> d[i];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  vector<int> hoge(8);
  for (int i = 1; i <= 8; ++i)
    hoge[i - 1] = i;
  int idx = 0;
  do {
    perm.PB(hoge);
    dic[hoge] = idx++;
  } while (next_permutation(ALL(hoge)));

  memset(mem, -1, sizeof(mem));
  int ans = 0;
  for (int i = 0; i < idx; ++i) {
    ans = max(ans, Solve(1, i));
  }

  cout << ans << endl;
}