atcoder#ARC126D. [ARC126D] Pure Straight

[ARC126D] Pure Straight

Score : 600600 points

Problem Statement

Given is a sequence of NN positive integers A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N), where each AiA_i is 11, 22, \ldots, or KK.

You can do the following operation on this sequence any number of times.

  • Swap two adjacent elements, that is, choose ii and jj such that ij=1|i-j|=1 and swap AiA_i and AjA_j.

Find the minimum number of operations needed to make AA satisfy the following condition.

  • AA contains (1,2,,K)(1, 2, \ldots, K) as a contiguous subsequence, that is, there is a positive integer nn at most NK+1N-K+1 such that An=1A_n = 1, An+1=2A_{n+1} = 2, \ldots, and An+K1=KA_{n+K-1} = K.

Constraints

  • 2K162\leq K\leq 16
  • KN200K \leq N\leq 200
  • AiA_i is 11, 22, \ldots, or KK.
  • AA contains at least one occurrence of each of 1,2,,K1, 2, \ldots, K.

Input

Input is given from Standard Input in the following format:

NN KK

A1A_1 A2A_2 \ldots ANA_N

Output

Print the minimum number of operations needed to make AA satisfy the condition.

4 3
3 1 2 1
2

One optimal sequence of operations is as follows.

  • Swap A1A_1 and A2A_2, changing AA to (1,3,2,1)(1,3,2,1).
  • Swap A2A_2 and A3A_3, changing AA to (1,2,3,1)(1,2,3,1).
  • Now we have A1=1A_1 = 1, A2=2A_2 = 2, A3=3A_3 = 3, satisfying the condition.
5 5
4 1 5 2 3
5
8 4
4 2 3 2 4 2 1 4
5