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:
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 __name__ == "__main__":
    li = list(map(int,input().split()))
    a=li[0]
    r=[]
    ret=a-1

    r=make_divisors(a)
    if(len(r)%2==1):
        print(r[-1]*2-2)
    else:
        for i in range(0,int(len(r)/2)):
            if(r[i*2]+r[i*2+1]-2<ret):
                ret=r[i*2]+r[i*2+1]-2
        print(ret)

Submission status: Accepted

Solution code:
import math
def main1():
    input_num = int(input())
    num_sqrt = int(math.sqrt(input_num))

    minimam = input_num
    for i in range(num_sqrt+1,0,-1):
        q, mod = divmod(input_num, i)
        if mod == 0:
            distance = q + i -2
            if minimam > distance:
                minimam = distance
    print(minimam)
main1()

Submission status: Accepted

Solution code:
import math
input_num = int(input())
num_sqrt = int(math.sqrt(input_num))

minimam = input_num
for i in range(num_sqrt+1,0,-1):
    q, mod = divmod(input_num, i)
    distance = q + i -2
    if mod == 0 and minimam > distance:
        minimam = distance
print(minimam)

Submission status: Accepted

Solution code:
import math
input_num = int(input())
num_sqrt = int(math.sqrt(input_num))+1

minimam = input_num
for i in range(1,num_sqrt+1):
    q, mod = divmod(input_num, i)
    distance = q + i -2
    if mod == 0 and minimam > distance:
        minimam = distance
print(minimam)

Submission status: Accepted

Solution code:
num = int(input())
minimam = 10**12
for i in range(1,10**6+1):
    q, mod = divmod(num, i)
    distance = q + i -2
    if mod == 0 and minimam > distance:
        minimam = distance
print(minimam)

Submission status: Accepted

Solution code:
import math
n = int(input())
n1 = int(math.sqrt(n)) + 1
for i in range(n1):
  if n%(n1-i) == 0:
      a = n1-i
      b = n//a
      print(a+b-2)
      exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

for i in range(int(n ** 0.5)):
  if n % (int(n ** 0.5) - i) == 0:
    j = n // (int(n ** 0.5) - i)
    break
    
print(int(n ** 0.5) - i + j - 2)

Submission status: Accepted

Solution code:
import math
N = int(input())
len = math.floor(N**0.5)
for i in range(len):
    if N%(len-i) == 0:
        j = N//(len-i)
        break
print(int(len-i+j-2))

Submission status: Accepted

Solution code:

a = int(input())
j=0
mass=1000000000000
for i in range(round(a**(1/2))):
    j = j+1
    h=a/j
    if (h.is_integer()):    
        mass2=j+h-2
        if(mass>mass2):
            mass=mass2
        

    

print(int(mass))

Submission status: Accepted

Solution code:
import math
n = int(input())
half = math.floor(n**0.5)
#print(half)

num1 = 0
for i in range(half+1,0,-1):
  if n % i == 0:
    num1,num2 = i,n//i  
    #print("Yes")
    break

if num1 == 0:
  print(n-1)
else:
  print(num1+num2-2)

Submission status: Accepted

Solution code:
import math 
 
