Read problems statements in Mandarin Chinese  and Russian. 

Rupsa recently started to intern under Chef. He gave her N type of ingredients of varying quantity  A_{1}, A_{2}, ..., A_{N} respectively to store it. But as she is lazy to arrange them she puts them all in a storage box.

Chef comes up with a new recipe and decides to prepare it. He asks Rupsa to get two units of each type ingredient for the dish. But when she went to retrieve the ingredients, she realizes that she can only pick one item at a time from the box and can know its type only after she has picked it out. The picked item is not put back in the bag.

She, being lazy, wants to know the maximum number of times she would need to pick items from the box in the worst case so that it is guaranteed that she gets at least two units of each type of ingredient. If it is impossible to pick items in such a way, print -1.

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases.		
The first line of each test case contains a single integer N denoting the number of different type of ingredients.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the quantity of each ingredient.
 
------ Output ------ 

For each test case, output a single line containing an integer denoting the answer corresponding to that test case.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{4}$

------ Sub tasks ------ 

$Subtask #1: 1 ≤ N ≤ 1000 (30 points)$
$Subtask #2: original constraints (70 points)$

----- Sample Input 1 ------ 
2

2

2 2

1

6
----- Sample Output 1 ------ 
4

2
----- explanation 1 ------ 

In Example 1, she need to pick up all items.
In Example 2, since there is only one type of ingredient, picking two items is enough.

Difficulty: EASY
Tags: Constructive algorithms

Mathematics

Ad-hoc

Starter code:

Solutions:
T = int(input())
for i in range(T):
	N = int(input())
	A = [int(n) for n in input().split()]
	if 1 in A:
		print(-1)
		continue
	A_sorted = sorted(A)
	print(sum(A_sorted) + 2 - A_sorted[0])


for _ in range(int(input())):
	items = int(input())
	A = list(map(int, input().split()))
	c = sum(A)
	c -= min(A)
	c += 2
	if min(A) == 1:
		print(-1)
	else:
		print(c)


test_case = int(input())
for _ in range(test_case):
	n = int(input())
	arr = list(map(int, input().split()))
	if 1 not in arr:
		summ = sum(arr) - min(arr) + 2
		print(summ)
	else:
		print(-1)


T = int(input())
for t in range(T):
	N = int(input())
	minv = 1001
	ls = list(map(int, input().split()))
	B = 1
	for i in ls:
		if i == 1:
			B = 0
			break
		elif minv > i:
			minv = i
	if B == 1:
		print(sum(ls) - minv + 2)
	else:
		print('-1')


t = int(input())
for i in range(t):
	n = int(input())
	Cannot_pick = 0
	ar = list(map(int, input().split(' ')))
	for ele in ar:
		if ele < 2:
			Cannot_pick = 1
			break
	if Cannot_pick != 1:
		if ar.count(2) == n or n == 1:
			print(n * 2)
		else:
			ar = sorted(ar)
			if ar[0] == 2:
				print(sum(ar))
			else:
				ar[0] = 2
				print(sum(ar))
	else:
		print('-1')


