You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $x$ that occurs in the array $2$ or more times. Take the first two occurrences of $x$ in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, $2 \cdot x$).

Determine how the array will look after described operations are performed.

For example, consider the given array looks like $[3, 4, 1, 2, 2, 1, 1]$. It will be changed in the following way: $[3, 4, 1, 2, 2, 1, 1]~\rightarrow~[3, 4, 2, 2, 2, 1]~\rightarrow~[3, 4, 4, 2, 1]~\rightarrow~[3, 8, 2, 1]$.

If the given array is look like $[1, 1, 3, 1, 1]$ it will be changed in the following way: $[1, 1, 3, 1, 1]~\rightarrow~[2, 3, 1, 1]~\rightarrow~[2, 3, 2]~\rightarrow~[3, 4]$.


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 150\,000$) — the number of elements in the array.

The second line contains a sequence from $n$ elements $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$) — the elements of the array.


-----Output-----

In the first line print an integer $k$ — the number of elements in the array after all the performed operations. In the second line print $k$ integers — the elements of the array after all the performed operations.


-----Examples-----
Input
7
3 4 1 2 2 1 1

Output
4
3 8 2 1 

Input
5
1 1 3 1 1

Output
2
3 4 

Input
5
10 40 20 50 30

Output
5
10 40 20 50 30 



-----Note-----

The first two examples were considered in the statement.

In the third example all integers in the given array are distinct, so it will not change.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Data structures

Implementation


Solutions:
def main():
	input()
	arr = list(map(int, input().split()))
	dict = {}
	for i in range(len(arr)):
		item = arr[i]
		while item in dict:
			del dict[item]
			item *= 2
		dict[item] = i
	numbers = [str(x[0]) for x in sorted(dict.items(), key=lambda x: x[1])]
	print(len(numbers))
	print(' '.join(numbers))
main()


n = int(input())

def insert(x, d, i, s):
	s.add(x)
	try:
		d[x].append(i)
		if len(d[x]) == 2:
			d[x] = []
			s.remove(x)
			insert(2 * x, d, i, s)
	except:
		d[x] = [i]
a = list(map(int, input().split()))
d = dict()
s = set()
for i in range(n):
	insert(a[i], d, i, s)
fin = []
for i in d.keys():
	for j in d[i]:
		fin.append([j, i])
fin.sort()
print(len(fin))
for i in fin:
	print(i[1], end=' ')


from heapq import *

class priority_queue(object):

	def __init__(self):
		self.que = []
		heapify(self.que)

	def push(self, obj):
		heappush(self.que, obj)

	def pop(self):
		return heappop(self.que)

	def top(self):
		return self.que[0]

	def __bool__(self):
		return bool(self.que)

	def __repr__(self):
		return repr(self.que)

	def __len__(self):
		return len(self.que)
m = {}
n = int(input())
a = list(map(int, input().split()))
for (i, _) in enumerate(a):
	m.setdefault(_, priority_queue()).push(i)
que = priority_queue()
for (k, v) in m.items():
	que.push(k)
while que:
	cur = que.pop()
	if len(m[cur]) <= 1:
		continue
	while len(m[cur]) > 1:
		m[cur].pop()
		m.setdefault(cur << 1, priority_queue()).push(m[cur].pop())
		que.push(cur << 1)
l = [(k, v.top()) for (k, v) in m.items() if v]
l.sort(key=lambda x: x[1])
print(len(l))
for (k, v) in l:
	print(k, end=' ')


n = int(input())
L = input().split(' ')
for i in range(len(L)):
	L[i] = int(L[i])
dico = {}
j = 0
while j < len(L):
	if L[j] not in dico or dico[L[j]] == -1:
		dico[L[j]] = j
		j += 1
	else:
		L[dico[L[j]]] = -1
		dico[L[j]] = -1
		L[j] = L[j] * 2
sm = 0
for i in range(len(L)):
	if L[i] != -1:
		sm += 1
print(sm)
k = 1
for i in range(len(L)):
	if L[i] != -1:
		if k < sm:
			print(L[i], end=' ')
		else:
			print(L[i])
		k += 1


