Kristen loves playing with and comparing numbers. She thinks that if she takes two different positive numbers, the one whose digits sum to a larger number is better than the other. If the sum of digits is equal for both numbers, then she thinks the smaller number is better. For example, Kristen thinks that $13$ is better than $31$ and that $12$ is better than ${11}$.

Given an integer, $n$, can you find the divisor of $n$ that Kristin will consider to be the best?

Input Format

A single integer denoting $n$.

Constraints

$0<n\leq10^5$

Output Format

Print an integer denoting the best divisor of $n$.  

Sample Input 0
12

Sample Output 0
6

Explanation 0

The set of divisors of $12$ can be expressed as $\{1,2,3,4,6,12\}$. The divisor whose digits sum to the largest number is ${6}$ (which, having only one digit, sums to itself). Thus, we print ${6}$ as our answer.

Difficulty: EASY
Tags: Mathematics

Starter code:

Solutions:
import sys

def sumdigits(n):
	return sum(map(int, list(str(n))))
n = int(input().strip())
best = 1
best_sum = 1
for k in range(1, n + 1):
	if n % k == 0:
		the_sum = sumdigits(k)
		if the_sum > best_sum:
			best_sum = the_sum
			best = k
print(best)


import sys
import math

def quersumme(n):
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return s
n = int(input().strip())
if n == 1:
	print('1')
elif n == 2:
	print('2')
else:
	bestdivisor = 1
	bestvalue = 1
	for i in range(3, n + 1):
		if n % i == 0:
			s = quersumme(i)
			if s > bestvalue:
				bestvalue = s
				bestdivisor = i
	print(str(bestdivisor))


n = int(input().strip())
res = 1
val = 1
for i in range(2, n + 1):
	if not n % i:
		j = sum(map(int, str(i)))
		if j > val:
			val = j
			res = i
print(res)


import sys

def sum_digits(n):
	r = 0
	while n:
		r = r + n % 10
		n = n // 10
	return r
n = int(input().strip())
bn = bs = 0
for i in range(1, n + 1):
	if n % i == 0:
		sd = sum_digits(i)
		if sd > bs or (sd == bs and i < bn):
			bn = i
			bs = sd
print(bn)


