Problem:
G: 水やり
問題
鉢植えが $m$ 個あり、一列に並んでいます。また、$m$ 種類の花があり、種類 $i$ ($1\leq i\leq m$) の花が植えられた鉢植えがちょうど一つあります。これらの鉢植えは、左から一列に並んでいます。したがって、種類 $i$ ($1\leq i \leq m$) の花が左から $P_i$ 番目の鉢植えに植えられているとすると、数列 $P$ は次の性質を満たします。
$1 \leq P_i \leq m$
$P_i \neq P_j$ ($i \neq j$)
これらの花への水やりは自動化されており、長さ $n$ の操作列 $A = (a_1, a_2, \dots, a_n)$ に従い、以下のルールにしたがって行われるようになっています。
如雨露（じょうろ）を $P_{a_1}$（すなわち種類 $a_1$ の花の位置）に持っていきます。
$i$ ($1\leq i\leq n$) 回目の操作では次の区間に水をやります。
$i$ ($1\leq i< n$) 回目の操作では次の区間に水をやります。（13:47 訂正）
$P_{a_i} < P_{a_{i+1}}$ のとき、半開区間 [$P_{a_i}, P_{a_{i+1}}$)
$P_{a_i} > P_{a_{i+1}}$ のとき、半開区間 ($P_{a_{i+1}}, P_{a_i}$]
種類 $a_n$ の花に水をやります。
$n$ 回目の操作では種類 $a_n$ の花に水をやります。（13:47 訂正）
例えば、$A = (1,4,2,3,1)$ で、$P = (2,3,1,4)$ つまり、花が $3,1,2,4$ の順に並んでいるとき、如雨露が水をやる花の種類を順番に並べると、 $1,2,4,2,1,3,1$ となります。
花に水をやりすぎると枯れてしまうため、あなたは花の並び順を変えることで、一つの花に水をやる回数の最大値を最小化したいです。
並び順をうまく調整することで実現できる最大値の最小値を答えてください。
入力形式
$n$ $m$
$a_1$ $a_2$ $\dots$ $a_n$
制約
$1 \leq n \leq 10^5$
$1 \leq m \leq 20$
$1 \leq a_i \leq m$ ($1\leq i\leq n$)
$a_i \neq a_{i+1}$ ($1\leq i < n$)
出力形式
1 行に答えを出力してください。
入力例 1
5 4
1 4 2 3 1
出力例 1
2
例えば $P = (1, 3, 4, 2)$、つまり花の並び順を $(1, 4, 2, 3)$ とすると、如雨露が水をやる花は $(1,4,2,3,2,4,1)$ となり、花 $1, 3, 4$ には $2$ 回ずつ、花 $2$ には $1$ 回水をやる。したがって一つの花に対して水をやった回数の最大値は $2$ である。どのように並び替えても $2$ 未満にすることはできないため、答えは $2$ である。
入力例 2
10 5
2 3 1 4 5 2 4 1 3 5
出力例 2
4

Submitted solutions:

code_snippet: // AOJ #3191 Watering
// 2025.4.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), m = Cin();
    vector<int> A(n);
    for(int i = 0; i < n; i++) A[i] = Cin()-1;

    vector<int> base(m, 0);
    for(int x : A) base[x]++;

    static int tr[20][20];
    for(int i = 0; i + 1 < n; i++) tr[A[i]][A[i+1]]++;

    static int M[20][20];
    vector<int> totalM(m, 0);
    for(int i = 0; i < m; i++){
        for(int j = i+1; j < m; j++){
            M[i][j] = M[j][i] = tr[i][j] + tr[j][i];
            totalM[i] += M[i][j];
            totalM[j] += M[i][j];
        }
    }

    int MS = 1<<m;
    vector<int> sum(MS * m, 0);
    vector<int> cut(MS, 0);

    for(int s = 1; s < MS; s++){
        int lb = s & -s;
        int w  = __builtin_ctz(lb);
        int sp = s ^ lb;
        for(int t = 0; t < m; t++) sum[s*m + t] = sum[sp*m + t] + M[w][t];
        cut[s] = cut[sp] + totalM[w] - 2 * sum[sp*m + w];
    }

    const int INF = 1e9;
    vector<int> dp(MS, INF);
    dp[0] = 0;

    for(int s = 1; s < MS; s++){
        for(int u = 0; u < m; u++){
            if(!(s & (1<<u))) continue;
            int sp = s ^ (1<<u);
            int du = cut[sp] - sum[sp*m + u];
            int cand = max(dp[sp], base[u] + du);
            dp[s] = min(dp[s], cand);
        }
    }
    cout << dp[MS-1] << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 93456
