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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int a;
int l;
P b[100];
int d[93][40320];
vector<int> V;
bool e[8];
int sum;
bool S;
unordered_map<int, int> U;
int memo[40320][8];
int Hash(vector<int> c) {
  int e = 0;
  int f = 1;
  for (int d = 0; d < 8; d++, f *= 10) {
    e += c[d] * f;
  }
  return e;
}
void R() {
  for (int i = 0; i < 8; i++) {
    int JJJ = 0;
    vector<int> o;
    for (int w = 0; w < 8; w++) {
      if (V[w] != 0) {
        o.push_back(V[w] - 1);
      }
    }
    vector<int> copy;
    for (int x = 0; x < i; x++) {
      copy.push_back(o[x]);
      if (7 - o[x] <= b[l + 1 + o[x]].first) {
        JJJ += b[l + 1 + o[x]].second;
      }
    }
    copy.push_back(7);
    P t = b[l + 8];
    int q = b[l + 8].first, w = b[l + 8].second;
    for (int y = i; y < 7; y++) {
      copy.push_back(o[y]);
      if (7 - o[y] <= q) {
        JJJ += w;
      }
    }
    if (l == 0) {
      int z = U[Hash(copy)];
      d[l + 1][z] = max(d[l + 1][z], JJJ + d[l][sum]);
      memo[sum][i] = z;
    } else {
      int z = memo[sum][i];
      d[l + 1][z] = max(d[l + 1][z], JJJ + d[l][sum]);
    }
  }
}
void K() {
  int h = 0;
  U[Hash(V)] = sum;
  for (int i = 0; i < 7; i++) {
    for (int j = i + 1; j < 8; j++) {
      if (abs(V[i] - V[j]) <= b[V[i]].first) {
        h += b[V[i]].second;
      }
    }
  }
  d[0][sum] = h;
}
void saiki(int f) {
  if (f == 8) {
    if (S) {
      R();
    } else {
      K();
    }
    sum++;
  } else {
    for (int i = 0; i < 8; i++) {
      if (!e[i]) {
        V.push_back(i);
        e[i] = true;
        saiki(f + 1);
        e[i] = false;
        V.pop_back();
      }
    }
  }
}
signed main() {
  cin >> a;
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c].first);
  }
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c].second);
  }
  saiki(0);
  S = true;
  for (; l < a - 8; l++) {
    sum = 0;
    saiki(0);
  }
  int MAX = 0;
  for (int kk = 0; kk < 40320; kk++) {
    MAX = max(MAX, d[max(a - 8, 0)][kk]);
  }
  cout << MAX << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int a;
int l;
P b[100];
int d[93][40320];
vector<int> V;
bool e[8];
int sum;
bool S;
unordered_map<int, int> U;
int Hash(vector<int> c) {
  int e = 0;
  int f = 1;
  for (int d = 0; d < 8; d++, f *= 10) {
    e += c[d] * f;
  }
  return e;
}
void R() {
  for (int i = 0; i < 8; i++) {
    int JJJ = 0;
    vector<int> o;
    for (int w = 0; w < 8; w++) {
      if (V[w] != 0) {
        o.push_back(V[w] - 1);
      }
    }
    vector<int> copy;
    for (int x = 0; x < i; x++) {
      copy.push_back(o[x]);
      if (7 - o[x] <= b[l + 1 + o[x]].first) {
        JJJ += b[l + 1 + o[x]].second;
      }
    }
    copy.push_back(7);
    P t = b[l + 8];
    int q = b[l + 8].first, w = b[l + 8].second;
    for (int y = i; y < 7; y++) {
      copy.push_back(o[y]);
      if (7 - o[y] <= q) {
        JJJ += w;
      }
    }
    int z = U[Hash(copy)];
    d[l + 1][z] = max(d[l + 1][z], JJJ + d[l][sum]);
  }
}
void K() {
  int h = 0;
  U[Hash(V)] = sum;
  for (int i = 0; i < 7; i++) {
    for (int j = i + 1; j < 8; j++) {
      if (V[j] >= a)
        continue;
      if (abs(V[i] - V[j]) <= b[V[i]].first) {
        h += b[V[i]].second;
      }
    }
  }
  d[0][sum] = h;
}
void saiki(int f) {
  if (f == 8) {
    if (S) {
      R();
    } else {
      K();
    }
    sum++;
  } else {
    for (int i = 0; i < 8; i++) {
      if (!e[i]) {
        V.push_back(i);
        e[i] = true;
        saiki(f + 1);
        e[i] = false;
        V.pop_back();
      }
    }
  }
}
signed main() {
  cin >> a;
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c].first);
  }
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c].second);
  }
  saiki(0);
  S = true;
  for (; l < a - 8; l++) {
    sum = 0;
    saiki(0);
  }
  int MAX = 0;
  for (int kk = 0; kk < 40320; kk++) {
    MAX = max(MAX, d[max(a - 8, 0)][kk]);
  }
  cout << MAX << endl;
}