a = int(input(''))
for i in range(a):
	T1 = int(input(''))
	T = [int(x) for x in input('').split()]
	if min(T) < 2:
		print(-1)
	else:
		print(sum(T) - min(T) + 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	rarest = min(a)
	if rarest == 1:
		print(-1)
	else:
		print(sum(a) - rarest + 2)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in a:
		if i < 2:
			print(-1)
			ans = 1
			break
	if ans == 1:
		continue
	s = sum(a)
	m = min(a)
	s -= m
	s += 2
	print(s)


def maxIng(d):
	mini = 100001
	t = 0
	totalCount = 0
	for i in range(len(d)):
		totalCount += d[i]
		if d[i] < mini:
			mini = d[i]
			t = i
	if mini < 2:
		return -1
	else:
		return totalCount - mini + 2
T = int(input())
while T:
	T -= 1
	N = int(input())
	d = [int(i) for i in input().split()]
	print(maxIng(d))


T = int(input())
for i in range(T):
	N = int(input())
	s = list(map(int, input().split()))
	t = min(s)
	if t < 2:
		print(-1)
	else:
		print(sum(s) - t + 2)


t = int(input())
for val in range(0, t):
	n = int(input())
	k = list(map(int, input().split()))
	s = 0
	for v in range(0, len(k)):
		if k[v] < 2:
			s = 1
			break
	if s == 1:
		print(-1)
	else:
		k.sort()
		sump = sum(k[1:len(k)])
		print(sump + 2)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = 0
	if len(c) == 1:
		print(2)
		continue
	if min(c) == 1:
		print(-1)
		continue
	else:
		print(sum(c) - min(c) + 2)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	if m < 2:
		print(-1)
	else:
		print(sum(l) - m + 2)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	mini = 1000000
	sumi = 0
	for i in range(len(l)):
		if l[i] < 2:
			flag = 1
		if mini > l[i]:
			mini = l[i]
		sumi = sumi + l[i]
	if flag == 1:
		print(-1)
	else:
		print(sumi - mini + 2)
	t -= 1


T = int(input())
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	q = 0
	m = min(l)
	for i in l:
		if i < 2:
			print(-1)
			q = 1
			break
		s += i
	if q == 0:
		s -= m
		s += 2
		print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	flag = 0
	for i in a:
		if i < 2:
			flag = 1
			break
		else:
			s = s + i
	if flag == 1:
		print(-1)
	else:
		print(s - min(a) + 2)


t = int(input())
while t >= 1:
	n = input()
	c = input()
	c = c.split()
	arr1 = []
	sum = 0
	d = 0
	for i in range(len(c)):
		g = int(c[i])
		if g < 2:
			d = 1
		arr1.append(g)
	arr1.sort()
	i = len(arr1) - 1
	while i >= 0:
		sum = sum + arr1[i]
		i = i - 1
	numberofpairs = 0
	count = 0
	assist = 0
	f = 0
	if d == 1:
		print('-1')
	else:
		print(sum - arr1[0] + 2)
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	for i in range(len(l)):
		if l[i] < 2:
			flag = 1
			break
	if flag == 1:
		print(-1)
		continue
	s = sum(l) - min(l) + 2
	if len(l) == 1:
		print(2)
	else:
		print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if any((i < 2 for i in a)):
		print(-1)
	else:
		x = min(a)
		a.remove(x)
		if len(a) > 0:
			print(sum(a) + 2)
		elif len(a) == 0:
			print(2)


t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	minFrq = 1000000000.0
	for i in range(0, n):
		sum += a[i]
		minFrq = min(minFrq, a[i])
	if minFrq == 1:
		print(-1)
	else:
		print(sum - minFrq + 2)


t = int(input())
for loop in range(t):
	n = int(input())
	quant = list(map(int, input().split()))
	quant.sort(reverse=True)
	flag = True
	for i in range(len(quant)):
		if quant[i] < 2:
			flag = False
			break
	if flag == False:
		print(-1)
	else:
		ans = 2
		ans += sum(quant[0:len(quant) - 1])
		print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	m = 9999999999999
	for i in l:
		sum += i
		m = min(m, i)
	if m == 1:
		print(-1)
	elif n == 1:
		print(2)
	elif n >= 2:
		sum += 2 - m
		print(sum)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	if min(a) <= 1:
		print(-1)
	else:
		print(sum(a) - min(a) + 2)


def snek(l, n):
	(s, m) = (0, 100000000.0)
	for j in l:
		s += j
		m = min(m, j)
	if m == 1:
		print(-1)
	else:
		print(s - m + 2)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	snek(l, n)


t = int(input())
for i in range(0, t):
	n = int(input())
	l = [int(j) for j in input().split()]
	sum = 0
	l.sort()
	flag = 0
	for j in range(1, n):
		if l[j] < 2:
			flag = 1
			break
		else:
			sum += l[j]
	if flag == 1:
		print(-1)
	elif l[0] >= 2:
		print(sum + 2)
	else:
		print(-1)


n = int(input())
for i in range(n):
	x = int(input())
	a = list(map(int, input().split()))
	a.sort()
	a.reverse()
	sums = 0
	count = 0
	for i in range(x):
		if a[i] >= 2:
			sums += a[i]
		else:
			count = 1
			break
	if count == 0:
		print(sums - min(a) + 2)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	if n == 1:
		if l[0] >= 2:
			print(2)
		else:
			print(-1)
	else:
		l.sort(reverse=True)
		for i in range(n - 1):
			if l[i] >= 2:
				count += l[i]
			else:
				print(-1)
				break
		else:
			if l[n - 1] >= 2:
				count += 2
				print(count)
			else:
				print(-1)


t = int(input())
for p in range(t):
	n = int(input())
	inp = input()
	l = [int(x) for x in inp.split()]
	if 1 in l:
		print(-1)
	else:
		print(sum(l) - min(l) + 2)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = a.copy()
	b.sort(reverse=1)
	l = a.count(1)
	if l >= 1:
		print(-1)
	else:
		o = sum(b)
		print(o - (b[n - 1] - 2))
	t = t - 1


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	z = min(a)
	if z < 2:
		print(-1)
	else:
		print(sum(a) - min(a) + 2)


for t in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	li.sort()
	sm = sum(li[1:])
	if li[0] <= 1:
		print(-1)
	else:
		sm += 2
		print(sm)


t = int(input())
for _ in range(0, t):
	input()
	l = list(map(int, input().split()))
	l.sort()
	l.reverse()
	su = 0
	f = 0
	for i in range(len(l)):
		if l[i] < 2:
			f = 1
			break
		else:
			su = su + l[i]
	if f == 1:
		print('-1')
	else:
		print(su - min(l) + 2)


tests = int(input())
for i in range(tests):
	length = int(input())
	arr = list(map(int, input().split()))
	minimum = min(arr)
	if minimum < 2:
		print(-1)
	else:
		print(sum(arr) - minimum + 2)


def solve(arr, n):
	(sum, miN) = (0, 100000000.0)
	for i in arr:
		sum += i
		miN = min(miN, i)
	if miN == 1:
		print(-1)
	else:
		print(sum - miN + 2)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	solve(arr, n)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	Amin = min(A)
	if Amin < 2:
		print(-1)
	else:
		print(sum(A) - Amin + 2)


for _ in range(int(input())):
	n = input()
	arr = [int(x) for x in input().split()]
	arr.sort()
	if arr[0] < 2:
		print(-1)
	else:
		print(sum(arr) - arr[0] + 2)


for e in range(int(input())):
	n = int(input())
	s = 0
	l = list(map(int, input().split()))
	l.sort()
	if l[0] == 1:
		print(-1)
	else:
		s += sum(l[1:n])
		print(s + 2)


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	if min(ar) <= 1:
		print(-1)
	else:
		print(sum(ar) - min(ar) + 2)


for _ in range(int(input())):
	N = int(input())
	array = list(map(int, input().split()))
	p = min(array)
	if p < 2:
		print(-1)
	else:
		print(sum(array) - p + 2)


t = input()
for c in range(0, int(t)):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	a.sort()
	if a[0] < 2:
		print(-1)
	else:
		z = sum(a) - a[0] + 2
		print(z)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort(reverse=True)
	if l[-1] < 2:
		print(-1)
	else:
		print(sum(l[:n - 1]) + 2)


def solve(A, N):
	(sum, minn) = (0, 100000)
	for i in A:
		sum += i
		minn = min(minn, i)
	if minn == 1:
		print(-1)
	else:
		print(sum - minn + 2)
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	solve(A, N)


T = int(input().strip())
for _ in range(T):
	N = int(input().strip())
	items = list(map(int, input().strip().split()))
	if 1 in items:
		print(-1)
	else:
		a = min(items)
		picks = sum(items) - a + 2
		print(picks)


t = int(input())
while t:
	n = int(input())
	flag = 0
	arr = [int(x) for x in input().split()]
	arr.sort()
	sum = 0
	for i in arr:
		if i < 2:
			flag = 1
			break
		sum = sum + i
	sum = sum + 2 - arr[0]
	if flag == 1:
		print('-1')
	else:
		print(sum)
	t = t - 1


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mina = min(a)
	if mina == 1:
		ans = -1
	else:
		ans = sum(a) - (mina - 2)
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))[:n]
	a.sort()
	if a[0] < 2:
		print(-1)
	else:
		z = sum(a) - a[0] + 2
		print(z)
	t = t - 1