final_rank: 5

code_snippet: // AOJ #3191 Watering
// 2025.4.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), m = Cin();
    vector<int> A(n);
    for(int i = 0; i < n; i++) A[i] = Cin()-1;

    vector<int> base(m, 0);
    for(int x : A) base[x]++;

    static int tr[20][20];
    for(int i = 0; i + 1 < n; i++) tr[A[i]][A[i+1]]++;

    static int M[20][20];
    vector<int> totalM(m, 0);
    for(int i = 0; i < m; i++){
        for(int j = i+1; j < m; j++){
            M[i][j] = M[j][i] = tr[i][j] + tr[j][i];
            totalM[i] += M[i][j];
            totalM[j] += M[i][j];
        }
    }

    int MS = 1<<m;
    vector<int> sum(MS * m, 0);
    vector<int> cut(MS, 0);

    for(int s = 1; s < MS; s++){
        int lb = s & -s;
        int w = __builtin_ctz(lb);
        int sp = s ^ lb;
        for(int t = 0; t < m; t++) sum[s*m + t] = sum[sp*m + t] + M[w][t];
        cut[s] = cut[sp] + totalM[w] - 2 * sum[sp*m + w];
    }

    int lo = *max_element(base.begin(), base.end());
    int hi = n * m;

    vector<bool> dp(MS);
    auto feasible = [&](int K){
        static int cap[20];
        for(int t = 0; t < m; t++) cap[t] = K - base[t];
        fill(dp.begin(), dp.end(), false);
        dp[0] = true;
        for(int s = 1; s < MS; s++){
            for(int u = 0; u < m; u++){
                if(!(s & (1<<u))) continue;
                int sp = s ^ (1<<u);
                if(!dp[sp]) continue;
                int du = cut[sp] - sum[sp*m + u];
                if(du <= cap[u]){
                    dp[s] = true;
                    break;
                }
            }
        }
        return dp[MS-1];
    };

    while(lo < hi){
        int mid = (lo + hi) >> 1;
        if(feasible(mid)) hi = mid;
        else lo = mid + 1;
    }
    cout << lo << endl;
    return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 89668
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

int N,M;
int POW[22],A[SIZE];
int table[22][22],dp[1024*1024];

