Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese].

There are $N$ guests (numbered $1$ through $N$) coming to Chef's wedding. Each guest is part of a family; for each valid $i$, the $i$-th guest is part of family $F_{i}$. You need to help Chef find an optimal seating arrangement.

Chef may buy a number of tables, which are large enough for any number of guests, but the people sitting at each table must have consecutive numbers ― for any two guests $i$ and $j$ ($i < j$) sitting at the same table, guests $i+1, i+2, \ldots, j-1$ must also sit at that table. Chef would have liked to seat all guests at a single table; however, he noticed that two guests $i$ and $j$ are likely to get into an argument if $F_{i} = F_{j}$ and they are sitting at the same table.

Each table costs $K$ rupees. Chef defines the *inefficiency* of a seating arrangement as the total cost of tables plus the number of guests who are likely to get into an argument with another guest. Tell Chef the minimum possible inefficiency which he can achieve.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $K$.
The second line contains $N$ space-separated integers $F_{1}, F_{2}, \ldots, F_{N}$.

------  Output ------
For each test case, print a single line containing one integer ― the smallest possible inefficiency.

------  Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1,000$
$1 ≤ K ≤ 1,000$
$1 ≤ F_{i} ≤ 100$ for each valid $i$
The sum of $N$ across test cases is $≤ 5,000$

------  Subtasks ------
Subtask #1 (20 points): $K = 1$
Subtask #2 (80 points): original constraints

----- Sample Input 1 ------ 
3

5 1

5 1 3 3 3

5 14

1 4 2 4 4

5 2

3 5 4 5 1
----- Sample Output 1 ------ 
3

17

4
----- explanation 1 ------ 
Example case 1: The optimal solution is to use three tables with groups of guests $[1, 2, 3]$, $[4]$ and $[5]$. None of the tables have any guests that are likely to get into an argument, so the inefficiency is $3 \cdot K = 3$.

Example case 2: The optimal solution is to seat all guests at one table. Then, guests $2$, $4$ and $5$ are likely to get into an argument with each other, so the inefficiency is $K + 3 = 17$.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(n, cost) = list(map(int, input().split()))
	guest = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp[0] = 0
	dp[1] = cost
	for i in range(2, len(dp)):
		d = {}
		arg = 0
		dp[i] = cost + dp[i - 1]
		for j in range(i, 0, -1):
			d[guest[j - 1]] = d.get(guest[j - 1], 0) + 1
			if d[guest[j - 1]] == 2:
				arg += 2
			elif d[guest[j - 1]] > 2:
				arg += 1
			dp[i] = min(dp[i], cost + arg + dp[j - 1])
	print(dp[n])


for t in range(int(input())):
	(n, c) = list(map(int, input().split()))
	g = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp[0] = 0
	dp[1] = c
	for i in range(2, len(dp)):
		d = {}
		arg = 0
		dp[i] = c + dp[i - 1]
		for j in range(i, 0, -1):
			d[g[j - 1]] = d.get(g[j - 1], 0) + 1
			if d[g[j - 1]] == 2:
				arg += 2
			elif d[g[j - 1]] > 2:
				arg += 1
			dp[i] = min(dp[i], c + arg + dp[j - 1])
	print(dp[n])


t = int(input())
for testcase in range(t):
	(n, cost) = list(map(int, input().split()))
	guests = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp[0] = 0
	dp[1] = cost
	for i in range(2, len(dp)):
		freq_dict = {}
		arg = 0
		dp[i] = cost + dp[i - 1]
		for j in range(i, 0, -1):
			freq_dict[guests[j - 1]] = freq_dict.get(guests[j - 1], 0) + 1
			if freq_dict[guests[j - 1]] == 2:
				arg += 2
			elif freq_dict[guests[j - 1]] > 2:
				arg += 1
			dp[i] = min(dp[i], cost + arg + dp[j - 1])
	print(dp[n])


tea = int(input())
for i in range(tea):
	(number, cost) = list(map(int, input().split()))
	guest = list(map(int, input().split()))
	deep = [0] * (number + 1)
	deep[0] = 0
	deep[1] = cost
	for i in range(2, len(deep)):
		d = {}
		arg = 0
		deep[i] = cost + deep[i - 1]
		for j in range(i, 0, -1):
			d[guest[j - 1]] = d.get(guest[j - 1], 0) + 1
			if d[guest[j - 1]] == 2:
				arg += 2
			elif d[guest[j - 1]] > 2:
				arg += 1
			deep[i] = min(deep[i], cost + arg + deep[j - 1])
	print(deep[number])


p = int(input())
for _ in range(p):
	(n, co) = list(map(int, input().split()))
	gu = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp[0] = 0
	dp[1] = co
	for i in range(2, len(dp)):
		d = {}
		arg = 0
		dp[i] = co + dp[i - 1]
		for j in range(i, 0, -1):
			d[gu[j - 1]] = d.get(gu[j - 1], 0) + 1
			if d[gu[j - 1]] == 2:
				arg = arg + 2
			elif d[gu[j - 1]] > 2:
				arg = arg + 1
			dp[i] = min(dp[i], co + arg + dp[j - 1])
	print(dp[n])


h = int(input())
for i in range(h):
	(h, cost) = list(map(int, input().split()))
	guest = list(map(int, input().split()))
	di = [0] * (h + 1)
	di[0] = 0
	di[1] = cost
	for i in range(2, len(di)):
		d = {}
		arg = 0
		di[i] = cost + di[i - 1]
		for j in range(i, 0, -1):
			d[guest[j - 1]] = d.get(guest[j - 1], 0) + 1
			if d[guest[j - 1]] == 2:
				arg += 2
			elif d[guest[j - 1]] > 2:
				arg += 1
			di[i] = min(di[i], cost + arg + di[j - 1])
	print(di[h])