Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.

There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.

Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.

The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 200 000) — powers of video cards.


-----Output-----

The only line of the output should contain one integer value — the maximum possible total power of video cards working together.


-----Examples-----
Input
4
3 2 15 9

Output
27

Input
4
8 2 2 7

Output
18



-----Note-----

In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.

In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.

Difficulty: HARD
Tags: Implementation

Complete search

Number theory

Mathematics

Data structures
Skill types: Data structures

Complete search

Starter code:

Solutions:
def main():
	n = int(input())
	aa = list(map(int, input().split()))
	aa.sort()
	lim = aa[-1] + 1
	(cnt, a) = ([0] * lim, aa[0] - 1)
	for (i, b) in zip(range(n, -1, -1), aa):
		if a != b:
			cnt[a + 1:b + 1] = [i] * (b - a)
			a = b
	(avail, res) = ([True] * lim, [])
	for (i, a) in enumerate(aa):
		if avail[a]:
			avail[a] = False
			res.append(a * sum(cnt[a::a]))
	print(max(res))
main()


def main():
	input()
	l = list(map(int, input().split()))
	n = max(l)
	aa = [0] * (n + 1)
	for x in l:
		aa[x] += 1
	(f, x) = ([0] * n, 0)
	for a in reversed(aa):
		x += a
		f.append(x)
	f.reverse()
	res = []
	for (i, a) in enumerate(aa):
		if a:
			(tot, a) = (0, f[i])
			for j in range(i, n + 1, i):
				b = f[j + i]
				tot += (a - b) * j
				a = b
			res.append(tot)
	print(max(res))
main()


N = 200005
n = int(input())
A = input().split()
A = list(map(lambda x: int(x), A))
pref = [0] * N
for a in A:
	pref[a] += 1
for i in range(1, N):
	pref[i] += pref[i - 1]
ans = 0
for i in range(1, N):
	if pref[i] != pref[i - 1]:
		suma = 0
		for j in range(i, N, i):
			suma += j * (pref[min(N - 1, j + i - 1)] - pref[j - 1])
		ans = max(ans, suma)
print(ans)


import math

def main():
	prefSum = []

	def GetSum(l, r):
		return prefSum[min(r, 199999)] - prefSum[l - 1]
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(2 * 10 ** 5 + 10):
		b.append(0)
	for i in range(n):
		b[a[i]] += 1
	prefSum.append(0)
	for i in range(1, 2 * 10 ** 5 + 10):
		prefSum.append(b[i] + prefSum[i - 1])
	ans = 0
	cnt = 0
	for i in range(2 * 10 ** 5 + 10):
		if b[i] == 0:
			continue
		l = i
		r = 2 * i - 1
		curAns = 0
		for l in range(i, 200001, i):
			curAns += l * (prefSum[min(r, 200000)] - prefSum[l - 1])
			r += i
		ans = max(ans, curAns)
	print(ans)
main()


from bisect import bisect_left
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
for k in set(a):
	r = sum((n - bisect_left(a, j) for j in range(k, a[-1] + 1, k)))
	ans = max(ans, k * r)
print(ans)


n = int(input())
s = list(map(int, input().split()))
h = max(s)
ss = [0] * (h + 1)
for i in s:
	ss[i] += 1
(f, x) = ([0] * h, 0)
for j in reversed(ss):
	x += j
	f.append(x)
f.reverse()
res = []
for (i, x) in enumerate(ss):
	if x:
		(summ, x) = (0, f[i])
		for j in range(i, h + 1, i):
			o = f[j + i]
			summ += (x - o) * j
			x = o
		res.append(summ)
print(max(res))


n = int(input())
l = list(map(int, input().split()))
l1 = [0] * 200001
for i in l:
	l1[i] += 1
for i in range(1, 200001):
	l1[i] += l1[i - 1]
m = 0
l = list(set(l))
for i in l:
	s = 0
	for j in range(i, 200001, i):
		s += (l1[min(200000, i + j - 1)] - l1[j - 1]) * j
	if s > m:
		m = s
print(m)


n = int(input())
a = list(map(int, input().split()))
N = 2 * 10 ** 5
cnt = [0 for i in range(N + 1)]
S = [0 for i in range(N + 1)]
for x in a:
	cnt[x] += 1
	S[x] += x
for i in range(1, N + 1):
	cnt[i] += cnt[i - 1]
	S[i] += S[i - 1]
ans = 10 ** 18
used = [0 for i in range(N + 1)]
for i in a:
	if used[i]:
		continue
	used[i] = 1
	(prv, cur) = (0, 0)
	di = list(range(i, N + 1, i))
	di.append(N + 1)
	for x in di:
		(l, r) = (prv + 1, x - 1)
		cur += S[r] - S[l - 1]
		cur -= prv * (cnt[r] - cnt[l - 1])
		prv = x
	ans = min(ans, cur)
print(sum(a) - ans)