int main(){

	POW[0] = 1;
	for(int i = 1; i <= 21; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d",&N,&M);

	for(int i = 0; i < M; i++){
		for(int k = 0; k < M; k++){

			table[i][k] = 0;
		}
	}

	for(int i = 0; i < N; i++){

		scanf("%d",&A[i]);
		A[i]--;

		if(i == 0)continue;

		table[A[i-1]][A[i]]++;
	}

	vector<int> L,R;
	for(int state = 0; state < POW[M]; state++){

		dp[state] = BIG_NUM;
	}

	dp[0] = 0;

	for(int state = 0; state < POW[M]-1; state++){

		L.clear();
		R.clear();

		for(int loop = 0; loop < M; loop++){
			if(state&POW[loop]){

				L.push_back(loop);

			}else{

				R.push_back(loop);
			}
		}

		for(int i = 0; i < R.size(); i++){
			int next = R[i];

			int tmp = 0;
			for(int k = 0; k < L.size(); k++){

				tmp += table[next][L[k]];
			}
			for(int k = 0; k < R.size(); k++){
				tmp += table[next][R[k]]; //a[i] != a[i+1]
			}
			for(int a = 0; a < L.size(); a++){
				for(int b = 0; b < R.size(); b++){
					if(R[b] == next)continue;

					tmp += table[L[a]][R[b]];
					tmp += table[R[b]][L[a]];
				}
			}
			int next_state = state+POW[next];

			if(next == A[N-1])tmp++;

			//printf("next_state:%d tmp:%d\n",next_state,tmp);
			dp[next_state] = min(dp[next_state],max(tmp,dp[state]));
		}
	}

	printf("%d\n",dp[POW[M]-1]);

	return 0;
}
accuracy: 1
time_ms: 1220
memory_kb: 7784
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=9167167167167167167;
const int INF=100000000;
const ll mod=998244353;
#define rep(i,a) for (int i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int n,m;
	cin>>n>>m;
	vector<int> A(n);
	rep(i,n) cin>>A[i],A[i]--;
	vector<vector<int>> G(m,vector<int>(m));
	vector<int> p(m);
	rep(i,n-1){
		G[A[i]][A[i+1]]++;
		G[A[i+1]][A[i]]++;
	}
	rep(i,n) p[A[i]]++;
	vector<int> dp(1<<m,INF);
	dp[0]=0;
	rep(i,(1<<m)-1){
		int tmp=0;
		rep(j,m) rep(k,m){
			if((i&(1<<j))!=0&&0==(i&(1<<k))) tmp+=G[j][k];
		}
		rep(l,m){
			if(i&(1<<l)) continue;
			int v=tmp+p[l];
			rep(j,m){
				if(i&(1<<j)) v-=G[j][l];
			}
			chmin(dp[(1<<l)+i],max(v,dp[i]));
		}
	}
	cout<<dp[(1<<m)-1]<<"\n";
}
accuracy: 1
time_ms: 660
memory_kb: 7496
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int dp[1 << 20], cnt[20][20];

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int a[n];
    rep(i, n) {
        scanf("%d", &a[i]);
        --a[i];
        if (i >= 1) ++cnt[a[i - 1]][a[i]];
    }

    rep(S, 1 << m) dp[S] = mod;
    rep(i, m) {
        dp[1 << i] = (i == a[0]);
        rep(j, m) dp[1 << i] += cnt[j][i];
    }
    For(S, 1, 1 << m) if (dp[S] < mod) {
        int ST = 0, TS = 0;
        rep(i, m) if (S >> i & 1) rep(j, m) if (!(S >> j & 1)) {
            ST += cnt[i][j];
            TS += cnt[j][i];
        }

        rep(i, m) if (!(S >> i & 1)) {
            int iS = 0, Ti = 0;
            rep(j, m) {
                if (S >> j & 1)
                    iS += cnt[i][j];
                else
                    Ti += cnt[j][i];
            }
            chmin(dp[S | (1 << i)],
                  max(dp[S], ST + TS + Ti - iS + (i == a[0])));
        }
    }

    printf("%d\n", dp[(1 << m) - 1]);
}
accuracy: 1
time_ms: 730
memory_kb: 8052
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

int N,M;
int POW[22],A[SIZE];
int table[22][22],dp[1024*1024];

