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

#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;
struct wolf {
  int t[9];
  wolf() {}
};
int v[9];
int D[36][9] = {{2, 0, 0, 0, 0, 0, 0, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 0, 0},
                {3, 0, 0, 0, 0, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 1, 0, 0, 0},
                {0, 2, 0, 0, 0, 1, 0, 0, 0}, {2, 1, 0, 0, 0, 1, 0, 0, 0},
                {4, 0, 0, 0, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 1, 0, 0, 0, 0},
                {0, 1, 1, 0, 1, 0, 0, 0, 0}, {2, 0, 1, 0, 1, 0, 0, 0, 0},
                {1, 2, 0, 0, 1, 0, 0, 0, 0}, {3, 1, 0, 0, 1, 0, 0, 0, 0},
                {5, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 2, 0, 0, 0, 0, 0},
                {0, 0, 2, 1, 0, 0, 0, 0, 0}, {2, 0, 0, 2, 0, 0, 0, 0, 0},
                {1, 1, 1, 1, 0, 0, 0, 0, 0}, {0, 3, 0, 1, 0, 0, 0, 0, 0},
                {3, 0, 1, 1, 0, 0, 0, 0, 0}, {2, 2, 0, 1, 0, 0, 0, 0, 0},
                {4, 1, 0, 1, 0, 0, 0, 0, 0}, {6, 0, 0, 1, 0, 0, 0, 0, 0},
                {1, 0, 3, 0, 0, 0, 0, 0, 0}, {0, 2, 2, 0, 0, 0, 0, 0, 0},
                {2, 1, 2, 0, 0, 0, 0, 0, 0}, {4, 0, 2, 0, 0, 0, 0, 0, 0},
                {1, 3, 1, 0, 0, 0, 0, 0, 0}, {3, 2, 1, 0, 0, 0, 0, 0, 0},
                {5, 1, 1, 0, 0, 0, 0, 0, 0}, {7, 0, 1, 0, 0, 0, 0, 0, 0},
                {0, 5, 0, 0, 0, 0, 0, 0, 0}, {2, 4, 0, 0, 0, 0, 0, 0, 0},
                {4, 3, 0, 0, 0, 0, 0, 0, 0}, {6, 2, 0, 0, 0, 0, 0, 0, 0},
                {8, 1, 0, 0, 0, 0, 0, 0, 0}, {10, 0, 0, 0, 0, 0, 0, 0, 0}};
long long conv(wolf a) {
  long long ret = 0;
  for (int i = 0; i < 9; i++) {
    ret *= 128;
    ret += a.t[i];
  }
  return ret;
}
map<long long, int> dp;
int solve(wolf a, int b) {
  if ((int)(dp.count(conv(a))))
    return dp[conv(a)];
  int ret = 0;
  for (int i = b; i < 36; i++) {
    bool ok = true;
    for (int j = 0; j < 9; j++) {
      if (a.t[j] < D[i][j]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      for (int j = 0; j < 9; j++)
        a.t[j] -= D[i][j];
      ret = max(ret, solve(a, i) + 1);
      for (int j = 0; j < 9; j++)
        a.t[j] += D[i][j];
    }
  }
  return dp[conv(a)] = ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    dp.clear();
    for (int i = 0; i < 10; i++)
      v[i] = 0;
    for (int i = 0; i < a; i++) {
      int b;
      scanf("%d", &b);
      b--;
      v[b]++;
    }
    int ret = 0;
    for (int i = 5; i < 9; i++) {
      int V = min(v[i], v[8 - i]);
      ret += V;
      v[i] -= V;
      v[8 - i] -= V;
    }
    ret += v[4] / 2;
    v[4] %= 2;
    wolf S;
    for (int i = 0; i < 9; i++)
      S.t[i] = v[i];
    printf("%d\\n", ret + solve(S, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;
struct wolf {
  int t[9];
  wolf() {}
};
inline bool operator<(const wolf &a, const wolf &b) {
  for (int i = 0; i < 9; i++)
    if (a.t[i] < b.t[i])
      return true;
  return false;
}
int v[9];
int D[36][9] = {{2, 0, 0, 0, 0, 0, 0, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 0, 0},
                {3, 0, 0, 0, 0, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 1, 0, 0, 0},
                {0, 2, 0, 0, 0, 1, 0, 0, 0}, {2, 1, 0, 0, 0, 1, 0, 0, 0},
                {4, 0, 0, 0, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 1, 0, 0, 0, 0},
                {0, 1, 1, 0, 1, 0, 0, 0, 0}, {2, 0, 1, 0, 1, 0, 0, 0, 0},
                {1, 2, 0, 0, 1, 0, 0, 0, 0}, {3, 1, 0, 0, 1, 0, 0, 0, 0},
                {5, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 2, 0, 0, 0, 0, 0},
                {0, 0, 2, 1, 0, 0, 0, 0, 0}, {2, 0, 0, 2, 0, 0, 0, 0, 0},
                {1, 1, 1, 1, 0, 0, 0, 0, 0}, {0, 3, 0, 1, 0, 0, 0, 0, 0},
                {3, 0, 1, 1, 0, 0, 0, 0, 0}, {2, 2, 0, 1, 0, 0, 0, 0, 0},
                {4, 1, 0, 1, 0, 0, 0, 0, 0}, {6, 0, 0, 1, 0, 0, 0, 0, 0},
                {1, 0, 3, 0, 0, 0, 0, 0, 0}, {0, 2, 2, 0, 0, 0, 0, 0, 0},
                {2, 1, 2, 0, 0, 0, 0, 0, 0}, {4, 0, 2, 0, 0, 0, 0, 0, 0},
                {1, 3, 1, 0, 0, 0, 0, 0, 0}, {3, 2, 1, 0, 0, 0, 0, 0, 0},
                {5, 1, 1, 0, 0, 0, 0, 0, 0}, {7, 0, 1, 0, 0, 0, 0, 0, 0},
                {0, 5, 0, 0, 0, 0, 0, 0, 0}, {2, 4, 0, 0, 0, 0, 0, 0, 0},
                {4, 3, 0, 0, 0, 0, 0, 0, 0}, {6, 2, 0, 0, 0, 0, 0, 0, 0},
                {8, 1, 0, 0, 0, 0, 0, 0, 0}, {10, 0, 0, 0, 0, 0, 0, 0, 0}};
// map<wolf,int> dp;
int solve(wolf a, int b) {
  //	if((int)(dp.count(a)))return dp[a];
  int ret = 0;
  for (int i = b; i >= 0; i--) {
    bool ok = true;
    for (int j = 0; j < 9; j++) {
      if (a.t[j] < D[i][j]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      for (int j = 0; j < 9; j++)
        a.t[j] -= D[i][j];
      ret = max(ret, solve(a, i) + 1);
      for (int j = 0; j < 9; j++)
        a.t[j] += D[i][j];
    }
  }
  return ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 10; i++)
      v[i] = 0;
    for (int i = 0; i < a; i++) {
      int b;
      scanf("%d", &b);
      b--;
      v[b]++;
    }
    int ret = 0;
    for (int i = 5; i < 9; i++) {
      int V = min(v[i], v[8 - i]);
      ret += V;
      v[i] -= V;
      v[8 - i] -= V;
    }
    ret += v[4] / 2;
    v[4] %= 2;
    wolf S;
    for (int i = 0; i < 9; i++)
      S.t[i] = v[i];
    printf("%d\\n", ret + solve(S, 35));
  }
}