You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty.
Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once.

-----Input-----
- The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
- The first line of each test case contains a single integer N$N$.
- The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$.

-----Output-----
For each test case, print a single line containing one integer ― the minimum required number of modifications.

-----Constraints-----
- 1≤T≤1,000$1 \le T \le 1,000$
- 1≤N≤106$1 \le N \le 10^6$
- 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$
- the sum of N$N$ over all test cases does not exceed 106$10^6$

-----Subtasks-----
Subtask #1 (50 points):
- 1≤N≤1,000$1 \le N \le 1,000$
- the sum of N$N$ over all test cases does not exceed 10,000$10,000$
Subtask #2 (50 points): original constraints

-----Example Input-----
2
5
1 4 1 2 2
4
2 3 2 3

-----Example Output-----
1
2

Difficulty: VERY_HARD
Tags: Dynamic programming

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cost = 0
	dp = [[] for val in range(2 * n + 1)]
	cnt = [0] * (n * 2 + 1)
	dp[0].extend([0 for val in range(n * 2 + 1)])
	for val in a:
		if val <= 2 * n:
			cnt[val] += 1
		else:
			cost += 1
	for i in range(1, n * 2 + 1):
		rn = n * 2 // i
		for j in range(0, rn + 1):
			dp[i].extend([dp[i - 1][j] + abs(cnt[i] - j)])
		for j in range(rn - 1, -1, -1):
			dp[i][j] = min(dp[i][j], dp[i][j + 1])
	print(cost + min(dp[2 * n][0], dp[2 * n][1]))


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	li.sort()
	dli = dict()
	modi = 0
	for i in li:
		if i not in dli:
			dli[i] = li.count(i)
	if len(dli) != 0:
		while 1:
			tmp = []
			for i in dli:
				if dli[i] == 0:
					continue
				tmp.append(i)
				dli[i] -= 1
			l = len(tmp)
			mn = l
			for i in range(l):
				mn = min(mn, tmp[i] - 1 - i + l - 1 - i)
			modi += mn
			if l == 0:
				break
	print(modi)


T = int(input())
for _ in range(T):
	n = int(input())
	l = [int(e) for e in input().split()]
	difNum = dict()
	for e in l:
		difNum[e] = difNum.get(e, 0) + 1
	minAcum = 0
	difOrd = [[e, difNum[e]] for e in sorted(difNum.keys())]
	while len(difOrd) > 0:
		ant = 0
		addAct = 0
		actLen = len(difOrd)
		minAct = actLen
		era = []
		for (i, k) in enumerate(difOrd):
			addAct = k[0] - (ant + 1) + addAct
			ant = k[0]
			difOrd[i][1] -= 1
			if difOrd[i][1] == 0:
				era.append(i)
			minAct = min(minAct, addAct + actLen - (i + 1))
		difOrd = [[e[0], e[1]] for e in difOrd if e[1] > 0]
		minAcum += minAct
	print(min(minAcum, n))


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	set = [[]]
	a.sort()
	i = 0
	prev = 0
	for c in a:
		if c == prev:
			i += 1
			if len(set) <= i:
				set.append([])
			set[i].append(c)
		else:
			i = 0
			set[i].append(c)
		prev = c
	ans = 0
	for s in set:
		if s[-1] == len(s):
			continue
		j = 0
		remove = len(s)
		add = 0
		min = len(s)
		i = 0
		last = s[-1]
		while i < last:
			i += 1
			if s[j] != i:
				add += s[j] - i
				remove -= 1
				i = s[j]
				j += 1
			else:
				j += 1
				add += 0
				remove -= 1
			modifications = add + remove
			if modifications < min:
				min = modifications
			if min == 0:
				break
		ans += min
	print(ans)