n = int(input())
a = [int(x) for x in input().split()]
repeats = {}
for (i, v) in enumerate(a):
	x = v
	while x in repeats:
		del repeats[x]
		x *= 2
	repeats[x] = i
print(len(repeats))
print(*repeats.keys())


input()
arr = [int(x) for x in input().split()]
from heapq import *
numsdict = {}
for (i, x) in enumerate(arr):
	if x in numsdict:
		heappush(numsdict[x], i)
	else:
		numsdict[x] = [i]
heap = []
for (k, v) in numsdict.items():
	heappush(heap, [k, v])
while heap:
	curr = heap[0]
	x = curr[0]
	indices = curr[1]
	pnt = 0
	while len(indices) >= 2:
		first = heappop(indices)
		second = heappop(indices)
		if 2 * x in numsdict:
			heappush(numsdict[2 * x], second)
		else:
			numsdict[2 * x] = [second]
			heappush(heap, [2 * x, numsdict[2 * x]])
		arr[first] = None
		arr[second] = 2 * x
	heappop(heap)
arr2 = []
for x in arr:
	if x is not None:
		arr2.append(x)
print(len(arr2))
print(' '.join(map(str, arr2)))


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	while a[i] in d:
		del d[a[i]]
		a[i] *= 2
	d[a[i]] = i
print(len(d))
d1 = {}
for (k, v) in d.items():
	d1[v] = k
res = []
for i in sorted(d.values()):
	res.append(d1[i])
print(*res)


n = int(input())
a = list(map(int, input().split()))
idx = {}
for i in range(n):
	x = a[i]
	while x in idx:
		del idx[x]
		x *= 2
	idx[x] = i
print(len(idx))
print(*list(idx.keys()))


import sys
from collections import defaultdict
from heapq import heappush, heappop
n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(list)
for (i, x) in enumerate(a):
	dic[x].append(i)
hq = sorted(dic.keys())
while hq:
	key = heappop(hq)
	if len(dic[key]) <= 1:
		continue
	if key * 2 not in dic:
		heappush(hq, key * 2)
	dic[key].sort()
	for i in range(1, len(dic[key]), 2):
		a[dic[key][i - 1]] = 0
		a[dic[key][i]] = key * 2
		dic[key * 2].append(dic[key][i])
a = [x for x in a if x != 0]
print(len(a))
print(*a)


n = int(input())
a = list(map(int, input().split()))
c = {}
for p in range(len(a)):
	while c.get(a[p], -1) >= 0:
		a[c[a[p]]] = 0
		c[a[p]] = -1
		a[p] *= 2
	c[a[p]] = p
solution = [str(x) for x in a if x > 0]
print(len(solution))
print(' '.join(solution))


n = int(input().split()[0])
ns = [int(x) for x in input().split()[:n]]
ind = {}
i = 0
while i < n:
	ni = ns[i]
	while ni in ind:
		ns[ind[ni]] = None
		ind.pop(ni, None)
		ni *= 2
	ns[i] = ni
	ind[ni] = i
	i += 1
ans = [x for x in ns if x != None]
print(len(ans))
for i in ans:
	print(i, end=' ')


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	while a[i] in d:
		del d[a[i]]
		a[i] *= 2
	d[a[i]] = i
print(len(d))
print(*d)


n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in range(n):
	while a[i] in d:
		d.pop(a[i])
		a[i] *= 2
	d[a[i]] = i
print(len(d))
print(*d)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		while i in d:
			del d[i]
			i *= 2
		d[i] = 1
	print(len(d))
	print(*d)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
a = {}
for x in map(int, input().split()):
	while x in a:
		del a[x]
		x *= 2
	a[x] = 1
print(len(a))
print(*a)


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
from bisect import insort
n = t()
a = list(ll())
d = {}
v = [-1] * (n + 1)
c = 0
for i in range(n):
	if a[i] not in d.keys():
		d[a[i]] = [i]
	else:
		d[a[i]].append(i)
l = sorted(d.keys())
for i in l:
	while len(d[i]) >= 2:
		x = d[i].pop(0)
		y = d[i].pop(0)
		a[y] = 2 * a[y]
		if a[y] in d.keys():
			insort(d[a[y]], y)
		else:
			d[a[y]] = [y]
			insort(l, a[y])
	if len(d[i]):
		c += 1
		v[d[i][0]] = i
