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

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map<vector<int>, int> dp;

int solve(vector<int> &vec, int sum = 0, int id = 1) {
  int res = 0;
  if (sum == 10) {
    res++;
    sum = 0;
    id = 1;
  }
  if (dp.count(vec))
    return dp[vec];
  if (sum > 10)
    return 0;

  int add = 0;
  for (int i = id; i <= 9; i++) {
    if (vec[i] > 0) {
      vec[i]--;
      sum += i;
      chmax(add, solve(vec, sum, i));
      vec[i]++;
      sum -= i;
    }
  }
  res += add;

  if (sum == 0)
    dp[vec] = res;
  return res;
}

signed main() {
  while (cin >> N, N) {
    dp.clear();
    vector<int> countManju(11);

    for (int i = 0; i < N; i++) {
      int val;
      cin >> val;
      countManju[val]++;
    }

    int ans = 0;
    for (int i = 1; i < 5; i++) {
      int add = min(countManju[i], countManju[10 - i]);
      ans += add;
      countManju[i] -= add;
      countManju[10 - i] -= add;
    }
    ans += countManju[5] / 2;
    countManju[5] %= 2;

    ans += solve(countManju);
    cout << ans << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map<vector<int>, int> dp;

int solve(vector<int> &vec, int sum = 0, int id = 1) {
  int res = 0;
  if (sum == 10) {
    res++;
    sum = 0;
    id = 1;
  }
  if (sum == 0 and dp.count(vec))
    return dp[vec];
  if (id > 9 or sum + id > 10)
    return 0;

  int add = 0;
  if (vec[id] > 0) {
    vec[id]--;
    chmax(add, solve(vec, sum + id, id));
    vec[id]++;
  }
  chmax(add, solve(vec, sum, id + 1));
  res += add;

  if (sum == 0)
    dp[vec] = res;
  return res;
}

signed main() {
  while (cin >> N, N) {
    dp.clear();
    vector<int> countManju(11);

    for (int i = 0; i < N; i++) {
      int val;
      cin >> val;
      countManju[val]++;
    }

    int ans = 0;
    /*
    for(int i=1; i<5; i++) {
        int add = min(countManju[i], countManju[10 - i]);
        ans += add;
        countManju[i     ] -= add;
        countManju[10 - i] -= add;
    }
    ans += countManju[5] / 2;
    countManju[5] %= 2;
    */

    ans += solve(countManju);
    cout << ans << endl;
  }
  return 0;
}