There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.

For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.

You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.

The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) — the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).

The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.


-----Output-----

For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).


-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7

Output
4
10
4
0



-----Note-----

In the first test case of the example, the optimal sequence of moves can be as follows:

  before making moves $a=[40, 6, 40, 3, 20, 1]$;  choose $c=6$;  now $a=[40, 3, 40, 3, 20, 1]$;  choose $c=40$;  now $a=[20, 3, 20, 3, 20, 1]$;  choose $c=20$;  now $a=[10, 3, 10, 3, 10, 1]$;  choose $c=10$;  now $a=[5, 3, 5, 3, 5, 1]$ — all numbers are odd. 

Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.

Source: codeforces | Difficulty: EASY
Tags: Number theory

Greedy algorithms


Solutions:
tests = int(input())
for test in range(tests):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = {}
	for i in range(n):
		s = 0
		while a[i] % 2 == 0:
			a[i] //= 2
			s += 1
		if a[i] in list(d.keys()):
			d[a[i]] = max(s, d[a[i]])
		else:
			d[a[i]] = s
	s = 0
	for i in list(d.keys()):
		s += d[i]
	print(s)


t = int(input())
for g in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list()
	for i in range(n):
		while a[i] % 2 == 0:
			b.append(a[i])
			a[i] = a[i] // 2
	b.sort()
	count = 1
	for i in range(len(b) - 1):
		if b[i] != b[i + 1]:
			count += 1
	if len(b) == 0:
		print(0)
	else:
		print(count)


t = int(input())

def power(n):
	res = 0
	while n % 2 == 0:
		res += 1
		n //= 2
	if n not in d:
		d[n] = 0
	d[n] = max(d[n], res)
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxx = 0
	d = {}
	for num in a:
		power(num)
	print(sum(list(d.values())))


for _ in range(int(input())):
	d = dict()
	N = int(input())
	a = list(map(int, input().split()))
	for i in range(N):
		c = 0
		tmp = a[i]
		while tmp % 2 != 1:
			tmp = tmp // 2
			c += 1
		if tmp in d:
			d[tmp] = max(d[tmp], c)
		else:
			d[tmp] = c
	res = 0
	for i in list(d.keys()):
		res += d[i]
	print(res)


from collections import defaultdict

def f(n):
	st = 0
	while n % 2 == 0:
		n //= 2
		st += 1
	return (n, st)
t = int(input())
for _ in range(t):
	n = int(input())
	d = defaultdict(int)
	for i in input().split():
		el = int(i)
		(os, st) = f(el)
		d[os] = max(d[os], st)
	s = 0
	for el in list(d.values()):
		s += el
	print(s)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	ans = 0
	for i in range(n):
		k = a[i]
		while k % 2 == 0 and k not in s:
			s.add(k)
			k = k // 2
			ans += 1
	print(ans)


t = int(input())
for g in range(t):
	n = int(input())
	st = set()
	a = [int(i) for i in input().split()]
	for i in range(n):
		q = a[i]
		while q % 2 == 0:
			st.add(q)
			q //= 2
	print(len(st))


def f(x):
	tmp = x
	z = 0
	while tmp % 2 == 0:
		tmp //= 2
		z += 1
	return [tmp, z]
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sl = dict()
	for x in a:
		(y, z) = f(x)
		if sl.get(y) == None:
			sl[y] = z
		else:
			sl[y] = max(sl[y], z)
	ans = 0
	for x in sl.keys():
		ans += sl[x]
	print(ans)


for q in range(int(input())):
	n = int(input())
	line = list(map(int, input().split()))
	Q = dict()
	for i in range(n):
		l = 0
		r = 100
		while r - l > 1:
			m = (l + r) // 2
			if line[i] % (1 << m) == 0:
				l = m
			else:
				r = m
		f = line[i] // (1 << l)
		if f in Q:
			Q[f] = max(Q[f], l)
		else:
			Q[f] = l
	Q = list(Q.items())
	ans = 0
	for (a, b) in Q:
		ans += b
	print(ans)


import heapq
import sys
input = lambda : sys.stdin.readline()
for i in range(int(input())):
	n = int(input())
	s = set()
	h = []
	for i in map(int, input().split()):
		if i % 2 == 0:
			if i in s:
				continue
			s.add(i)
			heapq.heappush(h, -i)
	ans = 0
	while h:
		i = -heapq.heappop(h) // 2
		ans += 1
		if i % 2 == 0:
			if i in s:
				continue
			s.add(i)
			heapq.heappush(h, -i)
	print(ans)