print(c)
for i in v:
	if i != -1:
		print(i, end=' ')


n = int(input())
d = {}
for (i, x) in enumerate(list(map(int, input().split()))):
	while x in d:
		del d[x]
		x *= 2
	d[x] = i
print(len(d))
print(*d)


from collections import defaultdict
from heapq import *

def f(a):
	vis = [''] * len(a)
	ind = defaultdict(list)
	for i in range(len(a)):
		if len(ind[a[i]]) == 0:
			heapify(ind[a[i]])
		heappush(ind[a[i]], i)
	st = list(ind.keys())
	heapify(st)
	cnt = 0
	while st:
		i = heappop(st)
		while ind[i]:
			if len(ind[i]) == 1:
				t = heappop(ind[i])
				vis[t] = str(i)
				cnt += 1
			else:
				t1 = heappop(ind[i])
				t2 = heappop(ind[i])
				vis[t1] = ''
				if ind.get(2 * i, None) == None:
					heappush(st, 2 * i)
				heappush(ind[i * 2], t2)
	print(cnt)
	print(*vis, sep=' ')
a = input()
l = list(map(int, input().strip().split()))
f(l)


import bisect
n = int(input())
input_str = input().split()
numbers = [int(x) for x in input_str]
indices = dict()
for i in range(n):
	if numbers[i] not in indices:
		indices[numbers[i]] = [i]
	else:
		indices[numbers[i]].append(i)

def insert_sorted(arr, e, l, r):
	if l == r:
		if arr[l] >= e:
			arr.insert(l, e)
			return
		else:
			arr.insert(l + 1, e)
			return
	c = (l + r) // 2
	if arr[c] < e:
		return insert_sorted(arr, e, c + 1, r)
	else:
		return insert_sorted(arr, e, l, c)