n = int(input())
m = int(round(math.sqrt(n)))
while m >= 1:
    p, q = divmod(n, m)
    if q == 0:
        print(m + p - 2)
        break
    else:
        m -= 1

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
k = np.sqrt(N).astype(np.int)
for i in range(k,0,-1):
    if N % i == 0:
        print(i + N//i - 2)
        break

Submission status: Accepted

Solution code:
import math
import sys
input = sys.stdin.readline


def main():
    sc = Scan()
    n = sc.intarr()[0]
    ns = int(math.sqrt(n)) + 1
    minimum = n
    for i in range(1, ns):
        if n % i == 0:
            cost = (n//i) + i - 2
            if cost < minimum:
                minimum = cost
    print(minimum)


class Scan():

    def intarr(self):
        line = input()
        array = line.split(' ')
        num_array = [int(n) for n in array]
        return num_array

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


main()

Submission status: Accepted

Solution code:
import math
import sys
input = sys.stdin.readline


def main():
    sc = Scan()
    n = sc.intarr()[0]
    ns = int(math.sqrt(n)) + 1
    minimum = n
    for i in range(1, ns):
        if n % i == 0:
            cost = (n//i) + i - 2
            if cost < minimum:
                minimum = cost
    print(minimum)


class Scan():

    def intarr(self):
        self.line = input()
        array = self.line.split(' ')
        num_array = [int(n) for n in array]
        return num_array

    def strarr(self):
        self.line = input()
        array = self.line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


main()

Submission status: Accepted

Solution code:
import math

def dictcheck(dict):
    min = 0
    for k,v in dict.items():
        if k == 1:
            min = v
            break
        elif min == 0:
            min=(int(k)-1)+(int(v)-1)
        elif min > (int(k)-1)+(int(v)-1):
            min = (int(k)-1)+(int(v)-1)
        else:
            continue
    print(min)

def main():
    N = int(input())
    i = 2
    tmpdict= {}
    while i <= math.sqrt(N):
        if N % i == 0:
            tmpdata = {i:N/i}
            tmpdict.update(tmpdata)
            i+=1
        else:
            i+=1
    if len(tmpdict)==0:
        tmpdata = {1:N-1}
        tmpdict.update(tmpdata)
#    print(tmpdict)
    dictcheck(tmpdict)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
q = []

for i in range(1,min(N,2+math.ceil(math.sqrt(N)))):
    if N%i==0:
        q.append(i+int(N/i))

print(min(q)-2)

Submission status: Accepted

Solution code:
import math

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

for i in range(n_sqrt, 0, -1):
    if n % i == 0:
        yoko, tate = i, n//i
        break

print(yoko + tate - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input(''))
N_max=int(N**0.5)

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

print(int(A-2))

Submission status: Accepted

Solution code:
import math

raw = input()
N = int(raw)
n = math.sqrt(N)
a,b = math.modf(n)
m = int(b)

count = N - 1

for i in range(m,1,-1):
    if (N % i)==0:
        ans = i + (N // i) -2
        count = ans
        break

print(count)

Submission status: Accepted

Solution code:
N=int(input())

i=int(N**0.5);

while i>0:
    if N%i==0:
        i,j=(i,N/i)
        break
    i-=1

print(int(i+j-2))

Submission status: Accepted

Solution code:
n=int(input())

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

	# divisors.sort()
	return divisors
	
print(sum(make_divisors(n)[-1])-2)

Submission status: Accepted

Solution code:
s = int(input())
min = s - 1
sqr = s ** 0.5
i = int(sqr)
while i >= 1:
    if s % i == 0:
       t = i + (s // i) - 2
       break
    i -= 1
print(t)

Submission status: Accepted

Solution code:
import math

N = int(input())

# n = p * q → p or q <= n**(1/2)
# because p and q > n**(1/2) → p * q > n

divisor = []
for i in range(1 , math.floor(N**(1/2))+1):
    if N % i == 0:
        divisor.append(i)

distance = []
for j in range(int(len(divisor))):
    M = (N / int(divisor[j])) + int(divisor[j])
    distance.append(M)

if int(len(divisor)) == 1:
    print(N-1)
else:
    print(math.floor(min(distance))-2)

Submission status: Accepted

Solution code:
import math

N = int(input())

divisor = []
for i in range(1 , math.floor(N**(1/2))+1):
    if N % i == 0:
        divisor.append(i)

distance = []
for j in range(int(len(divisor))):
    M = (N / int(divisor[j])) + int(divisor[j])
    distance.append(M)

if int(len(divisor)) == 1:
    print(N-1)
else:
    print(math.floor(min(distance))-2)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import math

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

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

Submission status: Accepted

Solution code:
n = int(input())

x = 1
ans = float('inf')
while x * x <= n:
    if n % x == 0:
        y = n // x
        ans = min(ans, x - 1 + y - 1)
    x += 1
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt, ceil

n = int(input())
for i in range(ceil(sqrt(n)),0, -1):
    if n%i == 0:
#        print('Yes',i, n//i)
        print(i+(n//i)-2)
        exit()

Submission status: Accepted

Solution code:
import math
x=int(input())
s=set()
y=int(math.sqrt(x))
for i in range(y,0,-1):
  if x%i == 0:
    a=i+x//i-2
    s.add(a)
    if min(s)-a <0:
      break
print(min(s))

Submission status: Accepted

Solution code:
from sys import stdin
input = int(stdin.readline().rstrip())
# search_area = int(input / 2) + 1
search_area = 1
tf = True
output = (input - 1)
while(tf):
    if (input % search_area) == 0:
        output_temp = int(search_area + (input / search_area) - 2)
        if output > output_temp:
            output = output_temp
    search_area += 1
    if search_area > (input)**(1/2) + 1:
        tf = False

print(output)

Submission status: Accepted

Solution code:
a=int(input())
c=a+1
for i in range(1,10000000,1):
    if a%i==0:
        b=a/i
        if i+b<=c:
            c=i+b
print(int(c-2))

Submission status: Accepted

Solution code:
import numpy as np

A = int(input())
A_sq = int(np.sqrt(A))
for i in range(A_sq):
  if A % (A_sq - i) == 0:
    print(int(A_sq - i + A / (A_sq - i) - 2))
    break

Submission status: Accepted

Solution code:
n = int(input())
ans = n - 1
a = 1
while a * a <= n:
  if n % a == 0:
    ans = min(ans, a - 1 + (n // a) - 1)
  a += 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np 
N = int(input())

n = int(np.sqrt(N)) 
ans_list = []
for i in range(1, n + 1):
    if N % i == 0:
        k = int(i + (N / i) - 2) 
        ans_list.append(k) 
    else:
        pass

print(min(ans_list))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
count = 0
ans = []
for i in range(2, int(np.sqrt(n+1))+1):
    if n%i ==0:
        ans.append(i)
if len(ans) == 0:
    print(n-1)
else:
    tmp = max(ans)
    print(tmp+(n//tmp)-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
qplusmod = 0
i = 1
ans = n+1
for i in range(1,int(n)):
#  print(n,i)
  if n % i == 0:
    q = n / i
    qplusmod = q + i
#    print(n,i,qplusmod)
    if qplusmod < ans:
      ans = int(qplusmod)
  if i > n/i:
#    print(i)
    break
print(int(ans - 2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

r=round(n**0.5)

while n%r>0:
    r=r-1
print(r+int(n/r)-2)

Submission status: Accepted

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

sqA = int(math.sqrt(a))

for i in range(10000000):
    d = a / (sqA - i)
    if d.is_integer() == True:
        break


e = d + sqA - i - 2

print(int(e))

Submission status: Accepted

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

n = int(input())
a = 0

for i in range(1,int(sqrt(n))//1+1):
    if n%i == 0:
        b = i+n//i-2
        if i == 1:
            a = b
        elif b < a:
            a = b

print(a)

Submission status: Accepted

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

n = int(input())
a = []

for i in range(1,int(sqrt(n))//1+1):
    if n%i == 0:
        a.append(i+n//i-2)

print(min(a))

Submission status: Accepted

Solution code:
N=int(input())
K=list()

for i in range(1,int(N**0.5)+1):
  if(int(N/i)==N/i):
    M=int(N/i)  #N<=M
    L=i
    K.append(M+L-2)
    
print(min(K))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# # 整数の入力
# a = int(input())
# # スペース区切りの整数の入力
# b, c = map(int, input().split())
# # 文字列の入力
# s = input()
n = int(input())

ans = n

for i in range(1, 2000000):
    if n / i == n // i:
        ans = min(ans, i - 1 + n / i - 1)

print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
a = int(N**0.5)
ans = 10 ** 12
for x in range(1,a+1):
    b = N / x
    if b == int(b):
        if x + b - 2 < ans:
            ans = x+b-2
print(int(ans))

Submission status: Accepted

Solution code:
N =int(input())

n=int(N**(1/2)//1)
ans = N -1
for i in range(1,n+1):
    if N% i ==0:
        ans =min(i +N//i -2, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(n):
        if n < 4:
            return [1,n]
        res = [1]
        i = 2
        while i*i <= n:
            if n%i == 0:
                res.append(i)
                m = n//i
                if m != i:
                    res.append(m)
            i += 1
        res.append(n)
        return res

    n = I()
    l = f(n)
    ans = float("inf")
    for x in l:
        m = x+n//x-2
        if m < ans:
            ans = m
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N=int(input())
def factorization(n):
    arr = []
    for i in range(1, int(-(-n**0.5//1))+1):
        if n%i==0:
            arr.append(n//i+i-2)
#            arr.append([i,n//i])
    return arr

print(min(factorization(N) ))

Submission status: Accepted

Solution code:
N=int(input())

ans=1e15
i=1
while i*i<=N:
    if (N%i==0):
       ans=min(ans,(i-1)+(N/i-1)) 
    i+=1
print(int(ans))

Submission status: Accepted

Solution code:
input = int(input())
list = []
input_sqrt = int(input**(0.5))
for i in range(1, input_sqrt+1) :
    if input % i == 0 :
        list.append(i)

min = [max(list),input/max(list)]

print(int(min[0]+min[1]-2))

Submission status: Accepted

Solution code:
from math import sqrt
def main():
    n = int(input().rstrip("\n"))
    
    s = int(sqrt(n))
    for number in range(s, 0, -1):
        if n % number == 0:
            print(number + (n // number) - 2)
            break
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
ans=10**12
for  x in range(1,int((N)**0.5+1)):
    if N%x==0:
        if (x+int((N/x)-2))<ans:
            ans=(x+int((N/x))-2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

a = 1
while a <= N**0.5:
    if N % a == 0:
        a_max = a
    a += 1
print(int(a_max + N / a_max - 2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
yu = int(input())
result = []
if yu == 2:
	print(1)
	quit()
for i in range(1,round(math.sqrt(yu)+1)):
	if yu % i == 0:
		result.append(i)


print(round(max(result)-1+(yu/max(result))-1))

Submission status: Accepted

Solution code:


N = int(input())

chk = 0

for i in range(1, int(N ** 0.5) + 1):
    if N % i == 0:
        cp = N / i
        temp = i + cp

        if chk != 0:
            if temp < chk:
                chk = temp
        else:
            chk = temp

print(int(chk - 2))

Submission status: Accepted

Solution code:
a = int(input())

for i in range(int(a**0.5),0,-1):
    if a % i == 0:
        print(int(a/i + i -2))
        break

Submission status: Accepted

Solution code:
N = int(input())
D = 10**12
for i in range(int(N**(1/2))):
    if N % (i+1) == 0:
        if (i+1) + N//(i+1) -2 < D:
            D = (i+1) + N//(i+1) -2
print(D)

Submission status: Accepted

Solution code:
import sys
N = int(sys.stdin.readline())

maximum_x = int(N**(1/2))
for i in range(maximum_x, 0, -1):
  if N%i == 0:
    print(N//i + i - 2)
    break

Submission status: Accepted

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

    result_list = [int(x + n/x -2) for x in divisors]
    
    return min(result_list)

input_data = int(input())

print(minimumMove(input_data))

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline

def main():
    N = int(input())
    yaku = []
    for i in range(int(math.sqrt(N))+1, 0, -1):
        if N%i == 0:
            t = N//i
            print(i+t-2)
            return



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())

cand = []
for i in range(1, int(math.sqrt(n))+1):
  if n % i == 0:
    cand.append((i - 1) + (n // i - 1))
print(min(cand))

Submission status: Accepted

Solution code:
import math
N=int(input())
c=N+1
n=math.sqrt(N)
n=n//1
n=int(n)
for i in range(1,n+1):
    if N%i==0:
        a=i
        b=N//i
        if c>a+b:
            c=a+b
print(c-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
k=list()
for i in range(int(n**0.5)):
  if n%(i+1)==0:
    k.append(int(i+n/(i+1)-1))
print(min(k))

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:
    j =  n // i
    print(i+j-2)
    break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt

N = int(input())

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

Submission status: Accepted

Solution code:
from math import sqrt

N = int(input())

for i in range(int(sqrt(N) + 1), -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        break

Submission status: Accepted

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
show_flg=True


n=I()
for i in range(int(n**0.5),0,-1):
    if n%i==0:
        a,b=i,n//i
        break


print(a+b-2)

Submission status: Accepted

Solution code:
n = int(input())
ans = n - 1

for i in range(1, int(n ** 0.5) + 1):
  if n % i == 0:
    ans = (i - 1) + (n // i - 1)

print(ans)

Submission status: Accepted

Solution code:
num = int(input())
ans = 10**12
for a in range(1,int(num**0.5)+1):
    if num % a == 0 and ans > (num // a) + a:
        ans = (num // a) + a 
print(ans-2)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(map(int, input().split()))

import numpy as np
import math

n = ii()

sq = math.ceil(math.sqrt(n))

ans = n+1
for i in range(1, sq+1):
    if n%i==0 and i+n//i < ans:
        ans = i+n//i


print(ans-2)

Submission status: Accepted

Solution code:
N=int(input())
#N=list(map(int,input().split()))

min=10**12
for i in range(int(N**0.5)):
 if (N % (i+1))==0 and min>((N//(i+1))+i+1):
    min=(N//(i+1))+i+1
  
print(min-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+n//i)

    return divisors
  
print(min(make_divisors(N))-2)

Submission status: Accepted

Solution code:
from math import sqrt 
a = int(input())
ans = a*a
for i in range(1,int(sqrt(a))+1):
  if a % i == 0:
    tmp = a/i + i - 2
    if tmp < ans:
      ans = tmp
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())

import math
    
A = int(math.sqrt(N))
while N % A > 0:
    A -= 1
    continue

print(A + N // A - 2)

Submission status: Accepted

Solution code:
import sys
import math
n = int(input())
for i in range(int(math.sqrt(n)), 0, -1):
    if n%i==0:
        print(int((i-1)+(n/i-1)))
        sys.exit()

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())
divs = make_divisors(N)
ans = float('inf')
for i in divs:
    j = N//i
    ans = min(ans, i+j-2)
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

while k > 0:
    if N % k == 0:
        print(N // k + k -2)
        break
    k -= 1

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())

    k = math.floor(math.sqrt(n))

    while k > 0:
        if n % k == 0:
            print(n//k + k - 2)
            return
        k -= 1

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# N以下の約数
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())
cand = make_divisors(N)

ans = []
for c in cand:
    ans.append((N//c - 1) + c - 1)

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())

for i in range(int(n**0.5)+1, 0, -1):
  if n%i ==0:
    f = i
    break
print(f-1 + int(n/f-1))

Submission status: Accepted

Solution code:
import math

N = int(input())

ans = 1e12
for a in range(1, int(math.sqrt(N)) + 2):
    if N % a == 0:
        b = N // a
        ans = min(ans,a+b-2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

for i in range(1, int(n**.5)+1):
  if n%i == 0:
    p = i

q = n//p

print(p+q-2)

Submission status: Accepted

Solution code:
from math import sqrt, ceil

N = int(input())

minSteps = N - 1
for i in range(1, int(ceil(sqrt(N))) + 1):
    if N % i == 0:
        y = N // i
        minSteps = min(minSteps, i - 1 + y - 1)

print(minSteps)

Submission status: Accepted

Solution code:
import math
n=int(input())
mi=9999999999999
a=math.ceil(math.sqrt(n))
for i in range(1,a+1):
    x=n/i
    if x.is_integer():
        a=x+i-2
    if a<mi:
        mi=a
print(int(a))

Submission status: Accepted

Solution code:
inf = float('inf')
N = int(input())

from math import floor
# 約数全列挙(ソート済み)
# n = 24 -> [1,2,3,4,6,8,12,24]
def divisor(n):
    left = [1]
    right = [n]
    sup = floor(pow(n,1/2))
    for p in range(2,sup+1):
        if n % p == 0:
            left.append(p)
            right.append(n//p)
    return left, right

ans = inf
left, right = divisor(N)
for i, j in zip(left, right):
    d = abs(i-1) + abs(j-1)
    ans = min(ans, d)

print(ans)

Submission status: Accepted

Solution code:
import math
min = 1000000000000

n = int(input())
for i in range(1, math.ceil(math.sqrt(n))+1):
    x = n / i
    if x.is_integer():
        ans = (x-1) + (i-1)
        if ans < min:
            min = ans

print(int(ans))

Submission status: Accepted

Solution code:
import math
n = int(input())
root = math.ceil(math.sqrt(n))
answer = []
for x in range(1, root+1):
        if n%x == 0:
                answer.append(x + n//x - 2)
print(min(answer))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

m = math.floor(math.sqrt(n))

for i in range(m):
    p,q = divmod(n,m-i)
    if q == 0:
        print((m-i) + p - 2)
        break

Submission status: Accepted

Solution code:
def is_square(n):
    return n % (n**0.5) == 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)
    divisors.sort()
    return divisors

n = int(input())
if is_square(n): print(int(n**0.5*2-2))
else:
    res = make_divisors(n)
    print(res[len(res)//2-1]+res[len(res)//2]-2)

Submission status: Accepted

Solution code:
N = int(input())
i = 1
res = N
while i ** 2 <= N:
  if N % i == 0:
    q = N // i
    res = min(res, q - 1 + i - 1)
  
  i += 1
print(res)

Submission status: Accepted

Solution code:
# C
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())
divisors = make_divisors(N)
memo = 0
maX = divisors[0] + divisors[len(divisors) - 1]
for (i, r) in zip(divisors, reversed(divisors)):
    maX = min(i + r, maX)
    memo = i
    if memo == r:
        break
print(maX - 2)

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())

ans = n-1
i = 1
while i*i <= n:
    if n % i == 0:
        #print(n,i, ans)
        ans = min(ans, i + n//i - 2)
    i+=1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N = int(stdin.readline().rstrip())

move_i = 0
move_j = 0

result = []

for i in range(1, int(math.sqrt(N))+1):
    if N % i == 0:
        cnt = i - 1 + (N//i - 1)
        result.append(cnt)

ans = min(result) 

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
from math import sqrt

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

def ceil(x):
    return int(-(-x//1))

for i in range(ceil(N**(1/2)), 0, -1):
    if N%i == 0:
        print(i+N//i-2)
        exit()

Submission status: Accepted

Solution code:
a = int(input())

i = 1
while i <= a // i:
    i += 1

while a % i != 0:
    i -= 1

print(i + a//i - 2)

Submission status: Accepted

Solution code:
N = int(input())
n = 1
last = 0
while n**2 <= N:
  if N%n == 0 :
    last = n
  n += 1
print(last + N//last - 2)

Submission status: Accepted

Solution code:
import math
s = input().split() #標準入力　sに値を入れる
n = int(s[0])
root = int(math.sqrt(n))
ans = 10**18
for i in range(1, root + 1):
    if n % i != 0:
        continue
    j = n / i
    ans = min(ans, i + j - 2)
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys
read = sys.stdin.buffer.read

N = int(read())

a = [i for i in range(int(math.sqrt(N)), 0, -1) if N % i == 0][0]
b = N // a
print(a + b - 2)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
N_sqrt = int(np.sqrt(N))+1
N_ = [i for i in range(2, N_sqrt) if N % i == 0]

if len(N_) == 0:
    print(N-1)
else:
    pair = [(i, int(N/i)) for i in N_]
    my_lambda = lambda x : sum(x)
    x = list(map(my_lambda, pair))
    print(min(x)-2)

Submission status: Accepted

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