t = int(input())
for _ in range(t):
	used_q = set()
	n = int(input())
	nums = list(map(int, input().split(' ')))
	for i in range(len(nums)):
		q = nums[i]
		while q % 2 == 0:
			if q in used_q:
				q = q // 2
			else:
				used_q.add(q)
				q = q // 2
	print(len(used_q))


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	dell = []
	for i in range(n):
		new = 0
		while A[i] % 2 != 1:
			A[i] //= 2
			new += 1
		dell.append([A[i], new])
	dicter = {}
	for el in dell:
		if el[1] > dicter.get(el[0], -1):
			dicter[el[0]] = el[1]
	ans = 0
	for el in dicter:
		ans += dicter[el]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = dict()
	for j in range(n):
		if a[j] % 2 == 0:
			b[a[j]] = b.get(a[j], 0) + 1
	k = 0
	for key in b:
		c = key
		while c % 2 == 0:
			k += 1
			c = c // 2
			if c in b.keys():
				break
	print(k)


t = int(input())
for r in range(t):
	q = input()
	a = list(map(int, input().split()))
	d = dict()
	for w in a:
		s = 0
		while w % 2 == 0:
			w //= 2
			s += 1
		if w in list(d.keys()):
			d[w] = max([d[w], s])
		else:
			d[w] = s
	e = 0
	for w in list(d.keys()):
		e += d[w]
	print(e)


import sys
import math
import heapq

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return list(map(int, tinput()))

def rlinput():
	return list(rinput())

def main():
	(n, w, q, res) = (iinput(), set(), [], 0)
	for i in rinput():
		if i % 2 == 0:
			if i not in w:
				w.add(i)
				heapq.heappush(q, -i)
	while q:
		i = -heapq.heappop(q) // 2
		res += 1
		if i % 2 == 0:
			if i not in w:
				w.add(i)
				heapq.heappush(q, -i)
	print(res)
for i in range(iinput()):
	main()


for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ar1 = []
	ar2 = []
	for elem in ar:
		num = 0
		while elem % 2 == 0:
			elem //= 2
			num += 1
		ar1.append(num)
		ar2.append(elem)
	ar3 = []
	for i in range(n):
		ar3.append([ar2[i], ar1[i]])
	ar3.sort()
	i = 1
	j = 1
	num = 1
	ans = sum(ar1)
	while i < n:
		while j < n and ar3[j][0] == ar3[j - 1][0]:
			j += 1
		times = j - i
		prev_val = 0
		for h in range(i - 1, min(j, n)):
			ans -= times * (ar3[h][1] - prev_val)
			times -= 1
			prev_val = ar3[h][1]
		i = j + 1
		j = i
	print(ans)


a = int(input())
for i in range(a):
	s1 = set()
	ans = 0
	l = input()
	now = input().split()
	for i in now:
		k = int(i)
		while k % 2 == 0 and k not in s1:
			s1.add(k)
			k = k // 2
	print(len(s1))


def main():
	m = int(input())
	for i in range(m):
		n = int(input())
		nums = map(int, input().split())
		arr = {}
		for j in nums:
			base = j
			step = 0
			while not base & 1:
				base >>= 1
				step += 1
			if not base in arr:
				arr[base] = step
			else:
				arr[base] = max(arr[base], step)
		print(sum(arr.values()))

def __starting_point():
	main()
__starting_point()


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	d = {}
	for el1 in m:
		el = el1
		c = 0
		while el % 2 == 0:
			el //= 2
			c += 1
		if el in list(d.keys()):
			d[el] = max(d[el], c)
		else:
			d[el] = c
	s = 0
	for el in d:
		s += d[el]
	ans.append(s)
