Problem p00556 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (auto i = (a); i >= (b); --i)

#define REP(i, a) for (long i = 0; i < (a); ++i)
#define RREP(i, a) for (long i = (a); i >= 0; --i)

#define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i)
#define REACH(i, a)                                                            \\
  for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i)

// Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a, x) ((a).find(x) != (a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a)                                                              \\
  std::sort((a).begin(), a.end()),                                             \\
      a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

// Setting
#define OPT                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios::sync_with_stdio(false);

// debug message
bool debug = true;
#define MSG(s)                                                                 \\
  if (debug) {                                                                 \\
    std::cout << s << std::endl;                                               \\
  }
#define DEBUG(x)                                                               \\
  if (debug) {                                                                 \\
    std::cout << "debug(" << #x << "): " << x << std::endl;                    \\
  }

// alias
typedef long long LL;
typedef std::pair<int, int> PII;

typedef std::vector<char> VC;
typedef std::vector<int> VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;
typedef std::vector<PII> VPII;

typedef std::vector<VC> VC2;
typedef std::vector<VI> VI2;
typedef std::vector<VL> VL2;
typedef std::vector<VLL> VLL2;

int N, M;
VI memo(1 << 20, -1);
VI2 b(21, VI(100001, 0));

int dp(int k, int bit) {
  if (k == N)
    return 0;
  if (memo[bit] != -1)
    return memo[bit];

  int ret = std::numeric_limits<int>::max();
  FOR(i, 1, M + 1) {
    if ((bit & 1 << (i - 1)) == 0) {
      int x = N - b[i][N];
      int y = b[i][k + x] - b[i][k];
      ret = std::min(ret, dp(k + x, bit | 1 << (i - 1)) + y);
    }
  }
  return memo[bit] = ret;
}

int main() {
  std::cin >> N >> M;
  FOR(i, 1, N + 1) {
    int a;
    std::cin >> a;
    FOR(j, 1, M + 1) {
      if (j != a)
        b[j][i]++;
      b[j][i] += b[j][i - 1];
    }
  }
  std::cout << dp(0, 0) << std::endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (auto i = (a); i >= (b); --i)

#define REP(i, a) for (long i = 0; i < (a); ++i)
#define RREP(i, a) for (long i = (a); i >= 0; --i)

#define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i)
#define REACH(i, a)                                                            \\
  for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i)

// Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a, x) ((a).find(x) != (a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a)                                                              \\
  std::sort((a).begin(), a.end()),                                             \\
      a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

// Setting
#define OPT                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios::sync_with_stdio(false);

// debug message
bool debug = true;
#define MSG(s)                                                                 \\
  if (debug) {                                                                 \\
    std::cout << s << std::endl;                                               \\
  }
#define DEBUG(x)                                                               \\
  if (debug) {                                                                 \\
    std::cout << "debug(" << #x << "): " << x << std::endl;                    \\
  }

// alias
typedef long long LL;
typedef std::pair<int, int> PII;

typedef std::vector<char> VC;
typedef std::vector<int> VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;
typedef std::vector<PII> VPII;

typedef std::vector<VC> VC2;
typedef std::vector<VI> VI2;
typedef std::vector<VL> VL2;
typedef std::vector<VLL> VLL2;

int N, M;
VI p;
VI n(21, 0);
VI2 c(23, VI(112345));
VI memo(1 << 21, -1);

inline int count(int p, int l, int r) {
  return c[p][r - 1] - ((l == 0) ? 0 : c[p][l - 1]);
}

int dp(int i, int j, int b) {
  if (memo[i] != -1)
    return memo[i];
  if (b == 1)
    return memo[i] = count(log2(i), 0, j);

  int a = 0;
  REP(p, M) {
    if (i & 1 << p) {
      a = std::max(a,
                   dp(i ^ (1 << p), j - n[p], b - 1) + count(p, j - n[p], j));
    }
  }
  return (memo[i] = a);
}

int main() {
  std::cin >> N >> M;
  p.resize(N);
  REP(i, N) {
    std::cin >> p[i];
    ++n[p[i] - 1];
  }

  REP(i, M) {
    int t = 0;
    REP(j, N) {
      if (p[j] == i + 1)
        ++t;
      c[i][j] = t;
      c[i][j - 1] = t;
    }
  }

  int res = N - dp((1 << M) - 1, N, M);

  std::cout << res << std::endl;
  return 0;
}