int main(){

	POW[0] = 1;
	for(int i = 1; i <= 21; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d",&N,&M);

	for(int i = 0; i < M; i++){
		for(int k = 0; k < M; k++){

			table[i][k] = 0;
		}
	}

	for(int i = 0; i < N; i++){

		scanf("%d",&A[i]);
		A[i]--;

		if(i == 0)continue;

		table[A[i-1]][A[i]]++;
	}

	vector<int> L,R;
	for(int state = 0; state < POW[M]; state++){

		dp[state] = BIG_NUM;
	}

	dp[0] = 0;

	for(int state = 0; state < POW[M]-1; state++){

		L.clear();
		R.clear();

		for(int loop = 0; loop < M; loop++){
			if(state&POW[loop]){

				L.push_back(loop);

			}else{

				R.push_back(loop);
			}
		}

		for(int i = 0; i < R.size(); i++){
			int next = R[i];

			int tmp = 0;
			for(int k = 0; k < L.size(); k++){

				tmp += table[next][L[k]];
			}
			for(int k = 0; k < R.size(); k++){
				tmp += table[next][R[k]]; //a[i] != a[i+1]
			}
			for(int a = 0; a < L.size(); a++){
				for(int b = 0; b < R.size(); b++){
					if(R[b] == next)continue;

					tmp += table[L[a]][R[b]];
					tmp += table[R[b]][L[a]];
				}
			}
			int next_state = state+POW[next];

			if(next == A[N-1])tmp++;

			//printf("next_state:%d tmp:%d\n",next_state,tmp);
			dp[next_state] = min(dp[next_state],max(tmp,dp[state]));
		}
	}

	printf("%d\n",dp[POW[M]-1]);

	return 0;
}
accuracy: 1
time_ms: 1230
memory_kb: 7772
final_rank: 13

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n, m; cin >> n >> m;
  vector<int> a(n); REP(i, n) cin >> a[i], --a[i];
  vector seg(m, vector(m, 0));
  REP(i, n - 1) ++seg[a[i]][a[i + 1]];
  vector memo(1 << m, vector(m, 0));
  REP(i, 1 << m) {
    int sum = 0;
    REP(j, m) {
      if (i >> j & 1) {
        REP(k, m) {
          if (!(i >> k & 1)) sum += seg[j][k] + seg[k][j];
        }
      }
    }
    REP(j, m) {
      if (!(i >> j & 1)) {
        memo[i][j] = sum + (a[n - 1] == j);
        REP(k, m) {
          if (i >> k & 1) {
            memo[i][j] -= seg[k][j];
          } else {
            memo[i][j] += seg[j][k];
          }
        }
      }
    }
  }
  vector<int> dp(1 << m, INF);
  dp[0] = 0;
  REP(i, m) dp[1 << i] = memo[0][i];
  FOR(b, 1, m) {
    int comb = (1 << b) - 1;
    while (comb < (1 << m)) {
      REP(i, m) {
        if (comb >> i & 1) continue;
        chmin(dp[comb | (1 << i)], max(dp[comb], memo[comb][i]));
      }
      int x = comb & -comb, y = comb + x;
      comb = (((comb & ~y) / x) >> 1) | y;
    }
  }
  cout << dp[(1 << m) - 1] << '\n';
  return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 130532
final_rank: 16

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N, M;
ll A[25][25];
ll B[105000];
ll dp[1<<20];
ll ansdp[1<<20];

int main() {
    cin >> N >> M;
    for(int i = 0; i < N; i++) {
        cin >> B[i];
        B[i]--;
    }
    for(int i = 0; i < N - 1; i++) {
        A[B[i]][B[i+1]]++;
    }
    A[B[N-1]][B[N-1]]++;
    for(int i = 0; i < (1 << M); i++) dp[i] = INF;
    dp[0] = 0;
    for(int bits = 0; bits < (1 << M); bits++) {
        //cerr << bits << " " << dp[bits] << endl;
        ll base = 0;
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < M; j++) {
                if((1 & (bits >> i)) ^ (1 & (bits >> j))) {
                    base += A[i][j];
                }
            }
        }
        for(int to = 0; to < M; to++) {
            if(bits & (1 << to)) continue;
            int tobits = bits | (1 << to);
            ll cost = base;
            for(int i = 0; i < M; i++) {
                if(bits & (1 << i)) {
                    cost -= A[i][to];
                } else {
                    cost += A[to][i];
                }
            }
            //cerr << bitset<4>(bits) << " " << bitset<4>(tobits) << " " << cost << endl;
            chmin(dp[bits | (1 << to)], max(dp[bits], cost));
        }
    }
    for(int bits = 1; bits < (1 << M); bits++) {
        ansdp[bits] = dp[bits];
        ll minimum = INF;
        for(int i = 0; i < M; i++) {
            if(!(bits & (1 << i))) continue;
            chmin(minimum, ansdp[bits & (~(1 << i))]);
        }
        chmax(ansdp[bits], minimum);
    }
    cout << ansdp[(1 << M) - 1] << endl;
    return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 20592
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }



