Problem:


Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.



Sample input:
10


Sample output:
5

Submission status: Accepted

Solution code:
import math
import itertools


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


n=int(input())
d = make_divisors(n)
len = len(d)
if len % 2 == 0:
    print((d[int(len/2)]-1)+(d[int(len/2-1)]-1))
else:
    print((d[int((len-1)/2)]-1)+(d[int((len-1)/2)]-1))

Submission status: Accepted

Solution code:
N=int(input())
import math
ans=int(math.sqrt(N))
for i in range(ans,0,-1):
    if N%i==0:
        print(i+N//i-2)
        break

Submission status: Accepted

Solution code:
N = int(input())

sqN = int(pow(N,1/2))
for i in range(sqN,1,-1):
    if N%i == 0:
        j=int(N/i)
        print(i+j-2)
        break
else:
    print(N-1)

Submission status: Accepted

Solution code:
import math
import bisect

x = int(input())
n = math.sqrt(x)

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

li = make_divisors(x)
li.sort()
alpha = bisect.bisect_left(li,n,hi = len(li))

if int(n) ** 2 == x:
  print(2 * int(n) - 2)
else:
  print(li[alpha -1] + li[alpha] - 2)

Submission status: Accepted

Solution code:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

divisors = make_divisors(N)
length = len(divisors)
if length % 2 == 1:
    ans = (divisors[length // 2] * 2) - 2
    print(ans)
    exit()

if length % 2 == 0:
    ans = divisors[(length // 2)-1] + divisors[length // 2] - 2
    print(ans)
    exit()

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))


n=one_int()

divisors = []
for i in range(int(n**0.5)+1, 0, -1):
    if n % i == 0:
        if i!=1:
            divisors.append((i,n//i,i+n//i -2))
        else:
            divisors.append((i,n//i, n//i -1))

# import math
# nn= int(math.sqrt(n))-1
# ans=[]
# min_ans=10**13
# for i in range(nn,n+1):
#     temp=n/i
#     if temp.is_integer():
#         if (int(temp) + i) < min_ans:
#             ans=[int(n/i),i]
#             min_ans = int(n/i)+i

# divisors

divisors=sorted(divisors,key=lambda x:x[2])[0]

print(divisors[2])

Submission status: Accepted

Solution code:
import math
n=int(input())
q=math.floor(n**0.5)
z=q+1
while n%z!=0:
  z-=1
ee=n/z
print(int((ee+z)-2))

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors



n = int(input())
a=make_divisors(n)
if len(a)%2==0:
    print(a[-1]+a[-2]-2)
else:
    print(a[-1]*2-2)

Submission status: Accepted

Solution code:
x = int(input())
cand_list = []

for i in range(1, int(x**(1/2))+1):
    if x%i==0:
      cand_list.append([i, x//i])

ans = min([sum(cand_i)-2 for cand_i in cand_list])
if x == 2:
  ans = 1
elif x == 3:
  ans = 2

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

def divisors(a):
	divisors=[]
	for i in range(1,int(a**0.5)+1):
		if a%i==0:
			divisors.append(i)
			if i!=a//i:
				divisors.append(a//i)
	divisors.sort() # 昇順
	# divisors.sort(reverse=True) # 降順
	return divisors

d=divisors(N)
l=len(d)
if l%2==0:
  a=d[int(l/2)]+d[int(l/2-1)]
else:
  a=d[int((l-1)/2)]*2
print(a-2)

Submission status: Accepted

Solution code:
N = int(input())
ans = N-1
for i in range(1, min(10**6+1, N)):
  if N % i == 0:
    a = N // i
    ans = min(ans, i + a - 2)
print(ans)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

n=int(input())
l = make_divisors(n)
if len(l)%2==0:
    print((l[len(l)//2-1]-1)+(l[len(l)//2]-1))
else:
    print((l[len(l)//2]-1)+(l[len(l)//2]-1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

# 整数の入力
N = int(input())

nn = np.sqrt(N)
ni = int(nn)

lis = []
for i in range(1, ni+1):
  if(N % i == 0):
    lis.append(i)

ii = lis[-1]
jj = int(N / ii)
step = (ii-1) + (jj-1)
# 出力
print("{}".format(step))

Submission status: Accepted

Solution code:
N =int(input())
Lnum = 1
for i in range(1,int(N**0.5)//1+1):
    if N%i == 0:
        Lnum = i
print (Lnum - 1 + N//Lnum - 1)

Submission status: Accepted

Solution code:
n=int(input())
mini = (10** 12)+1
X=0
Y=0
t=0
K=0
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    return divisors
ans = make_divisors(n)
ans.sort()
K+=len(ans)
if K%2:
  for i in range(int((K+1)/2)):
    t=-(i+1)
    if mini>ans[t]-ans[i] and n==ans[t]*ans[i] and ans[t]>=ans[i]:
      mini =ans[t]-ans[i]
      X=ans[t]
      Y=ans[i]
else:
  for i in range(int(K/2)):
    t=-(i+1)
    if mini>ans[t]-ans[i] and n==ans[t]*ans[i] and ans[t]>=ans[i]:
      mini =ans[t]-ans[i]
      X=ans[t]
      Y=ans[i]
print(X-1+Y-1)

Submission status: Accepted

Solution code:
n = int(input())
sq = int(n ** 0.5)
ans = n - 1
for i in range(sq, 1, -1):
    if n % i == 0:
        ans = i + n // i - 2
        break
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
r = math.floor(math.sqrt(n))

for i in range(r, 0, -1):
  if n % i == 0:
    print(i + n // i -2)
    exit()

Submission status: Accepted

Solution code:
import math
a=int(input())
kyori=a
dest = int(math.sqrt(a))
for i in range(2,dest+1):
  if a % i == 0:
    if i+(a//i)-2 < kyori:
      kyori=i+(a//i)-2
if kyori==a:
  print(a-1)
else:
  print(kyori)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
N = int(input())
l = make_divisors(N)
l.sort()
if len(l)%2 == 0:
    a = l[(len(l)//2)-1]-1
    b = l[(len(l)//2)]-1
    print(a+b)
else:
    a = l[(len(l)//2)]-1
    print(a*2)

Submission status: Accepted

Solution code:
import math

n=int(input())
m=int(math.sqrt(n))
for i in reversed(range(1,m+1)):
	if n%i==0:
		print(int(i+n/i-2))
		break

Submission status: Accepted

Solution code:
n = int(input())
m = 9999999999999
for i in range (1, int(n ** 0.5 + 1)):
    if n % i == 0:
        m = min(m, i + n // i - 2)
print(m)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


def LI(): return list(map(int, input().split()))


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    A, B = LI()

    if A >= 1 and A <= 9 and B >= 1 and B <= 9:
        print(A*B)
    else:
        print(-1)
    return

# B


def B():
    N = II()
    if N <= 81 and N > 9:
        for i in range(2, 10):
            if N % i == 0 and N/i <= 9:
                print('Yes')
                return

    elif N > 81:
        print('No')
        return
    else:
        print('Yes')
        return
    print('No')

    return

# C


def C():
    N = II()
    
    
    L=[]
    count=0
    for i in range(2,int(sqrt(N)+2)):  
        if N%i==0:
            L.append(N/i)
            count=1
    if count==0:
        print(N-1)
    else :
        print(int(L[-1]-2+(N/L[-1])))


            

    
   

    return

# D


def D():
    N = II()
    L = LI()
    L.sort()

    count = 0
    for i in range(N-2):

        for j in range(i+1, N-1):

            p = L[i]+L[j]

            count += sum(x < p for x in L)-j-1

    print(count)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    N = len(S)

    pass


# Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

Solution code:
n = int(input())
x, y = 1,1

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

s = make_divisors(n)
l  = []
res = 10e+12
for i in range(len(s)):
    a = s[i] - x+  n // s[i] - y 
    if res > a:
        res = a
print(res)

Submission status: Accepted

Solution code:
k = int (input())

def make_divisors(n):
	divisors = []
	for i in range(1, int(n**0.5)+1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n//i)

	# divisors.sort()
	return divisors

a =  make_divisors(k)
a.sort()
mini = 10000000000000
for i in range (len(a)):
	ii = a[i]
	jj = k / a[i]
	wa = ii - 1 + jj - 1
	mini = min(wa, mini)
	if a[i] > k//2:
		break
print (int(mini))

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def prime_decomposition(n): #素因数分解
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(int(n))
  return table

def make_divisors(n): #約数列挙
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors




n = onem()

k = make_divisors(n)
k.sort()

ans = Max

for i in range(len(k)):
    if ans >= n//k[i] + k[i] - 2:
        ans = n//k[i] + k[i] - 2
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
sq = int(n ** 0.5)
ans = n - 1
for i in range(sq, 0, -1):
    if n % i == 0:
        ans = i + n // i - 2
        break
print(ans)

Submission status: Accepted

Solution code:
import math

import sys
import math

def Sieve_of_Eratosthenes(N):
    if N<2: return set()
    prime=set(i for i in range(2,N+1))
    for i in range(2,math.ceil(N**0.5)+1):
        tmp=set()
        for j in prime:
            if j>i and j%i==0: tmp.add(j)
        for j in tmp:
            prime.remove(j)

    return list(prime)

N=int(input())


s=int(math.sqrt(N))+2
d={}
ans=N-1
for i in range(1,s):
    if N%i==0:
        ans=min(ans,i+N//i-2)

print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import math


def resolve():
    N = int(input())
    A = 1
    B = 1

    center = math.ceil(math.sqrt(N))
    for i in range(1, center + 1)[::-1]:
        if N % i == 0:
            A = i
            B = int(N / i)
            break
        else:
            continue

    result = A - 1 + B - 1
    print(result)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """10"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """50"""
        output = """13"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10000000019"""
        output = """10000000018"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
lim = int(n**0.5)+1
while(lim != 1):
    if n%lim == 0:
        break
    lim -= 1
print(lim+n//lim-2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            divisors.append(n//i)

    return divisors


divisors = make_divisors(int(input()))
sub_list = []
for i in range(1, len(divisors), 2):
    sub_list.append(divisors[i] - divisors[i-1])

min_index = sub_list.index(min(sub_list))
print(divisors[min_index * 2] + divisors[(min_index+1) * 2 - 1] - 2)

Submission status: Accepted

Solution code:
n = int(input())

res = 10**20
for i in range(1,10**6+1):
    if n%i == 0:
        res = min(res,i+n//i)
print(res-2)

Submission status: Accepted

Solution code:
# C
N = int(input())
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
ans = N - 1
lst = make_divisors(N)
if (len(lst) % 2 == 0):
    for i in range(0, len(lst), 2):
        num = lst[i] + lst[i+1] - 2
        ans = min(ans,num)
else:
    for i in range(0, len(lst)-1, 2):
        num = lst[i] + lst[i+1] - 2
        ans = min(ans,num)
    ans = min(ans,2*lst[-1] - 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

root_N = int( N**(1/2) )

for n in reversed( range(1, root_N+1) ):
    if N%n == 0:
        n_x = n
        n_y = int(N/n)
        break
print(n_x+n_y-2)

Submission status: Accepted

Solution code:
N = int(input())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


div = make_divisors(N)

if div[-1] ** 2 == N:
    print(div[-1]*2 - 2)

else:
    x = div[-2] - 1
    y = div[-1] - 1

    print(x + y)

Submission status: Accepted

Solution code:
N = int(input())
sqrt = 1
for i in range(1,N+1):
    if((i**2)<=N and ((i+1)**2>N)):
        sqrt = i
        break

        
for i in range(sqrt):
    if(N%(sqrt-i)==0):
        a = sqrt-i
        break

b = int(N/a)
print(a-1+b-1)

Submission status: Accepted

Solution code:
import math
n = int(input())
move = 0
m = math.ceil(n ** (1/2))
for i in range(1, (m+1)):
    if n %  i == 0:
        move = i + (n/i) - 2
    else:
        pass
print(int(move))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math

# input
N = int(input())

# solve
def divisor(n):
    i = 1
    l = list()
    while i * i <= n:
        if n % i == 0:
            l.append(i)
            l.append(n // i)
        i += 1
    l = list(set(l))
    return l

l = sorted(divisor(N))
ans = 10 ** 12
for i in range(math.ceil(len(l) / 2)):
    ans = min(l[i] - 1 + l[len(l) - i - 1] - 1, ans)

# output
print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
limit = round(math.sqrt(N))+2
factor1 = 0
for i in range(1,limit):
    if N % i == 0:
        factor1 = i
factor2 = N // factor1
print(factor1-1+factor2-1)

Submission status: Accepted

Solution code:
N = int(input())
i = 1

ans = 1 + N - 2
while True:
    if i * i > N:
        break
    if N % i == 0:
        ans = min(ans, i + N//i - 2)
    i += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
ans=1e12
l=int(n**0.5)+2
for i in range(1,l):
  if n%i==0 and i not in a:
    a.append(i)
    a.append(n//i)
    ans=min(ans,(i+n//i-2))
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import ceil, sqrt

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())

sys.setrecursionlimit(10 ** 9)
INF = float('inf')


def main():
    N = INT()

    num = [i + 1 for i in range(ceil(sqrt(N)))]

    minimum = INF
    for i in num:
        if N % i == 0:
            temp = i + N//i
            if temp < minimum:
                minimum = temp

    print(minimum - 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def prime(n1):
	for i in range(2, int(math.sqrt(n1))+1):
		if n1%i == 0:
			return False
	return True

n = int(input())
if prime(n):
	print(n - 1)
else:
	k = sorted(list(factors(n)))
	#print(k)
	if len(k) % 2 != 0:
		if k[len(k)//2]*k[len(k)//2] == n:
			print(2*k[len(k)//2] - 2)
		#else:

	else:
		s = k[len(k)//2 - 1] + k[len(k)//2]
		print(s - 2)

Submission status: Accepted

Solution code:
import math
n = int(input())
minimum = n + 2
for x in range(1,int(math.sqrt(n)) + 1):
  if (n % x) == 0 and (n / x) + x < minimum:
    minimum = int(n / x) + x
print(minimum - 2)

Submission status: Accepted

Solution code:
import math
N=int(input())
for i in reversed(range(int(math.sqrt(N)))):
    if N%(i+1)==0:
        print(N//(i+1)+i+1-2)
        break

Submission status: Accepted

Solution code:
n = int(input())
ans = 10**12
List = []
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        List.append(i)
for i in range(len(List)):
    s = List[i] + n / List[i] - 2
    ans = min(ans, s)
print(int(ans))

Submission status: Accepted

Solution code:
N=int(input())
a=int(N**(1/2))
for i in reversed(range(a)):
    if N%(i+1)==0:
        print(int(N/(i+1)+i-1))
        break

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(1,int(n**0.5)+1):
    if n % i == 0:
        a.append(i+(n/i)-2)
ans = int(min(a))
print(ans)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

from math import sqrt
N = int(input())
a = make_divisors(N)

l = len(a)
if (l%2 == 0):
    print(a[-1] + a[-2] - 2)
else:
    print(a[-1]*2 - 2)

Submission status: Accepted

Solution code:
import math
n = int(input())
pair = []
divisors = []
for i in range(1, int(n**0.5)+1):
  if n % i == 0:
    divisors.append(i)
    if i != n // i:
      divisors.append(n//i)
ans = float('inf')
for i in divisors:
  ans = min(ans, i + int(n / i))
print(ans-2)

Submission status: Accepted

Solution code:
import math
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors
N = int(input())
arr = make_divisors(N)
# print(arr)
if len(arr) % 2 == 1:
    print(arr[-1]*2-2)
else:
    print(arr[-1]+arr[-2]-2)

Submission status: Accepted

Solution code:
import math

N = int(input())

a = int(math.sqrt(N))
b = 1

if N % a == 0:
	b = int(N / a)
else:
	a -= 1
	while a > 0:
		if N % a == 0:
			b = int(N / a)
			break
		a -= 1

if a*b == N:
	print(a + b -2)

Submission status: Accepted

Solution code:
import math
N= int(input())
M= int(math.sqrt(N)) + 1
for i in range(1,M)[::-1]:
    if N % i == 0:
        print(i + N //i - 2)
        break
else:
    print(N-1)

Submission status: Accepted

Solution code:
import math


def is_prime(q):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q & 1 == 0: return False
    return pow(2, q - 1, q) == 1


def main(n):
    if is_prime(n):
        return n - 1
    else:
        sqrt = int(math.sqrt(n))
        for i in range(sqrt, 1, -1):
            div, mod = divmod(n, i)
            if mod == 0:
                return i + div - 2


if __name__ == '__main__':
    N = int(input())
    print(main(N))

Submission status: Accepted

Solution code:
n = int(input())

lis = []
for i in range(1,int((n**0.5) + 1)):
    if n % i == 0:
        lis.append(i)
        if i != n //i:
            lis.append(n//i)
            
lis.sort()
n2 = len(lis)
if n2 % 2 == 0:
    n2 = n2//2
    key = True
else:
    n2 = n2//2
    key = False

if key == True:
    a = lis[n2]
    b = lis[n2 -1]
else:
    a = lis[n2]
    b = lis[n2]
    
print(a-1+b-1)

Submission status: Accepted

Solution code:
N=int(input())
def divisor(n): #nの約数を全て求める
  i = 1
  table = []
  while i * i <= n:
    if n%i == 0:
      table.append(i)
      table.append(n//i)
    i += 1
  table = sorted(list(set(table)))
  l=len(table)
  if l==2:
      return n-1
  elif l%2==1:
      a=0
      x=l//2
  else:
      a=1
      x=l//2-1
      y=l//2
  if a==0:
    return table[x]*2-2
  if a==1:
    return table[x]+table[y]-2
print(divisor(N))

Submission status: Accepted

Solution code:
N = int(input())

result = None
for i in range(1, int(N**0.5)+1):
    if N % i == 0:
        j = i + (N // i)
        if result is None or j < result:
            result = j

print(result - 2)

Submission status: Accepted

Solution code:
def make_divisors(n):
   divisors = []
   for i in range(1, int(n**0.5)+1):
       if n % i == 0:
           divisors.append(i)
           if i != n // i:
               divisors.append(n//i)
   return divisors

n = int(input())
divisors = make_divisors(n)
steps = min(d + (n // d) for d in divisors)
print(steps - 2)

Submission status: Accepted

Solution code:
N = int(input())
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
if (make_divisors(N)[-1])**2 ==N:
  print(2*make_divisors(N)[-1]-2)
else:
  print(make_divisors(N)[-1]+make_divisors(N)[-2]-2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    divisors.sort()
    return divisors


N = int(input())

list_y = make_divisors(N)
#  print(list_y)
point = len(list_y)
if point % 2 == 0:
    print(list_y[point // 2] + list_y[point // 2 - 1] - 2)
else:
    print(list_y[point // 2] * 2 - 2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            divisors.append(n//i)

    divisors.sort()
    return divisors

n = int(input())

factor = make_divisors(n)
print((factor[len(factor)//2 - 1] - 1) + (factor[len(factor)//2] - 1))

Submission status: Accepted

Solution code:
import math
n=int(input())
a=int(math.sqrt(n))
while True:
  if n%a==0:
    break
  a-=1
print(a+int(n/a)-2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())

div_num = []
for i in range(1,int(N**0.5)+1):
    if N % i == 0:
        div_num.append(i)
        if i != N // i:
            div_num.append(N//i)

print(min([i+j for i,j in zip(sorted(div_num),sorted(div_num)[::-1])])-2)

Submission status: Accepted

Solution code:
n = int(input())

ans = 10 ** 12 + 2
for a in range(1, int(n ** 0.5) + 1):
    b = n // a
    if n % a == 0 and a + b < ans:
        ans = a + b

print(ans - 2)

Submission status: Accepted

Solution code:
import sys
import math
n = int(input())
tmp = 10**12

for i in range(int(math.sqrt(n))+1, 0, -1):
    if n % i != 0:
        continue
    else:
        if tmp > (n//i)+(i)-2:
            tmp = (n//i)+i-2
print(tmp)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())

def divisor(n): #nの約数を全て求める
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(set(table))
    return table
a = divisor(N)
b =  10**13

for i in a:
    b = min(b,i + (N / i))
    
print(int(b-2))

Submission status: Accepted

Solution code:
n = int(input())
def yks(n):
    yakusu = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            yakusu.append(i)
            if i != n // i:
                yakusu.append(n//i)
    yakusu.sort()
    return yakusu
g=yks(n)
p=len(yks(n))//2
if g[p]*g[p-1]==n:
    print(g[p]+g[p-1]-2)
else:
    print(2*g[p]-2)

Submission status: Accepted

Solution code:
n=int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
p=make_divisors(n)

l=len(p)
if l%2==1:
  ans=p[l-1]*2-2
  
else:
  ans=p[l-1]+p[l-2]-2
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
min=n*2
for i in range(1,int((n)**(1/2))+1):
  if n%i==0:
    sum=i+n//i
    if min>sum:
      min=sum
print(sum-2)

Submission status: Accepted

Solution code:
N=int(input())

nums = []
for i in range(1,int(N**0.5)+1):
    if N % i == 0:
        nums.append(N//i-1 + i-1)

print(min(nums))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from math import ceil, sqrt

def solve():
    N = int(input())
    for i in range(ceil(sqrt(N)),0,-1):
        if N%i:
            continue
        else:
            return str(i+N//i-2)
        
if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import math
N = int(input())
j = int(N)
start = int(math.sqrt(N))

for i in range(start,0,-1):
  if N % i == 0:
    j = N // i
    break

print(i + j - 2)

Submission status: Accepted

Solution code:
n = int(input())

a = [1, n]

for i in range(1, int(n**0.5)+1):
    j = n // i
    if n % i == 0 and a[0] + a[1] > i + j:
        a = [i, j]

print(a[0] - 1 + a[1] - 1)

Submission status: Accepted

Solution code:
N=int(input())
ans=10**12
for i in range(1,1000001):
    if N%i==0:
        ans=min(ans,int((i-1)+N/i-1))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
#print(N**0.5)
tate = 0
for i in range(1,int(N**0.5)+1):
    if N % i == 0:
        tate = i
yoko = N // tate
print(yoko + tate -2)

Submission status: Accepted

Solution code:
n=int(input())
import math
list=[ ]

for i in range(1,math.floor(n**0.5)+1):
  if (n/i)%1==0:
    j=n/i
    list.append(i+j)

print(int(min(list)-2))

Submission status: Accepted

Solution code:
import math
N=int(input())

sq=math.floor(math.sqrt(N))

cond=0
for j in reversed(range(2,sq+1)):
  if N%j==0:
    print(j+N//j-2)
    cond=1
    break

if cond==0:
  print(N-1)

Submission status: Accepted

Solution code:
import math
n=int(input())
a=0
b=0

n_rt=math.sqrt(n)
if float.is_integer(n_rt)==True:
    print(2*int(n_rt-1))
else:
    for i in range(int(n_rt//1)):
        tmp=n%(n_rt//1-i)
        if tmp==0:
            y=int(n/(n_rt//1-i))
            print(int((n_rt//1-i)+y-2))
            break

Submission status: Accepted

Solution code:
import sys
import math

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


# def factorization(n):
#     factor = []
#     tmp = int(math.sqrt(n)) + 1
#     for i in range(2, tmp):
#         while n % i == 0:
#             n //= i
#             factor.append(i)
#         if len(factor) != 0:
#             factor.append(n)
#             return factor
#     return []


def solve(inputs):
    N = int(inputs[0])
    # factors = factorization(N)
    min_diff = None
    for i in range(1, 1000001):
        if N % i == 0:
            a = N // i
            diff = a + i - 2
            if min_diff is None or min_diff > diff:
                min_diff = diff
    return min_diff


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)