try:
	n = int(input())
	for i in range(0, n):
		j = int(input())
		l = list(map(int, input().split()))
		if len(l) == 1:
			print(2)
			continue
		elif min(l) == 1:
			print(-1)
			continue
		else:
			print(sum(l) - min(l) + 2)
except EOFError as e:
	print('')


for i in range(int(input())):
	N = int(input())
	l = list(map(int, input().split()))
	if 1 in l:
		print(-1)
	else:
		x = sum(l) - min(l) + 2
		print(x)


t = int(input())
for tc in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = sum(arr)
	m = min(arr)
	if m == 1:
		print(-1)
	else:
		print(s - m + 2)


t = int(input())
for i in range(t):
	n = int(input())
	N = list(map(int, input().split()))
	if len(N) == 1:
		print(2)
		continue
	if min(N) == 1:
		print(-1)
		continue
	else:
		print(sum(N) - min(N) + 2)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().split(' ')))
	total = sum(arr)
	if min(arr) < 2:
		print('-1')
	else:
		print(total - min(arr) + 2)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	total = 0
	a.sort()
	if 1 in a:
		print(-1)
	elif n == 1 and a[0] > 1:
		print(2)
	else:
		for x in range(n - 1, 0, -1):
			total += a[x]
		total += 2
		print(total)