const int INF = 1<<29;
int main() {
    int N, M;
    cin >> N >> M;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) cin >> a[i], --a[i];
    vector<vector<int>> num(M, vector<int>(M, 0));
    for (int i = 0; i + 1 < N; ++i) num[a[i]][a[i+1]]++;

    // 前処理
    vector<vector<int>> score(1<<M, vector<int>(M, INF));
    for (int bit = 0; bit < (1<<M); ++bit) {
        int base = 0;
        for (int j = 0; j < M; ++j) {
            if (!(bit & (1<<j))) continue;
            for (int k = 0; k < M; ++k) {
                if (bit & (1<<k)) continue;
                base += num[j][k] + num[k][j];
            }
        }
        for (int v = 0; v < M; ++v) {
            if (bit & (1<<v)) continue;
            int add = 0, sub = 0;
            for (int j = 0; j < M; ++j) {
                if (!(bit & (1<<j))) continue;
                sub += num[j][v]; // [j, v) は除外
            }
            for (int k = 0; k < M; ++k) {
                if ((bit & (1<<k))) continue;
                if (k == v) continue;
                add += num[v][k]; // [v, k) は追加
            }
            score[bit][v] = base + add - sub + (v == a.back());
        }
    }

    // bitDP
    vector<int> dp(1<<M, INF);
    dp[0] = 0;
    for (int bit = 0; bit < (1<<M); ++bit) {
        for (int v = 0; v < M; ++v) {
            if (bit & (1<<v)) continue;
            int nbit = bit | (1<<v);
            chmin(dp[nbit], max(dp[bit], score[bit][v]));
        }
    }
    cout << dp[(1<<M) - 1] << endl;
}
accuracy: 1
time_ms: 1290
memory_kb: 130420
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

ll kai[20];
ll cnt[20][20];
ll cost[(1ll<<20)];
ll total[(1ll<<20)][20];

int main(){

    /// 最後を数えるのを忘れない！！
    ll n, m;
    cin >> n >> m;
    V<ll> a(n);
    REP(i, n) cin >> a[i];
    REP(i, n) a[i]--;

    REP(i, n-1){
        cnt[a[i]][a[i+1]]++;
    }
    REP(i, n) kai[a[i]]++;

    // REP(i, 20){
    //     REP(j, 20){
    //         cout << cnt[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // 前計算1
    REP(mask, (1ll<<(m))){
        ll tmp = 0;
        REP(i, m){
            REP(j, m){
                bool flag1 = ((mask>>i)&1);
                bool flag2 = ((mask>>j)&1);
                if(flag1^flag2){
                    tmp += cnt[i][j];
                }
            }
        }
        cost[mask] = tmp;
    }

    // 前計算2
    REP(mask, (1ll<<m)){
        REP(i, m){
            if(!(mask>>i & 1)){
                total[mask][i] = cost[mask];
                REP(j, m){
                    if(mask>>j & 1){
                        total[mask][i] -= cnt[j][i];
                    }else{
                        if(j != i){
                            total[mask][i] += cnt[i][j];
                        }
                    }
                }
            }
        }
    }

    // dump(ok)
    V<ll> dp(1ll<<m, INF);
    dp[0] = 0;
    REP(mask, (1ll<<m)){
        REP(i, m){
            if(!(mask>>i & 1)){
                ll kaisu = total[mask][i];
                if(i == a.back()) kaisu++;
                ll next_mask = mask ^ (1ll<<i);
                chmin(dp[next_mask], max(dp[mask], kaisu));
            }
        }
    }

    cout << dp[(1ll<<m)-1] << endl;

    return 0;
}
accuracy: 1
time_ms: 1070
memory_kb: 184156
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) {
            os << ',';
        }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    vector G(m, vector(m, 0));
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    for(int i = 0; i < n - 1; i++) {
        G[a[i]][a[i + 1]]++;
    }
    G[a[n - 1]][a[n - 1]]++;
    vector<int> dp(1 << m, INF);
    dp[0] = 0;
    for(int bit = 0; bit < (1 << m); bit++) {
        int sum = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < m; j++) {
                if((bit >> i & 1) != (bit >> j & 1)) {
                    sum += G[i][j];
                }
            }
        }
        for(int i = 0; i < m; i++) {
            if((bit >> i) & 1) {
                continue;
            }
            int cost = sum;
            for(int j = 0; j < m; j++) {
                if((bit >> j) & 1) {
                    cost -= G[j][i];
                } else {
                    cost += G[i][j];
                }
            }
            chmin(dp[bit | (1 << i)], max(dp[bit], cost));
        }
    }
    cout << dp[(1 << m) - 1] << endl;
}
accuracy: 1
time_ms: 960
memory_kb: 7472
final_rank: 9

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n, m;
vector<int> a;

// vector<vector<int>> res((1<<m), vector<int>(m, 0));
vector<int> mae;
vector<int> co;