import sys
import math
n = int(input().strip())
L = list()
digit = list()
for i in range(1, int(math.sqrt(n)) + 1):
	if n % i == 0:
		L.append(i)
		L.append(n // i)
L.sort()
for num in L:
	digit.append(sum(list(map(int, str(num)))))
print(L[digit.index(max(digit))])


import sys

def bigger(x, y):
	sum_x = sum((int(i) for i in str(x)))
	sum_y = sum((int(i) for i in str(y)))
	if sum_x > sum_y:
		return x
	if sum_x < sum_y:
		return y
	return x if x < y else y
n = int(input().strip())
res = 1
for i in range(2, n + 1):
	if n % i == 0:
		res = bigger(res, i)
print(res)


import sys

def find(n):
	return sum(map(int, str(n)))
n = int(input().strip())
a = []
for i in range(1, n + 1):
	if n % i == 0:
		a.append(i)
m = 0
for i in range(len(a)):
	x = find(a[i])
	if m < x:
		ans = a[i]
		m = x
print(ans)


import sys
import math
n = int(input().strip())
best_devisor = 1
for i in range(1, n + 1):
	if n % i == 0:
		i_val = sum([int(x) for x in str(i)])
		best_val = sum([int(x) for x in str(best_devisor)])
		if i_val > best_val:
			best_devisor = i
		elif i_val == best_val:
			if best_devisor > i:
				best_devisor = i
print(best_devisor)


import math

def get_divisors(n):
	ret = []
	for x in range(1, n // 2 + 1):
		if n % x == 0:
			ret.append(x)
	ret.append(n)
	return ret

def get_sum(n):
	ret = 0
	while n:
		ret += n % 10
		n = n // 10
	return ret
n = int(input().strip())
divisors = get_divisors(n)
best = divisors[0]
best_sum = get_sum(best)
for d in divisors[1:]:
	new_sum = get_sum(d)
	if new_sum > best_sum:
		best_sum = new_sum
		best = d
print(best)


def sumDigits(n):
	sum = 0
	while n >= 1:
		sum += n % 10
		n //= 10
	return sum
n = int(input().strip())
max = 0
for i in range(1, n + 1):
	if n % i == 0:
		if sumDigits(i) > sumDigits(max):
			max = i
print(max)


n = int(input().strip())
a = [n]
for i in range(1, int(n / 2) + 1):
	if n % i == 0:
		a.append(i)
m = 0
m_a = []
for i in a:
	s = [int(a_temp) for a_temp in list(str(i))]
	s_s = sum(s)
	if s_s > m:
		m = s_s
		m_a.clear()
		m_a.append(int(i))
	elif s_s == m:
		m_a.append(int(i))
print(min(m_a))


import sys

def factors(n):
	myList = []
	for i in range(1, n + 1):
		if n % i == 0:
			myList.append(i)
	return myList

def sum_digits(n):
	s = 0
	while n:
		s += n % 10
		n //= 10
	return s
n = int(input().strip())
f = factors(n)
topsum = -1
topnum = 0
for i in f:
	sd = sum_digits(i)
	if sd > topsum:
		topsum = sd
		topnum = i
print(topnum)


def dsum(x):
	s = 0
	while x > 0:
		s += x % 10
		x //= 10
	return s

def div(x):
	red = []
	m = x ** 0.5
	n = int(m) + 1
	for i in range(1, n):
		if i == m:
			red.append(i)
		elif x % i == 0:
			red.append(i)
			red.append(x // i)
	return red
n = int(input())
red = div(n)
red.sort()
i = 1
grt = 0
for r in red:
	if dsum(r) > grt:
		grt = dsum(r)
		i = r
print(i)


n = int(input())
factors = []
if n < 10:
	print(n)
	exit(0)
for i in range(1, n // 2 + 1):
	if n % i == 0:
		factors.append(i)
factors.append(n)
maxi = 0
msum = 0
for i in factors:
	s = 0
	x = i
	while x > 0:
		s += x % 10
		x //= 10
	if s > msum:
		msum = s
		maxi = i
print(maxi)


def getDivisors(n):
	L = [1]
	for i in range(2, n + 1):
		if n % i == 0:
			L.append(i)
	return L

def digitSum(n):
	dSum = 0
	for i in str(n):
		dSum += int(i)
	return dSum
n = int(input().strip())
divisors = getDivisors(n)
dSums = [digitSum(i) for i in divisors]
ourList = []
for i in range(len(dSums)):
	if dSums[i] == max(dSums):
		ourList.append(divisors[i])
print(min(ourList))


import sys
import math

def digit_sum(n):
	return sum((int(i) for i in str(n)))
best_number = 1
best_ds = 1
n = int(input().strip())
for i in range(2, n + 1):
	if n % i != 0:
		continue
	ds = digit_sum(i)
	if ds > best_ds:
		best_number = i
		best_ds = ds
print(best_number)


import math

def dividers(n):
	res = [1]
	for i in range(2, n + 1):
		if n % i == 0:
			res.append(i)
	return res

def sum_digits(n):
	res = 0
	for char in str(n):
		res += int(char)
	return res

def best(n1, n2):
	s1 = sum_digits(n1)
	s2 = sum_digits(n2)
	if s1 > s2:
		return n1
	elif s1 < s2:
		return n2
	elif n1 < n2:
		return n1
	else:
		return n2
n = int(input().strip())
dd = dividers(n)
res = 1
for d in dd:
	res = best(res, d)
print(res)


import sys
n = int(input().strip())

def sumdigits(x):
	return sum(list(map(int, list(str(x)))))
largest = n
largest_sumdigits = sumdigits(n)
for i in range(2, n):
	if n % i == 0:
		i_sumdigits = sumdigits(i)
		if i_sumdigits == largest_sumdigits:
			if i < largest:
				largest = i
				largest_sumdigits = i_sumdigits
		elif i_sumdigits > largest_sumdigits:
			largest = i
			largest_sumdigits = i_sumdigits
print(largest)


n = int(input())
lis = []
for i in range(1, n + 1):
	if n % i == 0:
		lis.append((i, sum([int(x) for x in list(str(i))])))
lis.sort(key=lambda x: x[1], reverse=True)
print(lis[0][0])


n = int(input())
l = []
for i in range(1, n + 1):
	if n % i == 0:
		tmp = str(i)
		x = 0
		for j in range(len(tmp)):
			x += int(tmp[j])
		l.append((x, -1 * i))
l.sort()
print(l[len(l) - 1][1] * -1)


import sys
n = int(input().strip())
div = [1, n]
for i in range(2, n):
	if n % i == 0:
		div.append(i)
dsum = []
for i in range(len(div)):
	sum = 0
	s = str(div[i])
	for j in range(len(s)):
		sum += int(s[j])
	dsum.append(sum)
r = max(dsum)
cand = []
for i in range(len(dsum)):
	if dsum[i] == r:
		cand.append(div[i])
r = min(cand)
print(r)


import sys
n = int(input().strip())
lis1 = []
lis2 = []
for i in range(1, n // 2 + 1):
	if n % i == 0:
		lis1.append(str(i))
lis1.append(str(n))
for i in range(len(lis1)):
	sum = 0
	for j in lis1[i]:
		sum += int(j)
	else:
		lis2.append(sum)
m = max(lis2)
for i in range(len(lis2)):
	if m == lis2[i]:
		print(lis1[i])
		break


import sys
sd = lambda x: sum([int(c) for c in str(x)])
n = int(input().strip())
divisors = [[i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0]
flat = [i for sub_list in divisors for i in sub_list]
flat.sort()
(s, res) = (0, 0)
for x in flat:
	d = sd(x)
	if d > s:
		s = d
		res = x
print(res)


import sys
from math import sqrt

def getDigitSum(n):
	result = 0
	while n > 0:
		result += n % 10
		n = n // 10
	return result

def compare(a, b):
	s_a = getDigitSum(a)
	s_b = getDigitSum(b)
	if s_a > s_b:
		return True
	elif s_a == s_b and a < b:
		return True
	else:
		return False
n = int(input().strip())
result = 0
for i in range(1, min(int(sqrt(n)) + 1, n + 1)):
	if n % i == 0:
		if compare(i, result):
			result = i
		if compare(n // i, result):
			result = n // i
print(result)


import sys
import math

def sum_digits(n):
	s = 0
	while n:
		s += n % 10
		n //= 10
	return s
best = 1
n = int(input().strip())
if n < 10:
	best = n
else:
	for i in range(1, math.ceil(n / 2) + 1):
		if n % i == 0:
			if sum_digits(best) < sum_digits(i):
				best = i
if sum_digits(best) < sum_digits(n):
	best = n
print(best)


import sys

def divisors(n):
	divs = []
	for k in range(1, n + 1):
		if n % k == 0:
			divs.append(k)
	return divs

def digitSum(k):
	return sum(map(int, list(str(k))))
maxd = [0, 0]
n = int(input().strip())
for k in divisors(n):
	sd = [k, digitSum(k)]
	if sd[1] > maxd[1]:
		maxd = sd
	elif sd[1] == maxd[1]:
		if sd[0] < maxd[0]:
			maxd = sd
print(maxd[0])


def sumn(n):
	sum = 0
	num = n
	while num:
		x = num % 10
		num /= 10
		num = int(num)
		sum += x
	return sum
n = int(input())
big = 0
for i in range(1, n + 1):
	if n % i is 0:
		if sumn(big) < sumn(i):
			big = i
		elif sumn(big) == sumn(i):
			if big > i:
				big = i
			else:
				big = big
print(big)


import copy
n = int(input().strip())
arr = []
for x in range(1, n + 1):
	if n % x == 0:
		arr.append(x)
max_sum = [0] * (max(arr) + 1)
for y in range(len(arr)):
	(r, e) = (0, 0)
	e = copy.copy(arr[y])
	while e:
		(r, e) = (r + e % 10, e // 10)
	max_sum[arr[y]] = r
print(max_sum.index(max(max_sum)))


import sys
import math
n = int(input().strip())
factors = []
for i in range(2, math.ceil(n // 2) + 1):
	if n % i == 0:
		factors.append(i)
factors.append(n)
for i in range(len(factors)):
	string = str(factors[i])
	noval = 0
	for j in string:
		noval += int(j)
	factors[i] = (noval, factors[i])
factors.sort()
maxm = factors[-1][0]
lastarr = []
for i in reversed(range(len(factors))):
	if factors[i][0] == maxm:
		lastarr.append(factors[i][1])
	else:
		break
print(min(lastarr))


import sys

def divisors(n):
	result = [1, n]
	for i in range(2, n):
		if n % i == 0:
			result.append(i)
	return result

def digit_sum(n):
	if n == 0:
		return 0
	else:
		return n % 10 + digit_sum(n // 10)
n = int(input().strip())
divs = sorted([(d, digit_sum(d)) for d in divisors(n)], key=lambda x: (-x[1], x[0]))
print(divs[0][0])


import sys
n = int(input().strip())
best = (1, -1)
for x in range(1, n + 1):
	if n % x != 0:
		continue
	y = (sum((int(c) for c in str(x))), -x)
	if y > best:
		best = y
print(-best[1])


import sys
import math

def divisorGenerator(n):
	large_divisors = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			yield i
			if i * i != n:
				large_divisors.append(n / i)
	for divisor in reversed(large_divisors):
		yield int(divisor)
n = int(input().strip())
arr = list(divisorGenerator(n))
arr.reverse()
l = len(arr)
number = 0
summa = 0
for x in range(l):
	ss = str(arr[x])
	int_s = 0
	for i in ss:
		int_s += int(i)
	if int_s > summa:
		summa = int_s
		number = arr[x]
	elif int_s == summa:
		if arr[x] < number:
			number = arr[x]
print(number)


import sys
import functools

def divisors(n):
	return list(functools.reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def findBestDivisor(n):
	best = 0
	current = 0
	val = 0
	for i in range(0, len(n)):
		for ch in n[i]:
			current = current + int(ch)
		if current > best:
			best = current
			val = n[i]
		current = 0
	return val

def main():
	n = int(input().strip())
	div = divisors(n)
	list.sort(div)
	for i in range(0, len(div)):
		div[i] = str(div[i])
	print(findBestDivisor(div))
main()


import sys, math
max_sum = 1
n = int(input().strip())
min_num = 1
for x in range(1, int(math.sqrt(n) + 1)):
	if n % x == 0:
		d = n // x
		s = sum(map(int, str(x)))
		s2 = sum(map(int, str(d)))
		if s2 > s or (s2 == s and d < x):
			(s, x) = (s2, d)
		if s > max_sum or (s == max_sum and x < min_num):
			max_sum = s
			min_num = x
print(min_num)


import sys

def digitSum(num):
	add = 0
	while num != 0:
		add += num % 10
		num = int(num / 10)
	return add
n = int(input().strip())
li = []
digitArr = []
for i in range(1, n + 1):
	if n % i == 0:
		li.append(i)
		digitArr.append(digitSum(i))
m = max(digitArr)
print(li[digitArr.index(m)])


import sys

def ssum(x):
	return sum(map(int, str(x)))
n = int(input().strip())
m = 1
T = 1
for t in range(2, n + 1):
	if n % t == 0 and ssum(t) > m:
		m = ssum(t)
		T = t
print(T)


import sys

def seek_divs(x):
	div_set = set()
	d = 1
	while d <= x:
		if x % d == 0:
			div_set.add(d)
			div_set.add(int(x / d))
		d += 1
	return div_set
n = int(input().strip())
divisors = seek_divs(n)
sel_sum = 0
sel_div = 0
dict_sums = {}
for d in divisors:
	if len(str(d)) > 1:
		temp = list(str(d))
		temp = list(map(int, temp))
		s = sum(temp)
		dict_sums[str(d)] = s
	else:
		dict_sums[str(d)] = d
for (div, zums) in dict_sums.items():
	div = int(div)
	if zums > sel_sum:
		sel_div = div
		sel_sum = zums
	elif zums == sel_sum:
		if div < sel_div:
			sel_div = div
			sel_sum = zums
print(sel_div)


import sys
n = int(input().strip())
values = []
for x in range(1, n + 1):
	if n % x == 0:
		vale = sum([int(y) for y in list(tuple(str(x)))])
		values.append((x, vale))
val = sorted(values, key=lambda x: x[1], reverse=True)
print(val[0][0])


n = int(input().strip())
divisors = []
sums = []

def maxi(num):
	max = 0
	for i in num:
		if max < i:
			max = i
	return max
max = 0
for i in range(1, n // 2 + 1):
	if n % i == 0:
		divisors.append(i)
divisors.append(n)
for i in divisors:
	j = i
	total = 0
	while j > 0:
		total += j % 10
		j = j // 10
	sums.append(total)
maximum = maxi(sums)
for (pos, val) in enumerate(sums):
	if maximum == val:
		print(divisors[pos])
		break


import sys
from math import sqrt, floor

def dig_sum(n):
	ans = 0
	for ch in str(n):
		ans += int(ch)
	return ans
n = int(input().strip())
d = []
for i in range(1, floor(sqrt(n)) + 1):
	if n % i == 0:
		d.append((dig_sum(i), i))
		d.append((dig_sum(n // i), n // i))
d = sorted(d)
q = max(d)[0]
ans = 1000000000
for i in range(len(d)):
	if d[i][0] == q:
		if d[i][1] < ans:
			ans = d[i][1]
print(ans)


def func(a):
	sums = 0
	while a > 0:
		sums += a % 10
		a = a // 10
	return sums
n = int(input())
b = []
c = []
a = []
for i in range(1, int(n ** 0.5) + 1):
	if n % i == 0:
		a.append(i)
		a.append(n // i)
a = set(a)
a = list(a)
for i in a:
	b.append(func(i))
for i in range(len(a)):
	if b[i] == max(b):
		c.append(a[i])
print(min(c))


import math
n = int(input())
div = []
for i in range(1, n // 2 + 1):
	if n % i == 0:
		div.append(i)
div.append(n)
sm = [sum(map(int, str(num))) for num in div]
max_value = max(sm)
d = [[num, sum(map(int, str(num)))] for num in div]
maxarray = []
for x in range(len(d)):
	if d[x][1] == max_value:
		maxarray.append(d[x][0])
print(min(maxarray))


import sys
n = int(input().strip())
div = []
bnum = 1
for i in range(1, n + 1):
	if n % i == 0:
		val = 0
		x = i
		while i != 0:
			val += i % 10
			i = i // 10
		if val > bnum:
			bnum = val
			div = []
			div.append(x)
		elif val == bnum:
			div.append(x)
print(div[0])


import sys
import functools
n = int(input().strip())

def factors(n):
	return set(functools.reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def digitsum(n):
	sum = 0
	r = n
	while r > 0:
		(r, p) = divmod(r, 10)
		sum += p
	return sum

def best(a, b):
	(dsa, dsb) = (digitsum(a), digitsum(b))
	if dsa > dsb:
		return a
	elif dsa < dsb:
		return b
	elif a < b:
		return a
	else:
		return b

def solve(n):
	b = 0
	for f in factors(n):
		b = best(b, f)
	return b
print(solve(n))


import sys
n = int(input().strip())
m = 0
x = 1
for i in range(1, n + 1):
	if n % i == 0:
		s = 0
		k = i
		while k:
			s += k % 10
			k //= 10
		if s == m:
			x = min(x, i)
		elif s > m:
			x = i
			m = s
print(x)


import sys

def sum_digits(n):
	sum = 0
	while n:
		sum += n % 10
		n //= 10
	return sum
n = int(input().strip())

def choose_max(max_div, sum_max, div):
	sum_div = sum_digits(div)
	if sum_div > sum_max or (sum_div == sum_max and div < max_div):
		return (div, sum_div)
	return (max_div, sum_max)
(div1, div2) = (1, n)
(max_div, sum_max) = (1, 1)
while div1 < div2:
	if n % div1 == 0:
		div2 = n / div1
		(max_div, sum_max) = choose_max(max_div, sum_max, div1)
		(max_div, sum_max) = choose_max(max_div, sum_max, div2)
	div1 += 1
print(int(max_div))


import sys

def sumDigit(num):
	return sum([int(i) for i in str(num)])
n = int(input().strip())
winner = n
digitSum = sumDigit(n)
for t in range(1, n // 2 + 1):
	if n % t != 0:
		continue
	tempSum = sumDigit(t)
	if tempSum > digitSum:
		digitSum = tempSum
		winner = t
	elif tempSum == digitSum:
		winner = min(winner, t)
print(winner)


import sys

def divisorGenerator(n):
	for i in range(1, int(n / 2 + 1)):
		if n % i == 0:
			yield i
	yield n
BD = 0
x = 0
y = 0
n = int(input().strip())
if n > 0 and n <= pow(10, 5):
	for i in divisorGenerator(n):
		max = sum([int(k) for k in str(i)])
		y = int(i)
		if x == 0:
			x = int(i)
			BD = max
		elif BD < max:
			BD = max
			x = y
		elif BD == max:
			if x > y:
				x = y
print(x)


import sys
import math

def qsum(n):
	s = 0
	while n > 0:
		s += int(n % 10)
		n = int(n / 10)
	return s

def diviter(n):
	s = 0
	bestdiv = 1
	bestdiv_qsum = 1
	for i in range(1, int(n / 2) + 1):
		if n % i == 0:
			div_qsum = qsum(i)
			if div_qsum > bestdiv_qsum:
				bestdiv_qsum = div_qsum
				bestdiv = i
			elif div_qsum == bestdiv_qsum and i < bestdiv:
				bestdiv_qsum = div_qsum
				bestdiv = i
			i = n / i
			div_qsum = qsum(i)
			if div_qsum > bestdiv_qsum:
				bestdiv_qsum = div_qsum
				bestdiv = i
			elif div_qsum == bestdiv_qsum and i < bestdiv:
				bestdiv_qsum = div_qsum
				bestdiv = i
	return bestdiv
n = int(input().strip())
result = diviter(n)
print('%d' % result)


import sys

def divisorGenerator(n):
	for i in range(1, int(n / 2 + 1)):
		if n % i == 0:
			yield i
	yield n
BD = 0
x = 0
y = 0
n = int(input().strip())
if n > 0 and n <= pow(10, 5):
	for i in divisorGenerator(n):
		max = sum([int(k) for k in str(i)])
		y = int(i)
		if x == 0:
			x = int(i)
			BD = max
		elif BD < max:
			BD = max
			x = y
		elif BD == max:
			if x > y:
				x = y
print(x)


import sys
sumd = lambda n: sum([int(i) for i in str(n)])
n = int(input().strip())
div = 1
for i in range(1, n // 2 + 1):
	if n % i == 0:
		if sumd(i) > sumd(div):
			div = i
		elif sumd(i) == sumd(div) and i < div:
			div = i
if sumd(n) > sumd(div):
	div = n
print(div)


import sys
n = int(input().strip())
best = prev = 1
for i in range(1, n + 1):
	factor = n % i == 0
	digitS = sum(map(int, list(str(i))))
	if factor and digitS > prev:
		(best, prev) = (i, digitS)
	elif factor and digitS == prev and (i < best):
		best = i
print(best)


import sys
import math

def divisorGenerator(n):
	large_divisors = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			yield i
			if i * i != n:
				large_divisors.append(n // i)
	for divisor in reversed(large_divisors):
		yield divisor

def bestfinder(a, b):
	i = str(a)
	j = str(b)
	i = [int(a_temp) for a_temp in i]
	j = [int(a_temp) for a_temp in j]
	if sum(j) > sum(i):
		return True
	elif sum(i) == sum(j):
		if a > b:
			return True
		else:
			return False
	else:
		return False
n = int(input().strip())
val = 0
for i in divisorGenerator(n):
	val = i if bestfinder(val, i) else val
print(val)


def digits(k):
	res = 0
	while k:
		res += k % 10
		k //= 10
	return res

def order(k):
	return (-digits(k), k)
n = int(input().strip())
print(min([k for k in range(1, n + 1) if n % k == 0], key=order))


import sys
divisor_set = []
divisor_set_summed = []
n = int(input().strip())
for number in range(1, n + 1):
	if n % int(number) == 0:
		divisor_set.append(number)
for divisor in divisor_set:
	if divisor > 9:
		divisor_set_summed.append(sum([int(i) for i in str(divisor)]))
	else:
		divisor_set_summed.append(divisor)
max_summed_divisor = max(divisor_set_summed)
for number in divisor_set_summed:
	if number == max_summed_divisor:
		divisor_index = divisor_set_summed.index(max_summed_divisor)
		print(divisor_set[divisor_index])
		break


import sys
n = int(input().strip())
low_divisors = [i for i in range(1, round(n ** 0.5) + 1) if n % i == 0]
high_divisors = [n // i for i in low_divisors]
divisors = low_divisors + high_divisors
sums = {}
for d in divisors:
	ds = sum([int(i) for i in str(d)])
	if ds in sums:
		sums[ds].append(d)
	else:
		sums[ds] = [d]
print(sorted(sums[sorted(sums.keys(), reverse=True)[0]])[0])


import sys
n = int(input().strip())
foo = []
for k in range(1, n // 2 + 1):
	if n % k == 0:
		foo.append(k)
foo.append(n)
joo = []
for x in range(len(foo)):
	joo.append(sum(map(int, str(foo[x]))))
m = max(joo)
bar = [foo[i] for (i, j) in enumerate(joo) if j == m]
if len(bar) == 1:
	print(bar[0])
elif len(bar) > 1:
	print(min(bar))


import sys
n = int(input().strip())
divisors = [n]
best_num = 0
best_sum = 0

def get_sum(n):
	str_n = str(n)
	sum = 0
	for ch in str_n:
		sum += int(ch)
	return sum
for i in range(1, int(n / 2) + 1):
	if n % i == 0:
		divisors.append(i)
for divisor in divisors:
	sum_n = get_sum(divisor)
	if sum_n > best_sum:
		best_sum = sum_n
		best_num = divisor
	if sum_n == best_sum:
		if divisor < best_num:
			best_num = divisor
print(best_num)


n = int(input())
print(-max([(sum(map(int, str(d))), -d) for d in range(1, n + 1) if n % d == 0])[1])


import sys

def factors(n):
	from functools import reduce
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def sum_digits(num):
	result = 0
	while num != 0:
		result += num % 10
		num = int(num / 10)
	return result
n = int(input().strip())
fcs = list(factors(n))
max = 0
for i in range(0, len(fcs)):
	sum = sum_digits(fcs[i])
	summax = sum_digits(max)
	if sum == summax:
		if fcs[i] < max:
			max = fcs[i]
			continue
	if sum > summax:
		max = fcs[i]
print(max)


import math

def factor(i):
	if i < 10:
		return i
	f = 0
	while i != 0:
		f += i % 10
		i = i // 10
	return f
n = int(input().strip())
root = int(math.sqrt(n))
s = 0
s_factor = 0
for i in range(1, root + 1):
	if n % i == 0:
		for k in [i, n // i]:
			k_factor = factor(k)
			if k_factor > s_factor or (k_factor == s_factor and k < s):
				s = k
				s_factor = k_factor
print(s)


import sys
n = int(input().strip())
num = 0
smax = 0

def sumdig(number):
	return sum(map(int, str(number)))
for i in range(1, n + 1):
	if n % i == 0:
		t = sumdig(i)
		if t > smax:
			smax = t
			num = i
print(num)


import sys
from math import sqrt

def primary_score(d):
	str_d = str(d)
	score = sum(map(int, list(str_d)))
	return score

def secondary_score(d):
	return d

def compare(a, b):
	if primary_score(a) > primary_score(b):
		return a
	if primary_score(b) > primary_score(a):
		return b
	return a if secondary_score(a) < secondary_score(b) else b
n = int(input().strip())
sqrtn = int(sqrt(n))
best_divisor = n
for i in range(1, sqrtn + 1):
	if n % i == 0:
		best_divisor = compare(best_divisor, i)
		other = int(n / i)
		best_divisor = compare(best_divisor, other)
print(best_divisor)


import operator

def sum_of_digits(k):
	r = 0
	while k:
		r += k % 10
		k = k // 10
	return r
n = int(input())
if n < 2:
	print(n)
else:
	max_value = 0
	res = n - 1
	for i in range(n, 0, -1):
		if n % i == 0:
			next_max_value = sum_of_digits(i)
			if next_max_value >= max_value:
				max_value = next_max_value
				res = i
	print(res)


n = int(input())
ma = 1
ans = 1
for i in range(2, n + 1):
	if n % i == 0 and sum([int(j) for j in str(i)]) > ma:
		ans = i
		ma = sum([int(j) for j in str(i)])
print(ans)


import sys
import math

def divisors(n):
	lowest = []
	for i in range(1, math.ceil(n / 2) + 1):
		if n % i == 0:
			lowest.append(i)
	lowest.append(n)
	return lowest

def integer_sum(n):
	sum_so_far = 0
	n = str(n)
	for i in n:
		sum_so_far += int(i)
	return sum_so_far
n = int(input().strip())
reference_sum = integer_sum(n)
max_so_far = 0
for num in divisors(n):
	num_sum = integer_sum(num)
	if num_sum > max_so_far:
		max_so_far = num_sum
		r = num
print(r)


n = int(input().strip())
divs = [x for x in range(1, n + 1) if n % x == 0]
(mx, xx) = (0, 100000000)
for x in divs:
	sm = sum(map(int, list(str(x))))
	if sm > mx or (sm == mx and x < xx):
		(mx, xx) = (sm, int(x))
print(xx)


import sys

def sum_digits(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	return r
n = int(input().strip())
best = n
value = sum_digits(n)
for i in range(1, n // 2 + 1):
	if n % i == 0:
		tmp = sum_digits(i)
		if tmp > value:
			best = i
			value = tmp
		elif tmp == value and i < best:
			best = i
print(best)


n = int(input())
m = int(n ** (1 / 2))
maxuniq = 1
div = set({1})
for i in range(2, m + 1):
	if n % i == 0:
		if i // 10 == 0:
			if maxuniq < i:
				maxuniq = i
		div.add(i)
condiv = set()
for i in div:
	condiv.add(n // i)
div.update(condiv)
divlist = sorted(div)
maxsumlist = [(maxuniq, maxuniq)]
for i in divlist[divlist.index(maxuniq) + 1:len(divlist)]:
	s = 0
	j = i
	while True:
		s += j % 10
		j //= 10
		if j == 0:
			break
	maxsumlist.append((i, s))
a = max((x[1] for x in maxsumlist))
b = min((y for y in maxsumlist if y[1] == a))
print(b[0])


import sys
n = int(input().strip())
best = 1
bestdsum = 1
for x in range(2, n + 1):
	if n % x == 0:
		dsum = 0
		x2 = x
		while x2:
			dsum += x2 % 10
			x2 = x2 // 10
		if dsum > bestdsum:
			best = x
			bestdsum = dsum
print(best)


import sys

def factors(n):
	res = []
	val = int(n ** 0.5)
	for i in range(1, val + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return res

def sumdigit(n):
	x = n % 10
	s = 0
	while x:
		s += x
		n = n // 10
		x = n % 10
	return s
n = int(input().strip())
fact = factors(n)
(maxval, maxnum) = (0, 99999)
for i in fact:
	s = sumdigit(i)
	if s > maxval:
		maxval = s
		maxnum = i
	elif s == maxval:
		maxnum = min(maxnum, i)
print(maxnum)


def sumDigits(x):
	s = 0
	while x > 0:
		s += x % 10
		x //= 10
	return s
n = int(input())
print(-max([(sumDigits(i), -i) for i in range(1, n + 1) if n % i == 0])[1])


import sys

def print_factors(x):
	result = list()
	for i in range(1, x + 1):
		if x % i == 0:
			result.append(i)
	return result

def maximum_divisor(x):
	least_divisor = 0
	max_all_sum = 0
	for i in range(len(x)):
		sum_temp = all_sum(x[i])
		if sum_temp > max_all_sum:
			max_all_sum = sum_temp
			least_divisor = x[i]
		elif sum_temp == max_all_sum:
			if x[i] < least_divisor:
				least_divisor = x[i]
	return least_divisor

def all_sum(x):
	result = 0
	while x > 0:
		result += x % 10
		x //= 10
	return result
n = int(input().strip())
print(maximum_divisor(sorted(print_factors(n))))


import sys
n = int(input().strip())
a = []
for x in range(1, n // 2):
	if x not in a:
		if n % x == 0:
			a.append(x)
			a.append(n // x)
ma = 0
ans = 0
for x in range(1, len(a), +2):
	s = str(a[x])
	k = 0
	for y in s:
		k += int(y)
	if k > ma:
		ma = k
		ans = int(s)
	elif k == ma:
		if ans > int(s):
			ans = int(s)
print(ans if ans > 0 else n)


import sys

def summum(n):
	NumStr = str(n)
	val = 0
	for c in NumStr:
		val = val + int(c)
	return val
n = int(input().strip())
li = [1]
for i in range(1, n + 1):
	if n % i == 0 and summum(i) > summum(li[0]):
		li[0] = i
	elif n % i == 0 and summum(i) == summum(li[0]) and (i < li[0]):
		li[0] = i
print(li[0])


from math import sqrt

def quality(x):
	return (sum(map(int, str(x))), -x)

def divisors(n):
	for x in range(1, int(sqrt(n + 1)) + 1):
		if not n % x:
			yield x
			yield (n // x)
n = int(input())
print(max(divisors(n), key=quality))


n = input()
list_div = []
for i in range(1, int(n) + 1):
	if int(n) % i == 0:
		list_div.append(i)
sum_div = []
for i in list_div:
	t = str(i)
	t1 = [int(j) for j in t]
	sum_div.append(sum(t1))
print(list_div[sum_div.index(max(sum_div))])


import sys

def dig(num):
	val = num
	sum = 0
	while val > 0:
		sum = sum + val % 10
		val = val // 10
	return sum
l = []
n = int(input().strip())
temp = n // 2
if n == 1:
	print(1)
	exit()
for i in range(1, temp + 1):
	if n % i == 0:
		if i not in l:
			l.append(i)
			l.append(n // i)
		else:
			break
(max, val, sum) = (0, 0, dig(l[0]))
for i in range(len(l)):
	val = dig(l[i])
	if sum < val:
		sum = val
		max = i
	elif sum == val:
		if l[max] > l[i]:
			max = i
print(l[max])


def sumdigits(n):
	s = 0
	q = n
	while q > 0:
		s += q % 10
		q = q // 10
	return s
n = int(input().strip())
l = range(1, n // 2 + 1)
ressum = list([(x, sumdigits(x)) for x in l if n % x == 0])
ressum.append((n, sumdigits(n)))
maxdigits = max([y for (x, y) in ressum])
for (x, y) in ressum:
	if y == maxdigits:
		break
print(x)


import sys

def getCost(num):
	cost = 0
	while num > 0:
		cost += num % 10
		num = int(num / 10)
	return cost
n = int(input().strip())
best_number = 0
best_cost = 0
for i in range(1, n + 1):
	if n % i != 0:
		continue
	cost = getCost(i)
	if cost > best_cost:
		best_cost = cost
		best_number = i
print(best_number)


from functools import reduce
from collections import defaultdict
import sys

def find_best_divisor(n):
	factors = set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
	by_quality = defaultdict(list)
	for factor in factors:
		quality = sum([int(c) for c in str(factor)])
		by_quality[quality].append(factor)
	top = max(by_quality.keys())
	numbers = by_quality[top]
	return sorted(numbers)[0]
n = int(input().strip())
print(find_best_divisor(n))


import sys

def sum_digits(n):
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return s
n = int(input().strip())
best_sum = 1
best_div = 1
for i in range(1, n + 1):
	if n % i == 0 and sum_digits(i) > best_sum:
		best_sum = sum_digits(i)
		best_div = i
print(best_div)


import sys

def digit_sum(no):
	sum = 0
	while no != 0:
		sum += no % 10
		no = int(no / 10)
	return sum
n = int(input().strip())
ans = 1
divisors = []
for i in range(1, n + 1):
	if n % i == 0:
		divisors += [i]
for i in range(len(divisors) - 1):
	for j in range(i + 1, len(divisors)):
		if digit_sum(divisors[i]) < digit_sum(divisors[j]):
			temp = divisors[j]
		elif digit_sum(divisors[i]) > digit_sum(divisors[j]):
			temp = divisors[i]
		elif divisors[i] > divisors[j]:
			temp = divisors[j]
		else:
			temp = divisors[i]
		if digit_sum(temp) < digit_sum(ans):
			pass
		elif digit_sum(temp) > digit_sum(ans):
			ans = temp
		elif temp < ans:
			ans = temp
print(ans)


import sys

def getDivisors(n):
	divisors = [n]
	for i in range(1, n // 2 + 1):
		if n % i == 0:
			divisors.append(i)
	return divisors

def getDigitSum(digit):
	sum = 0
	for d in str(digit):
		sum += int(d)
	return sum

def formSumValueMapper(divisors):
	mapper_dict = {}
	for digit in divisors:
		digit_sum = getDigitSum(digit)
		if digit_sum in mapper_dict:
			value_to_sum = mapper_dict[digit_sum]
		else:
			value_to_sum = []
		value_to_sum.append(digit)
		mapper_dict[digit_sum] = value_to_sum
	return mapper_dict

def getOutput(values):
	max_value = -1
	min_value = float('Inf')
	dict_keys = values.keys()
	for key in dict_keys:
		if key > max_value:
			max_value = key
	values_against_sum = values[max_value]
	for value in values_against_sum:
		if value < min_value:
			min_value = value
	print(min_value)
n = int(input().strip())
getOutput(formSumValueMapper(getDivisors(n)))


import sys

def cal(n):
	n = str(n)
	k = list(n)
	ans = 0
	for x in k:
		ans += int(x)
	return ans
n = int(input().strip())
a = []
for i in range(1, n + 1):
	if n % i == 0:
		v = cal(i)
		a.append([v, i])
a.sort(reverse=True)
new = []
new.append(a[0])
for i in range(1, len(a)):
	if a[i][0] == a[i - 1][0]:
		new.append(a[i])
	else:
		break
if len(new) == 1:
	print(new[0][1])
else:
	print(new[len(new) - 1][1])


import sys

def compare(i, j):
	isum = sum((int(di) for di in str(i)))
	jsum = sum((int(dj) for dj in str(j)))
	if isum == jsum:
		return min(i, j)
	if isum > jsum:
		return i
	return j

def find_best(n):
	best = n
	for i in range(2, int(n / 2)):
		if n % i == 0:
			better = compare(i, int(n / i))
			best = compare(best, better)
	return best
n = int(input().strip())
print(str(find_best(n)))


from functools import reduce

def get_factors(x):
	sieve = [False] * 2 + [True] * (x - 1)
	for i in range(2, int(x ** 0.5) + 1):
		if sieve[i]:
			for j in range(i + i, x + 1, i):
				sieve[j] = False
	primes = [i for (i, p) in enumerate(sieve) if p]
	prime_factors = []
	for p in primes:
		if p * p > x:
			break
		while x % p == 0:
			prime_factors.append(p)
			x //= p
	if x > 1:
		prime_factors.append(x)
	else:
		prime_factors.append(1)
	return sorted([(i, prime_factors.count(i)) for i in set(prime_factors)])

def divisor_gen(n):
	factors = get_factors(n)
	nfactors = len(factors)
	f = [0] * nfactors
	while True:
		yield reduce(lambda x, y: x * y, [factors[x][0] ** f[x] for x in range(nfactors)], 1)
		i = 0
		while True:
			f[i] += 1
			if f[i] <= factors[i][1]:
				break
			f[i] = 0
			i += 1
			if i >= nfactors:
				return
n = int(input())
divisors = [i for i in divisor_gen(n)]
div_digits_sum = [(sum([int(dig) for dig in str(divisor)]), divisor) for divisor in divisors]
div_values = sorted(div_digits_sum, key=lambda x: (x[0], -len(str(x[1]))))
print(div_values[-1][1])


import sys
n = int(input().strip())
x = 0
t = []
for i in range(1, n + 1):
	if n % i == 0:
		a = str(i)
		t.append(a)
		if len(a) == 1:
			if i > x:
				x = i
		else:
			b = list(a)
			c = sum(list(map(int, b)))
			if c > x:
				x = c
for i in t:
	w = sum(list(map(int, i)))
	if w == x:
		print(i)
		break


def sum_digits(a):
	rv = 0
	while a > 0:
		rv += a % 10
		a //= 10
	return rv

def is_better(a, b):
	a_sum = sum_digits(a)
	b_sum = sum_digits(b)
	if a_sum > b_sum:
		return True
	if a_sum < b_sum:
		return False
	return a < b
n = int(input().strip())
best = 0
a = 1
while a * a <= n:
	if n % a == 0:
		if is_better(a, best):
			best = a
		if is_better(n // a, best):
			best = n // a
	a += 1
print(best)


import sys
n = int(input().strip())
ls = []
num = 0
tem = 0
for j in range(1, n // 2 + 1):
	if n % j == 0:
		ls.append(j)
ls.append(n)
for j in ls:
	add = 0
	for i in str(j):
		add += int(i)
	if add > tem:
		tem = add
		num = int(j)
print(num)


import sys
import math
n = int(input().strip())

def divisorGenerator(n):
	large_divisors = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			yield i
			if i * i != n:
				large_divisors.append(n / i)
	for divisor in reversed(large_divisors):
		yield divisor

def sum_of_digit(n):
	num_str = str(n)
	sum = 0
	for i in range(0, len(num_str)):
		sum += int(num_str[i])
	return sum
divs = list(divisorGenerator(n))
divs = [int(float(x)) for x in divs]
best = 1
for i in divs:
	if sum_of_digit(i) > sum_of_digit(best):
		best = i
	elif sum_of_digit(i) == sum_of_digit(best):
		if i < best:
			best = i
print(best)


import sys
n = int(input().strip())
(best, bests) = (1, 1)
for d in range(1, n + 1):
	if n % d:
		continue
	ds = sum(map(int, str(d)))
	if ds > bests:
		(best, bests) = (d, ds)
print(best)


import sys
import math

def divisorGenerator(n):
	large_divisors = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			yield i
			if i * i != n:
				large_divisors.append(n / i)
	for divisor in reversed(large_divisors):
		yield int(divisor)
n = int(input().strip())
x = list(divisorGenerator(n))
x.sort()
k = []
for i in x:
	k.append(sum(map(int, str(i))))
p = k.index(max(k))
print(x[p])


import sys
tot = 0
n = int(input().strip())

def dig_sum(a):
	return sum(map(int, list(str(a))))
for x in range(1, n + 1):
	if n % x == 0 and dig_sum(x) > tot:
		tot = dig_sum(x)
		v = x
print(v)


import sys
n = int(input().strip())
largest_sum = 1
best = 1
for i in range(2, n + 1):
	if n % i == 0:
		number = str(i)
		total = 0
		for digit in number:
			total += int(digit)
		if total > largest_sum:
			largest_sum = total
			best = i
print(best)


import sys
n = int(input().strip())
divisors = []
i = 1
while i <= n:
	if n % i == 0:
		divisors.append(i)
	i = i + 1
best_divisor = 1
for i in range(1, len(divisors)):
	curr_div = divisors[i]
	curr_arr = [int(i) for i in list(str(curr_div))]
	curr_sum = sum(curr_arr)
	best_div_arr = [int(i) for i in list(str(best_divisor))]
	best_div_sum = sum(best_div_arr)
	if curr_sum > best_div_sum:
		best_divisor = curr_div
	if curr_sum == best_div_sum:
		if curr_div < best_divisor:
			best_divisor = curr_div
print(best_divisor)


import sys
from itertools import chain, cycle, accumulate

def getdivisors(n):

	def prime_powers(n):
		for c in accumulate(chain([2, 1, 2], cycle([2, 4]))):
			if c * c > n:
				break
			if n % c:
				continue
			(d, p) = ((), c)
			while not n % c:
				(n, p, d) = (n // c, p * c, d + (p,))
			yield d
		if n > 1:
			yield (n,)
	r = [1]
	for e in prime_powers(n):
		r += [a * b for a in r for b in e]
	return r
n = int(input().strip())
ss = dd = 1
for d in getdivisors(n):
	s = sum(map(int, str(d)))
	if s > ss:
		ss = s
		dd = d
print(dd)


import sys
n = int(input().strip())
divisor = 0
fVal = 0
for i in range(1, n + 1):
	if n % i == 0:
		stri = str(i)
		tmp = 0
		for j in range(len(stri)):
			tmp += int(stri[j])
		if tmp > divisor:
			divisor = tmp
			fVal = i
print(fVal)