t = int(input())
for ni in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	if 1 in l:
		print(-1)
	else:
		m = min(l)
		print(sum(l) - m + 2)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	q = 0
	m = min(l)
	for i in l:
		if i < 2:
			print(-1)
			q = 1
			break
		s += i
	if q == 0:
		s -= m
		s += 2
		print(s)
	t -= 1


class Solution(object):

	def __init__(self):
		for _ in range(int(input())):
			self.get_input()
			self.solve()
			self.print_ans()
	pass

	def get_input(self):
		self.n = int(input())
		self.a = list(map(int, input().split()))
		pass

	def solve(self):
		self.ans = -1
		if len(self.a) == 1:
			if self.a[0] >= 2:
				self.ans = 2
			else:
				self.ans = -1
			return
		if self.a.count(1):
			return
		self.ans = sum(self.a) - min(self.a) + 2
		pass

	def print_ans(self):
		print(self.ans)
		pass
Solution()


t = int(input())
for a00 in range(t):
	n = int(input())
	ci = [int(_) for _ in input().strip().split()]
	print(sum(ci) - min(ci) + 2 if min(ci) - 2 >= 0 else '-1')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	for i in a:
		if i < 2:
			f = 1
			break
	if f == 1:
		print(-1)
		continue
	print(sum(a) - (min(a) - 2))


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	if min(li) < 2:
		print(-1)
		continue
	else:
		print(sum(li) - (min(li) - 2))


for _ in range(int(input())):
	n = int(input())
	items = list(map(int, input().split()))
	if min(items) < 2:
		total = -1
	else:
		total = sum(items) - min(items) + 2
	print(total)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	if 1 in l:
		print(-1)
	elif min(l) == 2:
		print(sum(l))
	else:
		print(sum(l) - min(l) + 2)