number_list = sorted(list(indices))
number_index = 0
while number_index < len(number_list):
	number = number_list[number_index]
	need_add = False
	double = number * 2
	indices[number] = sorted(indices[number])
	for op_i in range(len(indices[number]) // 2):
		fir_i = indices[number][op_i * 2]
		sec_i = indices[number][op_i * 2 + 1]
		numbers[fir_i] = 0
		numbers[sec_i] = double
		if double not in indices:
			indices[double] = [sec_i]
			need_add = True
		else:
			indices[double].append(sec_i)
	if need_add:
		insert_sorted(number_list, double, number_index, len(number_list) - 1)
	number_index += 1
output = [str(x) for x in numbers if x > 0]
print(len(output))
print(' '.join(output))


n = int(input())
arr = list(map(int, input().strip().split(' ')))
d = {}
i = 0
while i < n:
	try:
		t = d[arr[i]]
		arr[t] = -1
		del d[arr[i]]
		arr[i] = 2 * arr[i]
	except KeyError:
		d[arr[i]] = i
		i += 1
print(n - arr.count(-1))
for i in range(n):
	if arr[i] != -1:
		print(arr[i], end=' ')


n = int(input())
ls = list(map(int, input().split()))
dic = {}
for i in range(n):
	while ls[i] in dic:
		del dic[ls[i]]
		ls[i] = ls[i] * 2
	dic[ls[i]] = i
print(len(dic))
print(*dic)


n = int(input())
a = list(map(int, input().split()))
ans = {}
for i in range(n):
	while a[i] in ans:
		del ans[a[i]]
		a[i] *= 2
	ans[a[i]] = 0
print(len(ans))
print(*ans)


(n, a, q) = (int(input()), list(map(int, input().split())), {})
for i in range(n):
	if a[i] not in q:
		q[a[i]] = i
	else:
		while a[i] in q:
			a[q[a[i]]] = 0
			q.pop(a[i])
			a[i] *= 2
		q[a[i]] = i
a = [i for i in a if i != 0]
print(len(a), '\n', *a)


import sys
input = sys.stdin.readline
t = int(input())
a = list(map(int, input().split(' ')))
d = {}
b = []
for i in a:
	b.append(i)
for i in range(t):
	curr = a[i]
	while True:
		if d.get(curr, -1) == -1:
			d[curr] = i
			break
		else:
			ind = d[curr]
			b[ind] = -1
			del d[curr]
			b[i] = 2 * curr
			if d.get(b[i], -1) == -1:
				d[b[i]] = i
				break
			curr = 2 * curr
ans = []
for i in b:
	if i != -1:
		ans.append(str(i))
print(len(ans))
print(' '.join(ans))


n = int(input())
d = {}
for (i, x) in enumerate(map(int, input().split())):
	while d.get(x, -1) >= 0:
		del d[x]
		x *= 2
	d[x] = i
print(len(d))
print(*d)


input()
a = {}
for (i, x) in enumerate(map(int, input().split())):
	while x in a:
		del a[x]
		x *= 2
	a[x] = i
print(len(a))
print(*a)


import sys
n = int(input())
a = list(map(int, input().split()))
p = {}
for i in range(n):
	while a[i] in p:
		del p[a[i]]
		a[i] *= 2
	p[a[i]] = i
print(len(p))
print(*p)


import heapq

def problem_D(length, array):
	cache = []
	for (idx, num) in enumerate(array):
		heapq.heappush(cache, (num, idx))
	out = []
	while len(cache) > 1:
		first = heapq.heappop(cache)
		second = heapq.heappop(cache)
		if first[0] == second[0]:
			newValue = (first[0] * 2, second[1])
			heapq.heappush(cache, newValue)
		else:
			heapq.heappush(cache, second)
			out.append(first)
	if len(cache) == 1:
		out.append(cache.pop())
	out.sort(key=lambda tup: tup[1])
	outputLength = len(out)
	outputList = ''
	for el in out:
		outputList += str(el[0]) + ' '
	print(outputLength)
	print(outputList)
	return (outputLength, [x[0] for x in out])
length = input()
array = [int(x) for x in input().split(' ')]
problem_D(length, array)


from queue import deque
import sys

def process(arr):
	trackers = dict()
	arr = [elm for elm in arr]
	for i in range(len(arr)):
		while True:
			prev_occ = trackers.get(arr[i], deque())
			if len(prev_occ) == 0:
				prev_occ.append(i)
				trackers[arr[i]] = prev_occ
				break
			first_occ = prev_occ.popleft()
			arr[first_occ] = None
			arr[i] = arr[i] * 2
	return [elm for elm in arr if elm is not None]
n = int(sys.stdin.readline())
arr = [int(elm) for elm in sys.stdin.readline().split()]
res = process(arr)
print(len(res))
print(' '.join([str(elm) for elm in res]))


def inpmap():
	return list(map(int, input().split()))
n = int(input())
arr = list(inpmap())
dc = {}
for i in range(n):
	x = arr[i]
	while x in dc:
		del dc[x]
		x *= 2
	dc[x] = i
print(len(dc))
print(*dc)


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
b = set()
ls = []
i = 0
while i < n:
	if a[i] not in b:
		b.add(a[i])
		ls.append(a[i])
		i += 1
	else:
		while a[i] in b:
			b.remove(a[i])
			a[i] *= 2
		ls.append(a[i])
ans = []
c = set()
for i in range(len(ls)):
	if ls[-i - 1] in b and ls[-i - 1] not in c:
		ans.append(ls[-i - 1])
		c.add(ls[-i - 1])
print(len(ans))
for i in range(len(ans)):
	print(ans[-i - 1], end=' ')


n = int(input())
a = list(map(int, input().split()))
d = {}
for x in a:
	while x in d:
		del d[x]
		x *= 2
	d[x] = 0
print(len(d))
print(*d)


from heapq import heappop, heappush, heapify
n = int(input())
arr = list(map(int, input().split()))
ans = [-1 for i in range(n + 1)]
he = []
for i in range(n):
	he.append([arr[i], i])
he.append([99999999999999999999999999999, -1])
heapify(he)
while he:
	(q, indx_q) = heappop(he)
	(p, indx_p) = heappop(he)
	if indx_p == -1:
		ans[indx_q] = q
		break
	if q == p:
		heappush(he, [2 * p, indx_p])
		continue
	ans[indx_q] = q
	heappush(he, [p, indx_p])
aa = []
for i in range(n + 1):
	if ans[i] != -1:
		aa.append(ans[i])
print(len(aa))
print(*aa)


from collections import OrderedDict
n = int(input())
a = [int(x) for x in input().split()]
d = OrderedDict()
for x in a:
	t = x
	while t in d:
		del d[t]
		t *= 2
	d[t] = 1
print(len(d))
print(*d)


def merge(a, b):
	la = len(a)
	lb = len(b)
	i = 0
	j = 0
	ans = []
	while i < la and j < lb:
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			j += 1
	while i < la:
		ans.append(a[i])
		i += 1
	while j < lb:
		ans.append(b[j])
		j += 1
	return ans
n = int(input())
a = list(map(int, input().strip().split()))
d = {}
for i in range(n):
	if a[i] in d:
		d[a[i]].append(i)
	else:
		d[a[i]] = [i]
x = []
y = []
for i in d:
	x.append(i)
x.sort()
lx = len(x)
ly = 0
i = 0
j = 0
ans = {}
while i < lx or j < ly:
	r = 0
	if i >= lx:
		r = y[j]
		j += 1
	elif j >= ly:
		r = x[i]
		i += 1
	elif x[i] < y[j]:
		r = x[i]
		i += 1
	else:
		r = y[j]
		j += 1
	l = len(d[r])
	if l % 2 != 0:
		ans[d[r][-1]] = r
	if l == 1:
		continue
	temp = []
	for rj in range(1, l, 2):
		temp.append(d[r][rj])
	if 2 * r in d:
		d[2 * r] = merge(d[2 * r], temp)
	else:
		d[2 * r] = temp
		y.append(2 * r)
		ly += 1
x = []
for i in ans:
	x.append(i)
x.sort()
print(len(x))
for i in x:
	print(ans[i], end=' ')


n = int(input())
numbers = list(map(int, input().split()))
pos = {}
i = 0
removed = 0
while i < n:
	if numbers[i] not in pos or pos[numbers[i]] == -1:
		pos[numbers[i]] = i
		i += 1
	else:
		numbers[pos[numbers[i]]] = -1
		pos[numbers[i]] = -1
		numbers[i] *= 2
		removed += 1
print(n - removed)
for i in numbers:
	if i != -1:
		print('%d ' % i, end='')
print()


n = int(input())
line = input().split()
dic = {}
ans = [0] * n
how_many = 0
index = 0
for word in line:
	num = int(word)
	while num in dic:
		if dic[num] == -1:
			break
		else:
			how_many += 1
			ans[dic[num]] = 0
			dic[num] = -1
			num *= 2
	dic[num] = index
	ans[index] = num
	index += 1
print(n - how_many)
for num in ans:
	if num != 0:
		print(num, end=' ')
print()


import math
n = int(input())
arr = list(map(int, input().split()))
tb = {}
for i in range(n):
	if arr[i] in tb:
		x = arr[i]
		while x in tb:
			y = tb.pop(x)
			if 2 * x not in tb:
				tb[2 * x] = i
				break
			x = 2 * x
	else:
		tb[arr[i]] = i
s = ''
print(len(tb))
key = list(tb.keys())
ans = []
for i in key:
	ans.append([tb[i], i])
ans.sort()
for i in ans:
	s += str(i[1]) + ' '
print(s)


n = int(input())
xs = [int(x) for x in input().split()]
pos = {}
for (i, x) in enumerate(xs):
	while x in pos:
		del pos[x]
		x *= 2
	pos[x] = i
ks = sorted(pos.keys(), key=lambda k: pos[k])
print(len(ks))
print(' '.join(map(str, ks)))


input()
a = {}
for i in list(map(int, input().split())):
	while i in a:
		del a[i]
		i *= 2
	a[i] = 1
print(len(a))
print(*a)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		s = set()
		ans = []
		d = {}
		for x in range(n):
			if l[x] not in s:
				s.add(l[x])
				d[l[x]] = x
			else:
				temp = l[x]
				while temp in s:
					s.remove(temp)
					d[temp] = -1
					temp *= 2
				s.add(temp)
				d[temp] = x
		for x in d:
			if d[x] != -1:
				ans.append([x, d[x]])
		ans.sort(key=lambda x: x[1])
		print(len(ans))
		for x in ans:
			print(x[0], end=' ')
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


from collections import *
from bisect import *
from heapq import *
from copy import deepcopy
(n, a) = (int(input()), list(map(int, input().split())))
(mem, dis) = (defaultdict(list), Counter(a))
for i in range(n):
	mem[a[i]].append(i)
queue = list(mem.items())
heapify(queue)
while queue:
	ele = heappop(queue)
	for i in range(1, len(ele[1]), 2):
		a[ele[1][i]] *= 2
		a[ele[1][i - 1]] = 0
		insort_right(mem[ele[0] * 2], ele[1][i])
	if not dis[ele[0] * 2] and mem[ele[0] * 2]:
		heappush(queue, (ele[0] * 2, mem[ele[0] * 2]))
new = list(filter(lambda x: x, a))
print(len(new))
print(*new)


n = int(input())
a = list(map(int, input().split()))
d = {}
i = 0
while i < n:
	num = a[i]
	while num in d:
		a[d[num]] = -1
		del d[num]
		num = num * 2
	d[num] = i
	a[i] = num
	i += 1
ans = []
for i in a:
	if i != -1:
		ans.append(i)
print(len(ans))
print(*ans)


n = int(input())
a = list(map(int, input().split()))
new = {}
for i in range(n):
	num = a[i]
	while num in new:
		del new[num]
		num *= 2
	new[num] = i
print(len(new))
print(*list(new.keys()))


n = int(input())
lis = list(map(int, input().split()))
d = {}
for i in lis:
	a = i
	while a in d:
		del d[a]
		a *= 2
	d[a] = 1
print(len(d))
for i in d:
	print(i, end=' ')


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	a = list(map(int, input().split()))
	d = collections.defaultdict(lambda : -1)
	(l, r) = (0, 0)
	while r < n:
		if a[r] not in d.keys():
			d[a[r]] = r
		elif d[a[r]] == -1:
			d[a[r]] = r
		else:
			while a[r] in d.keys():
				if d[a[r]] == -1:
					break
				tmp = a[r]
				a[d[tmp]] = -1
				d[tmp] = -1
				a[r] = 2 * tmp
			d[a[r]] = r
		r += 1
	ans = []
	for x in a:
		if x != -1:
			ans.append(x)
	print(len(ans))
	print(' '.join([str(b) for b in ans]))
main()


import sys
import math
import itertools
import heapq
import bisect

def solution(n, arr):
	count = {}
	for (i, num) in enumerate(arr):
		if num in count:
			heapq.heappush(count[num], i)
		else:
			count[num] = [i]
	pq = []
	for (num, c) in count.items():
		occs = len(c)
		if occs >= 2:
			heapq.heappush(pq, num)
	while pq:
		num = heapq.heappop(pq)
		if len(count[num]) < 2:
			continue
		i = heapq.heappop(count[num])
		j = heapq.heappop(count[num])
		arr[i] = None
		arr[j] = num * 2
		if num * 2 in count:
			heapq.heappush(count[num * 2], j)
			if len(count[num * 2]) >= 2:
				heapq.heappush(pq, num * 2)
		else:
			count[num * 2] = [j]
		if len(count[num]) >= 2:
			heapq.heappush(pq, num)
	res = [str(ai) for ai in arr if ai != None]
	print(len(res))
	print(' '.join(res))
n = sys.stdin.readline().strip()
arr = list(map(int, sys.stdin.readline().strip().split(' ')))
solution(n, arr)


tam = int(input())
vet = list(map(int, input().split()))
freq = {}
for i in range(tam):
	num = vet[i]
	while num in freq:
		del freq[num]
		num *= 2
	freq[num] = i
print(len(freq))
print(*list(freq.keys()))


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	idx = {}
	for i in range(len(a)):
		x = a[i]
		while x in idx:
			del idx[x]
			x *= 2
		idx[x] = i
	ans = list(sorted(idx.keys(), key=lambda x: idx[x]))
	print(len(ans))
	print(' '.join([str(x) for x in ans]))

def __starting_point():
	main()
__starting_point()


from collections import deque
N = int(input())
A = list(map(int, input().split()))
dic = dict()
for i in range(N):
	while A[i] in dic:
		A[dic[A[i]]] = -1
		del dic[A[i]]
		A[i] *= 2
	dic[A[i]] = i
B = []
for a in A:
	if a > 0:
		B.append(a)
print(len(B))
print(' '.join(list(map(str, B))))


n = 0
while True:
	try:
		n = int(input())
	except:
		break
	mp = dict()
	a = list()
	ans = list()
	a = input().split()
	for i in range(len(a)):
		a[i] = int(a[i])
	for i in range(n):
		if a[i] in mp.keys():
			while a[i] in mp.keys():
				t = mp.get(a[i])
				a[t] = -1
				mp.pop(a[i])
				a[i] <<= 1
		mp[a[i]] = int(i)
	for i in a:
		if i != -1:
			ans.append(i)
	print(len(ans))
	print(*ans, sep=' ')


n = int(input())
ns = [int(x) for x in input().split()]
ind = {}
for i in range(n):
	ni = ns[i]
	while ni in ind:
		ns[ind[ni]] = None
		ind.pop(ni, None)
		ni *= 2
	ns[i] = ni
	ind[ni] = i
ans = [str(x) for x in ns if x != None]
print(len(ans))
print(' '.join(ans))


n = int(input())
nums = list(map(int, input().split()))
lastSeen = {}
i = 0
while i < len(nums):
	if nums[i] in lastSeen:
		nums[lastSeen[nums[i]]] = 0
		del lastSeen[nums[i]]
		nums[i] <<= 1
		i -= 1
	else:
		lastSeen[nums[i]] = i
	i += 1
newnums = []
for n in nums:
	if n != 0:
		newnums.append(n)
print(len(newnums))
print(' '.join(map(str, newnums)))


import heapq
n = int(input())
A = list(map(int, input().split()))
hq = []
for i in range(n):
	heapq.heappush(hq, (A[i], i))
while len(hq) >= 2:
	t1 = heapq.heappop(hq)
	t2 = heapq.heappop(hq)
	if t1[0] == t2[0]:
		A[t1[1]] = 0
		A[t2[1]] = 2 * t1[0]
		heapq.heappush(hq, (2 * t2[0], t2[1]))
	else:
		heapq.heappush(hq, t2)
print(len(A) - A.count(0))
for a in A:
	if a != 0:
		print(a, end=' ')


import heapq
n = int(input())
nums = list(map(int, input().strip().split()))
pq = []
for (i, item) in enumerate(nums):
	heapq.heappush(pq, (item, i))
res = []
while len(pq) >= 2:
	x = pq[0]
	heapq.heappop(pq)
	y = pq[0]
	if x[0] == y[0]:
		heapq.heappush(pq, (2 * y[0], y[1]))
		heapq.heappop(pq)
	else:
		res.append(x)
res.append(pq[0])
res = sorted(res, key=lambda x: x[1])
ans = []
for (k, v) in res:
	ans.append(str(k))
print(len(ans))
print(' '.join(ans))


import collections
import functools
import math
import sys
import bisect

def In():
	return map(int, sys.stdin.readline().split())
input = sys.stdin.readline

def mergeeq():
	n = int(input())
	nums = collections.defaultdict(list)
	pos = 0
	for i in input().split():
		nums[int(i)].append(pos)
		pos += 1
	nums = sorted(nums.items(), key=lambda x: x[0])
	ln = len(nums)
	acordpos = dict()
	for i in range(ln):
		acordpos[nums[i][0]] = i
	pos = 0
	for i in range(ln):
		nums[i] = list(nums[i])
	added = []
	for i in nums:
		(num, l) = i
		l.sort()
		ll = len(l)
		if ll >= 2:
			if num * 2 in acordpos:
				lpos = acordpos[num * 2]
				for x in added:
					if x <= lpos:
						lpos += 1
			else:
				lpos = bisect.bisect_left(nums, [num * 2])
				acordpos[num * 2] = lpos
				added.append(lpos)
				nums.insert(lpos, [num * 2, []])
		for x in range(0, ll - ll % 2, 2):
			nums[lpos][1].append(l[x + 1])
		if ll % 2:
			nums[pos][1] = [nums[pos][1][-1]]
		else:
			nums[pos][1] = []
		pos += 1
	ans = [-1] * n
	for i in nums:
		(num, l) = i
		for x in l:
			ans[x] = num
	res = []
	for i in ans:
		if i != -1:
			res.append(str(i))
	print(len(res))
	print(' '.join(res))
mergeeq()


def read_data():
	n = int(input().strip())
	a = list(map(int, list(input().strip().split())))
	return (n, a)

def solve():
	s = {}
	for i in range(len(a)):
		j = a[i]
		while j in s:
			del s[j]
			j *= 2
		s[j] = i
	sol = list(sorted(s.keys(), key=lambda j: s[j]))
	print(len(sol))
	print(*sol)
(n, a) = read_data()
solve()


n = int(input())
from collections import defaultdict
a = [int(i) for i in input().split()]
d = defaultdict(int)
k = 0
for i in range(n):
	if a[i] in d:
		while d[a[i]] != 0:
			del d[a[i]]
			a[i] = 2 * a[i]
			if d[a[i]] != 0:
				d[a[i]] += 1
			else:
				d[a[i]] += 1
				break
	else:
		d[a[i]] += 1
print(len(d))
for i in d:
	print(i, end=' ')


import heapq

def update(d, h, n, i):
	l = d.setdefault(n, [])
	heapq.heappush(l, i)
	if len(l) == 2:
		heapq.heappush(h, n)

def main(nums):
	d = {}
	h = []
	for (i, n) in enumerate(nums):
		update(d, h, n, i)
	while h:
		m = heapq.heappop(h)
		l = d[m]
		assert len(l) >= 2
		i = heapq.heappop(l)
		j = heapq.heappop(l)
		nums[i] = -1
		nums[j] = 2 * nums[j]
		if len(l) >= 2:
			heapq.heappush(h, m)
		update(d, h, nums[j], j)
	res = [str(n) for n in nums if n != -1]
	print(len(res))
	print(' '.join(res))
_ = input()
nums = list(map(int, input().split()))
main(nums)


n = int(input())
out = n
L = list(map(int, input().split()))
X = dict()

def f(x):
	try:
		return X[x]
	except:
		return -1
for i in range(n):
	x = L[i]
	while f(x) != -1:
		L[X[x]] = -1
		out -= 1
		X[x] = -1
		L[i] *= 2
		x = L[i]
	X[x] = i
print(out)
for i in range(n):
	if L[i] != -1:
		print(L[i], end=' ')
print()


from heapq import heappush, heappop

def main():
	heap = []
	for i in range(10, 0, -1):
		heappush(heap, (i, -i))
	print(heap)
	while heap:
		print(heappop(heap), end=' ')
	print()

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	heap = []
	for (idx, i) in enumerate(arr):
		heappush(heap, (i, idx))
	ans = [-1 for x in range(0, n)]
	cnt = 0
	while heap:
		cur = heappop(heap)
		if len(heap) == 0:
			ans[cur[1]] = cur[0]
			break
		nxt = heappop(heap)
		if cur[0] == nxt[0]:
			heappush(heap, (2 * nxt[0], nxt[1]))
			cnt += 1
		else:
			heappush(heap, nxt)
			ans[cur[1]] = cur[0]
	print(n - cnt)
	for i in ans:
		if i == -1:
			continue
		print(i, end=' ')
	print()
solve()


from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
a = gil()
idx = {}
for i in range(n):
	if a[i] in idx:
		idx[a[i]].append(i)
	else:
		idx[a[i]] = [i]
h = []
for v in idx:
	if len(idx[v]) > 1:
		h.append(v)
heapify(h)
while h:
	v = heappop(h)
	idxs = idx[v]
	idxs.sort()
	nv = 2 * v
	idx[nv] = idx.get(nv, [])
	temp = idx[nv]
	prevLen = len(temp)
	for i in range(1, len(idxs), 2):
		(l, r) = (idxs[i - 1], idxs[i])
		a[l] = 0
		a[r] *= 2
		temp.append(r)
	if prevLen < 2 and len(temp) > 1:
		heappush(h, nv)
	idx[nv] = temp
ans = []
for v in a:
	if v:
		ans.append(v)
print(len(ans))
print(*ans, sep=' ')