bool check(int x) {
    vector<bool> dp((1<<m), 0);
    dp[0] = 1;
    rep(bit, (1<<m)) {
        if(!dp[bit]) continue;
        rep(i, m) {
            if(!(bit & (1<<i))) {
                if(n - 1 - mae[bit] - mae[((1<<m) - 1) ^ (bit | (1<< i))] - co[i] <= x) {
                    dp[bit | (1<<i)] = 1;
                }
            }
        }
    }
    return dp[(1<<m) - 1];
}

void solve() {
	cin >> n >> m;
    a.resize(n);
    vector<vector<int>> cnt(m, vector<int>(m, 0));
    co.assign(m, 0);
    rep(i, n) {
        cin >> a[i];
        -- a[i];
        if(i != n - 1 and i != 0) co[a[i]] ++;
    }
    rep(i, n - 1) {
        cnt[a[i]][a[i + 1]] ++;
        cnt[a[i + 1]][a[i]] ++;
    }
    mae.assign((1<<m), 0);
    rep(bit, (1<<m)) {
        rep(j, m) {
            if(bit & (1<<j)) {
                for(int k = j + 1; k < m; ++ k) {
                    if(bit & (1<<k)) mae[bit] += cnt[j][k];
                }
            }
        }
        // cerr << bit << " " << mae[bit] << endl;
    }
    int ng = -1, ok = 2 * n;
    while(ok - ng > 1) {
        int mid = (ok + ng) / 2;
        if(check(mid)) ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 12052
final_rank: 7

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int b[22][22];

int main(){
    int n,m; cin >> n >> m;
    vl a(n); rep(i,n) cin >> a[i], a[i]--;
    rep(i,n-1){
        b[a[i]][a[i+1]]++;
    }
    b[a[n-1]][a[n-1]]++;
    vector<int> dp(1<<m,inf), cost(1<<m,0);
    dp[0] = 0;
    rep(bit,1<<m){
        rep(i,m){
            if(bit>>i & 1) continue;
            int nxt = bit | (1<<i);
            int s = 0;
            int mi = 0;
            rep(j,m){
                if(i == j) continue;
                if(bit>>j & 1){
                    s -= b[j][i];
                    s -= b[i][j];
                    mi -= b[i][j];
                }
                else{
                    s += b[i][j];
                    s += b[j][i];
                    mi += b[j][i];
                }
            }
            chmin(dp[nxt], max(dp[bit], cost[bit] + s - mi + b[i][i]));
            cost[nxt] = cost[bit] + s;
        }
    }
    cout << dp[(1<<m)-1] << "\n";
}
accuracy: 1
time_ms: 530
memory_kb: 12184
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}


signed main(){
    int n,m;cin>>n>>m;
    vector<int> a(n);
    cin>>a;
    for(auto &x:a) x--;
    vector<vector<int>> mat(m,vector<int>(m,0));
    rep(i,n-1){
        mat[a[i]][a[i+1]]++;
    }
    mat[a[n-1]][a[n-1]]++;

    vector<bool> check((1<<m),false);
    vector<int> dp((1<<m),INF);

    
    vector<int> cost(1<<m,0);

    rep(bit,1<<m){
        rep(i,m)rep(j,m){
            if(((bit>>i)&1)!=((bit>>j)&1)) cost[bit]+=mat[i][j];
        }
    }



    function<int(int)> DP=[&](int bit){
        if(bit==0) return 0;
        if(check[bit]) return dp[bit];
        
        int ret=INF;
        rep(i,m)if((bit>>i)&1){
            int res=0;
            int pbit=bit^(1<<i);
            chmax(res,DP(pbit));
            int s=cost[bit];
            rep(j,m){
                if(((bit>>j)&1)==0) s-=mat[i][j]+mat[j][i];
                s+=mat[i][j];
            }
            chmax(res,s);
            chmin(ret,res);
        }
        check[bit]=true;
        return dp[bit]=ret;
    };  
    cout<<DP((1<<m)-1)<<endl;
    return 0;
}
accuracy: 1
time_ms: 1350
memory_kb: 11604
final_rank: 15

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int b[22][22];

int main(){
    int n,m; cin >> n >> m;
    vl a(n); rep(i,n) cin >> a[i], a[i]--;
    rep(i,n-1){
        b[a[i]][a[i+1]]++;
    }
    b[a[n-1]][a[n-1]]++;
    vector<int> dp(1<<m,inf);
    dp[0] = 0;
    rep(bit,1<<m){
        int cost = 0;
        rep(i,m){
            rep(j,m){
                if((bit>>i & 1) != (bit>>j & 1)){
                    cost += b[i][j];
                }
            }
        }
        rep(i,m){
            if(bit>>i & 1) continue;
            int nxt = bit | (1<<i);
            int s = 0;
            rep(j,m){
                if(bit>>j & 1) s += b[j][i];
                else s -= b[i][j];
            }
            chmin(dp[nxt], max(dp[bit], cost - s));
        }
    }
    cout << dp[(1<<m)-1] << "\n";
}
accuracy: 1
time_ms: 960
memory_kb: 8052
final_rank: 10

code_snippet: //#define _GLIBCXX_DEBUG

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = int;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e9;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>a(n);
  vector<ll>b(m);
  auto v=vec(m,m,0LL);
  rep(i,0,n){
    cin>>a[i];a[i]--;
    b[a[i]]++;
  }
  rep(i,0,n-1){
    v[a[i]][a[i+1]]++;
  }
  vector<ll>dp(1<<m,INF);
  vector<ll>prev(1<<m,0);
  dp[0]=0;
  rep(i,0,1<<m){
    rep(j,0,m){
      if(i&1<<j)continue;
      ll add=prev[i];
      ll cost=b[j]+add;
      rep(o,0,m){
        if(i&1<<o){
          cost-=v[j][o];
          cost-=v[o][j];
          add-=v[j][o];
          add-=v[o][j];
        }
        else{
          add+=v[j][o];
          add+=v[o][j];
        }
      }
      chmin(dp[i|1<<j],max(dp[i],cost));
      prev[i|1<<j]=add;
    }
  }
  cout<<dp[(1<<m)-1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 11620
final_rank: 2

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

typedef array<ll, 2> ary;
int main(){
  ll n, m;std::cin >> n >> m;
  vll a(n);
  for(int i=0;i<n;i++){
    std::cin >> a[i];
    a[i]--;
  }
  vvl G = VV(m, m, 0, ll);
  vvl T = VV(m, m, 0, ll);
  for(int i=0;i<n-1;i++){
    G[a[i]][a[i+1]]++;
    T[a[i+1]][a[i]]++;
  }
  vector<vector<int>> cost(m, vector<int>(1<<m));

  for(int i=0;i<(1<<m);i++){//sがある状態で
    ll cnt = 0;
    for(int j=0;j<m;j++){
      if(((i>>j)&1)==0) continue;
      for(int k=0;k<m;k++){
        if((i>>k)&1) continue;
        cnt+=G[j][k];
        cnt+=T[j][k];
      }
    }
    for(int j=0;j<m;j++){
      if((i>>j)&1) continue;
      //kを含んでいる状態でjを選んだ場合
      ll tmp = cnt;
      for(int k=0;k<m;k++) {
        if((i>>k)&1) {
          tmp -= T[j][k];
        }else{
          tmp += G[j][k];
        }
      }
      cost[j][i] = tmp + (j==a[n-1]?1:0);
    }
  }

  ll INF = 1000000000;
  vector<vector<int>> dp(m+1, vector<int>(1<<m, INF));
  dp[0][0] = 0;
  for(int i=0;i<m;i++){
    int comb = (1<<i)-1;
    while(comb < (1 << m)){
      ll S = comb;
      for(int j=0;j<m;j++){
        if((S>>j)&1) continue;
        dp[i+1][S|(1<<j)] = min(dp[i+1][S|(1<<j)], max(dp[i][S], cost[j][S]));
      }
      //process
      if(i==0) break;
      int x = comb & -comb, y = comb + x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
  }
  std::cout << dp[m][(1<<m)-1] << '\n';
}
accuracy: 1
time_ms: 690
memory_kb: 176156
final_rank: 18

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<random>
#include<stdio.h>
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
using P=pair<ll,ll>;

int main(){
    ll n,m;
    scanf("%lld %lld",&n,&m);
    ll pre;
    scanf("%lld",&pre);

    vector<vector<ll>> data(m,vector<ll>(m));
    vector<ll> data_only(m);
    vector<ll> data_sum(m);

    ll a;

    for (ll i=0;i<n-1;i++){
        scanf("%lld",&a);
        data[a-1][pre-1] += 1;
        data[pre-1][a-1] += 1;
        data_only[pre-1] += 1;
        pre = a;
    }

    data_only[pre-1] += 1;

    for (ll i=0;i<m;i++){
        for (ll j=0;j<m;j++){
            data_sum[i] += data[i][j];
        }
        data_sum[i] -= 2*data_only[i];
    }

    vector<ll> base_score(1<<m);
    ll idx,score;

    for (ll rest=1;rest<(1<<m);rest++){
        ll lsb = rest&(-rest);
        idx = 0;
        while (lsb%2==0){
            idx += 1;
            lsb>>=1;
        }
        lsb = 1<<idx;
        score = base_score[rest-lsb];
        for (ll i=0;i<m;i++){
            if ((rest>>i)&1){
                score -= data[idx][i];
            }
            else{
                score += data[idx][i];
            }
        }
        base_score[rest] = score;
    }

    vector<ll> dp(1<<m,1000000000);
    dp[0] = 0;
    ll mask = (1<<m) - 1;

    for (ll rest=1;rest<(1<<m);rest++){
        for (ll i=0;i<m;i++){
            if ((rest>>i)&1){
                ll tmp = max((-data_sum[i] + base_score[rest] + base_score[rest-(1<<i)])>>1,dp[rest-(1<<i)]);
                dp[rest] = min(tmp,dp[rest]);
            }
        }
    }

    cout<<dp[mask]<<endl;

}
accuracy: 1
time_ms: 70
memory_kb: 19624
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n,m;cin >> n >>m;
    vvl g(m,vl(m));
    vl a(n);
    rep(i,n)cin >> a[i],a[i]--;
    rep(i,n-1){
        g[a[i]][a[i+1]]++;
    }
    vector<ll> dp(1<<m,INF);dp[0]=0;
    rep(bit,1<<m){
        ll al=0;
        rep(i,m){
            if(!(bit&(1<<i)))continue;
            rep(j,m){
                if(bit&(1<<j))continue;
                al+=g[i][j];
                al+=g[j][i];
            }
        }
        rep(i,m){
            if(bit&(1<<i))continue;
            ll pls=al;
            rep(j,m){
                if(bit&(1<<j)){
                    pls-=g[j][i];
                }
                else pls+=g[i][j];
            }
            if(i==a[n-1])pls++;
            chmin(dp[bit|(1<<i)],max(pls,dp[bit]));
        }
    }
    cout << dp[(1<<m)-1] <<endl;
    //rep(bit,1<<m)cout << dp[bit] <<endl;
}
accuracy: 1
time_ms: 870
memory_kb: 12144
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, m;
vector<vector<int>> memo, table;
vector<int> a, dp;

long long solve();

int main() {
  cin >> n >> m;
  a.resize(n);
  for (auto &p : a) {
    cin >> p;
    --p;
  }
  {  // make memo
    memo.assign(m, vector<int>(m, 0));
    for (int i = 1; i < n; ++i) ++memo[a[i - 1]][a[i]];
    ++memo[a[n - 1]][a[n - 1]];
  }
  cout << solve() << endl;
  return 0;
}
long long solve() {
  dp.assign((1 << m), n + 1);
  dp[0] = 0;
  // iが右にいて左のビットがjのときに何回行くか
  table.assign(m, vector<int>(1 << m, 0));
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < (1 << m); ++j)
      for (int k = 0; k < m; ++k)
        if (j >> k & 1) {
          table[i][j] += memo[i][k] + memo[k][i];
        }
  for (int i = 0; i < (1 << m); ++i)
    for (int j = 0; j < m; ++j)
      if (i >> j & 1) {
        int bf = i ^ (1 << j), now = dp[bf], cnt = 0;
        for (int k = 0; k < m; ++k) cnt += memo[j][k];
        for (int k = 0; k < m; ++k)
          if (!(i >> k & 1)) cnt += table[k][bf];
        now = max(cnt, now);
        dp[i] = min(dp[i], now);
      }
  return dp[(1 << m) - 1];
}
accuracy: 1
time_ms: 1210
memory_kb: 93712
final_rank: 17