for el in ans:
	print(el)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = set()
	s1 = set(a)
	ans = 0
	l = n
	while l > 0:
		now = a.pop()
		l -= 1
		if now not in s and now % 2 == 0:
			s.add(now)
			ans += 1
			if now // 2 not in s1:
				s1.add(now // 2)
				a.append(now // 2)
				l += 1
	print(ans)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for j in range(0, n):
		if a[j] % 2 == 0:
			num = 0
			k = a[j]
			while k % 2 == 0:
				k //= 2
				num += 1
			b.append([k, num])
	b.sort()
	ans = 0
	length = len(b)
	for q in range(0, length - 1):
		if b[q][0] != b[q + 1][0]:
			ans += b[q][1]
	if length != 0:
		print(ans + b[length - 1][1])
	else:
		print(ans)


k = int(input())

def absolute():
	c = dict()
	m = 0
	for i in [int(x) for x in input().split()]:
		q = 0
		if i % 2 != 0:
			continue
		while i % 2 == 0:
			i //= 2
			q += 1
		if c.get(i, 0) < q:
			m += q - c.get(i, 0)
			c[i] = q
	return m
for j in range(k):
	input()
	print(absolute())


def ck(a):
	ans = 0
	while a % 2 == 0:
		a = a // 2
		ans += 1
	return [a, ans]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = {}
	for i in range(n):
		(x, y) = ck(a[i])
		if c.get(x) == None:
			c[x] = y
		elif c.get(x) < y:
			c[x] = y
	ans = sum(c.values())
	print(ans)


def res(e):
	ans = 0
	e1 = int(e)
	while e1 % 2 == 0:
		e1 //= 2
		ans += 1
	return 2 ** ans
for i in range(int(input())):
	n = int(input())
	s = list([x for x in list(map(int, input().split())) if x % 2 == 0])
	if len(s) == 0:
		print(0)
	else:
		temp = list([x // res(x) for x in s])
		ans = 0
		s1 = set()
		while temp != s:
			for i1 in range(len(s)):
				if temp[i1] == s[i1]:
					continue
				elif temp[i1] not in s1:
					s1.add(temp[i1])
					ans += 1
					temp[i1] *= 2
				elif temp[i1] in s1:
					temp[i1] *= 2
		print(ans)


t = int(input())
for i in range(0, t):
	n = int(input())
	data = list(map(int, input().split()))
	d = dict()
	for j in range(0, n):
		a = data[j]
		count = 0
		while a % 2 == 0:
			a = a // 2
			count += 1
		d[a] = max(d.get(a, 0), count)
	print(sum(d.values()))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set()
	d = {}
	for a in l:
		j = 0
		while a % 2 == 0:
			a = a // 2
			j += 1
		s.add(a)
		if a in d:
			if d[a] < j:
				d[a] = j
		else:
			d[a] = j
	p = 0
	for q in d:
		p += d[q]
	print(p)


from sys import stdin as s
for i in range(int(s.readline())):
	n = int(s.readline())
	l = sorted([i for i in set(map(int, s.readline().split())) if i % 2 == 0], reverse=True)
	t = set()
	c = 0
	for i in l:
		if i not in t:
			t.add(i)
			while i % 2 == 0:
				i //= 2
				t.add(i)
				c += 1
	print(c)


from collections import Counter

def primfacs(n):
	if n % 2 == 0:
		primfac = [0, 0]
	else:
		primfac = [0, 0]
	while n % 2 == 0:
		n = n / 2
		primfac[0] += 1
	primfac[1] = n
	return primfac
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	Ost = []
	for j in range(n):
		Ost.append(primfacs(A[j]))
	Ost.sort()
	d = {}
	for j in range(len(Ost)):
		d[Ost[j][1]] = Ost[j][0]
	print(sum(list(d.values())))


t = int(input())
for i in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	even_numbers = {x for x in a if x % 2 == 0}
	used_numbers = set()
	k = 0
	for x in even_numbers:
		while x % 2 == 0 and x not in used_numbers:
			used_numbers.add(x)
			x //= 2
			k += 1
	print(k)


from collections import Counter
import heapq
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	rep = Counter()
	ans = 0
	heap = []
	for i in range(len(a)):
		rep[a[i]] += 1
		if rep[a[i]] == 1:
			heapq.heappush(heap, -a[i])
	while heap:
		x = -heapq.heappop(heap)
		if x % 2 == 0:
			dx = x // 2
			if rep[dx] == 0:
				heapq.heappush(heap, -dx)
				rep[dx] = 1
			else:
				rep[dx] += rep[x]
			ans += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list([bin(int(x))[2:] for x in input().split()])
	d = dict()
	for i in a:
		ir = i.rfind('1')
		c = len(i) - ir - 1
		raw = int(i[:ir + 1], base=2)
		d[raw] = max(d.get(raw, c), c)
	print(sum(d.values()))


def factorize(x):
	tmp = x
	cnt = 0
	while tmp % 2 == 0:
		tmp //= 2
		cnt += 1
	return (tmp, cnt)
n = int(input())
for i in range(n):
	k = int(input())
	x = dict()
	cnt = 0
	tmp = list(map(int, input().split()))
	for j in tmp:
		(g, v) = factorize(j)
		try:
			x[g] = max(x[g], v)
		except:
			x[g] = v
	for c in list(x.keys()):
		cnt += x[c]
	print(cnt)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	used = set()
	for j in a:
		if j % 2 == 1:
			continue
		while j % 2 == 0 and j not in used:
			used.add(j)
			j /= 2
	print(len(used))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = dict()
	su = 0
	for i in a:
		k = 0
		while i % 2 == 0:
			i = i // 2
			k += 1
		if i not in d:
			d[i] = k
		else:
			d[i] = max(d[i], k)
	for i in list(d.values()):
		su += i
	print(su)


t = int(input())
for i in range(t):
	ans = 0
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		count = 0
		while a[j] % 2 == 0:
			a[j] = a[j] // 2
			count += 1
		a[j] = [a[j], count]
	a.sort()
	j = 0
	while j != n:
		m = a[j][1]
		while j + 1 < n and a[j][0] == a[j + 1][0]:
			m = max([a[j][1], a[j + 1][1]])
			j += 1
		j += 1
		ans += m
	print(ans)


n = int(input())
for i in range(n):
	answer = 0
	d = set()
	m = int(input())
	arr = [int(x) for x in input().split()]
	for j in arr:
		if j % 2 == 0:
			if j not in d:
				d.add(j)
	s = list(d)
	s.sort(reverse=True)
	for j in s:
		ch = j // 2
		answer += 1
		while ch % 2 == 0:
			if ch not in d:
				ch //= 2
				answer += 1
			else:
				break
	print(answer)


t = int(input())
ans_l = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ar = set()
	for i in a:
		if i % 2 == 0:
			x = i
			ar.add(x)
			while x % 2 == 0:
				ar.add(x)
				x //= 2
	ans_l.append(len(ar))
print(*ans_l, sep='\n')


a = int(input())
for i in range(a):
	f = int(input())
	k = list(map(int, input().split()))
	l = set()
	ch = 0
	lol = 0
	for i in range(len(k)):
		lol = k[i]
		while lol % 2 == 0:
			l.add(lol)
			lol /= 2
	print(len(l))


def razl(a):
	if a % 2 == 0:
		r = [0, 0]
	else:
		r = [0, 0]
	while a % 2 == 0:
		a = a / 2
		r[0] += 1
	r[1] = a
	return r
ans = []
for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = []
	for j in range(a):
		c.append(razl(b[j]))
	c.sort()
	d = {}
	for j in range(len(c)):
		d[c[j][1]] = c[j][0]
	ans.append(sum(list(d.values())))
for i in ans:
	print(i)


def f(n):
	minn = 0
	maxx = 30
	mid = 10
	while mid != minn:
		if n // 2 ** mid == n / 2 ** mid:
			minn = mid
			mid = (minn + maxx) // 2
		else:
			maxx = mid
			mid = (minn + maxx) // 2
	return mid
t = int(input())
for i in range(t):
	d = dict()
	n = int(input())
	a = set(map(int, input().split()))
	for j in a:
		p = f(j)
		if j // 2 ** p in d:
			if p > d[j // 2 ** p]:
				d[j // 2 ** p] = p
		else:
			d[j // 2 ** p] = p
	print(sum(d.values()))


def ans():
	nonlocal lst
	d = dict()
	for i in lst:
		(s2, delit) = st2(i)
		if delit not in d:
			d[delit] = s2
			continue
		if d[delit] < s2:
			d[delit] = s2
	return sum(d.values())

def st2(num):
	c = 0
	while num % 2 == 0 and num != 0:
		num = num >> 1
		c += 1
	return [c, num]
lst = []
for i in range(int(input())):
	t = int(input())
	lst = list(map(int, input().split()))
	print(ans())


t = int(input())
answers = [0] * t
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	arr = [[] for _ in range(n)]
	ans = 0
	for j in range(n):
		pow1 = 0
		cur = a[j]
		while cur % 2 == 0:
			cur //= 2
			pow1 += 1
		arr[j] = [cur, pow1]
	arr.sort(reverse=True)
	cur_nech = -1
	for j in range(n):
		if arr[j][0] != cur_nech:
			ans += arr[j][1]
			cur_nech = arr[j][0]
	answers[i] = ans
print(*answers, sep='\n')