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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ld pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //???£??\\????????????¨???
  ll to, cost;
};
struct S { //????????????±?????\\????????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
ll gcm(ll i, ll j) { //??????§???¬??´????°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad??????§??¨???????2???????????????¢
  //---------------------------------------------------
  //+++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m;
int a[100000], b[20][100000], c[20];
int dp[1048576];
int solve(int s, int sum) {
  if (s == (1 << m) - 1)
    return 0;
  if (dp[s] != -1)
    return dp[s];
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (!(s & (1 << i))) {
      int ret = c[i] - b[i][sum + c[i] - 1] + (sum != 0 ? b[i][sum - 1] : 0);
      res = min(res, solve(s | (1 << i), sum + c[i]) + ret);
    }
  }
  return dp[s] = res;
}
signed main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[--a[i]]++;
    b[a[i]][i]++;
    for (int j = 0; j < m; j++)
      b[j][i + 1] = b[j][i];
  }
  for (int i = 0; i < 1048576; i++)
    dp[i] = -1;
  cout << solve(0, 0) << endl;
  getchar();
  getchar();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ld pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad?????§?¨???????2??????????????¢
  //---------------------------------------------------
  //+++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m;
int a[100000], b[20][100000], c[20];
int dp[1048576];
int solve(int s, int sum) {
  if (s == (1 << m) - 1)
    return 0;
  if (dp[s] != -1)
    return dp[s];
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (!(s & (1 << i))) {
      int ret = c[i] - b[i][sum + c[i] - 1] + (sum != 0 ? b[i][sum - 1] : 0);
      res = min(res, solve(s | (1 << i), sum + c[i]) + ret);
    }
  }
  return res;
}
signed main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[--a[i]]++;
    b[a[i]][i]++;
    for (int j = 0; j < m; j++)
      b[j][i + 1] = b[j][i];
  }
  for (int i = 0; i < 1048576; i++)
    dp[i] = -1;
  cout << solve(0, 0) << endl;
  getchar();
  getchar();
}