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:
from math import sqrt
n = int(input())
ans = []
for i in range(1, int(sqrt(n)) + 1):
    if n % i == 0:
        ans.append(i + n // i - 2)
print(min(ans))

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter
import itertools
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

N=II()

L=list()

for n in range(1, int(N**0.5)+1):
    if N%n==0:
        L.append((n+int(N/n))-2)

print(min(L))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math
import numpy as np
 
n = int(input())
ans = np.inf
for i in range(1, int(math.sqrt(n))+1):
    if n%i==0:
        q = n/i 
        tmp = i+q 
        if tmp <= ans:
            ans = tmp 
print(int(ans-2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math
import numpy as np

n = int(input())
ans = np.inf
for i in range(1, int(math.sqrt(n))+1):
    if n%i==0:
        q = n//i
        tmp = i+q 
        if tmp <= ans:
            ans = tmp 
            i_ = i 
            q_ = q 

print(ans-2)

Submission status: Accepted

Solution code:
n=int(input())

r=[]

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

print(min(r))

Submission status: Accepted

Solution code:
n = int(input())
div = []
for i in range(1, int(n**0.5)+1):
    if n % i == 0:
        div.append(i)
        if i != n // i:
            div.append(n//i)
i = 0
div.sort()
while True:
    if div[i]*div[i+1] == n:
        print(div[i]+div[i+1]-2)
        break
    elif div[i]*div[i] == n:
        print(div[i]+div[i]-2)
        break     
    else:
        i += 1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import  math

N = int(input())

root = int(math.sqrt(N))
sum = 10 ** 13

for i in range(1, root + 2):
    if N % i == 0 and int(N / i) + i - 2 < sum:
        sum = int(N / i) + i - 2

print(sum)

Submission status: Accepted

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()

ans = 10 ** 18
for i in range(1, int(sqrt(N))+1):
    if N % i != 0:
        continue
    j = N // i

    ans = min(ans, i+j-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+n//i)
    return divisors

n = int(input())
lis = make_divisors(n)
print(min(lis)-2)

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])
ans = []
for a, b in li:
    ans.append(a + b - 2)
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
val = []
for a in range(int(N**0.5)+1):
  b, mod = divmod(N, a+1)
  if mod == 0:
    val.append( (a+1) + b - 2 )
print(min(val))

Submission status: Accepted

Solution code:
import math
import itertools

n=int(input())


res = 10**13


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

        res=min(res,a + b -2)


print(int(res))

Submission status: Accepted

Solution code:
import os

def main():
	n = int(input())
	list_a = make_divisors(n)
	min_num = 1000000000000
	for i in range(0,len(list_a),2):
		if n//list_a[i] == list_a[i]:
			min_num = list_a[i] * 2 - 2
			break
		if min_num < (list_a[i] + list_a[i+1] - 2):
			pass
		else:
			min_num = list_a[i] + list_a[i+1] - 2
	print(min_num)
	
			


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

	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import math
n = int(input())
n_list = []
for i in range(1, int(math.sqrt(n))+1):
    if n % i == 0:
        n_list.append((i, n//i))
sa = n-1
for x, y in n_list:
    if sa >= abs(x-1)+abs(y-1):
        sa = abs(x-1) + abs(y-1)
print(sa)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))

n = int(input())
for i in range(ceil(sqrt(n)), 0, -1):
    f = n / i
    j = floor(f)
    if f - j > 0: continue
    print(i + j - 2)
    sys.exit()

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = []
for i in range(1, int(math.sqrt(n)+1)):
  if i * (n // i) == n:
    ans.append(i + (n//i) -2 )

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

cnt = 0
for i in range(1, int(n ** 0.5)+1):
    if n % i == 0:
        cnt = max(cnt, i)

print(cnt + n//cnt - 2)

Submission status: Accepted

Solution code:
import math
n = int(input())
d = {}
for i in range(1,(int(math.sqrt(n)))+1):
  if (n/i).is_integer():
    d.setdefault(i,n//i)
ans = []
for k,v in d.items():
  ans.append((k-1)+(v-1))
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())

ans = 10**12 * 2
for i in range(1, int(N**0.5) + 1):
    if N % i == 0:
        distance = i + N//i - 2
        if ans > distance:
            ans = distance

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
n=int(N**(1/2))

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

Submission status: Accepted

Solution code:
import math

n = int(input())
ans_min = 10 ** 13
for i in range(1, int(math.sqrt(n)) + 1):
    a, b = divmod(n, i)
    if b == 0:
        ans = a + i - 2
        if ans_min > ans > 0:
            ans_min = ans
print(ans_min)

Submission status: Accepted

Solution code:
n = int(input())

i_max = int(n**(0.5)//1)

result = 10**12

for i in range(1, i_max+1):
    if n%i == 0:
        result = min(result, i+n//i-2)
print(result)

Submission status: Accepted

Solution code:
import math

N = int(input())
ans = []
k = int(math.modf(math.sqrt(N))[1])

for a in range(1, k+1):
    if N % a == 0:
        b = int(N / a)
        ans.append(a + b - 2)

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**18+1
for i in range(1, int(N**0.5)+1):
    if N % i != 0:
        continue
    j = N // i
    ans = min(ans, i+j-2)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

def divisions(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
divisors=divisions(n)
if len(divisors)%2==1:
  print((divisors[-1]-1)*2)
else:
  print(divisors[-1]+divisors[-2]-2)

Submission status: Accepted

Solution code:
import math

n = int(input())

mins = n
for i in range(1, int(math.sqrt(n))+1):
  if n % i == 0:
    mins = min(mins, i + (n // i) - 2)
print(mins)

Submission status: Accepted

Solution code:
N = int(input())

ans = []

if N**(1/2) < 2:
    print(N-1)
    exit()

for i in range(1, int(N**(1/2)+10)):
    if N%i == 0:
        ans.append(int(N/i)+i)


print(min(ans)-2)

Submission status: Accepted

Solution code:
N = int(input())
ans = 1 + N - 2 #求めるのは(x-1) + (y-1)の最小値
for i in range(1, int(N**0.5)+1):
    if N%i==0:
        ans = min(ans, i+N//i-2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

Solution code:
from math import floor, sqrt

n = int(input())

ans = n - 1
for i in range(2, floor(sqrt(n)) + 1):
    j, mod = divmod(n, i)
    if mod == 0:
        temp = i + j - 2
        if temp < ans:
            ans = temp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
a=int(input())
ans=a
ct=0
while ct<=(math.sqrt(a)-1):
    ct+=1
    if a%ct==0:
        if ans>=((a/ct)+ct):
            ans=((a/ct)+ct)
if a==ans:
    ans+=1
if a==4:
    ans=4
print(int(ans-2))

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import math

N = int(input())
yakusu = 1
for i in range(1, math.floor(math.sqrt(N)) + 1):
    if N % i == 0:
        yakusu = i
print(yakusu + N // yakusu - 2)

Submission status: Accepted

Solution code:
N = int(input())

ans = 10**12
for i in range(1, int(N**.5)+1):
    if N%i==0:
        reg = N//i + i - 2
        ans = min(reg, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
min_ = n -1 
for i in range(1, int(n**0.5)+10):
    if n % i != 0:
        continue
    else:
        min_ = min(min_, i-1+(n//i)-1)
    
print(min_)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def I(): return int(input())
##################################################
N = I()
yakusuu = []
ans = 0
for i in range(1,int(N**0.5)+1):
    if N%i==0:
        yakusuu.append(i)
if len(yakusuu)==0:
    ans = N-1
else:
    ans += max(yakusuu)
    ans += N//max(yakusuu)
    ans -= 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())
for i in range(10**6+1):
    if i ** 2 >= n:
        break

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

print(j + n // j - 2)

Submission status: Accepted

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


for a in range(int(math.sqrt(N)) +1, 0, -1):
    if N % a == 0:
        print((a-1)+(N//a-1))
        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

def myAnswer(N:int) -> int:
   divisors = make_divisors(N)
   length = len(divisors)
   if(length % 2 == 1):
      return divisors[length//2]*2 - 2
   else:
      return divisors[length//2] + divisors[length//2 - 1] - 2



def modelAnswer():
   tmp=1
def main():
   N = int(input())
   print(myAnswer(N))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
import math
n=int(input())
min=n-1
x=1
y=n
for i in range(1,int(math.sqrt(n)+1)):
  if n%i==0:
    if abs(i-n//i)<min:
      x=i
      y=n//i
      min=abs(i-n//i)
print(x+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)
    return divisors
l=make_divisors(n)
if l[-1]**2==n:print((l[-1]-1)*2)
else:print(l[-2]-1+l[-1]-1)

Submission status: Accepted

Solution code:
N = int(input())
s =N+1
n = int(N**0.5) 
i = 1
while i<=n:
  if N % i != 0:
    i += 1
  elif N % i == 0 and N//i +i < s:
    s = N//i + i
    i += 1
  else:
    i += 1
print(s-2)

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

Solution code:
# solution
import io
import math
from math import *

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

Submission status: Accepted

Solution code:
N = int(input())
def divisor_all(n): # 約数全列挙
    l = [1,n]
    for i in range(2,int(pow(n,1/2))+1):
        if n % i == 0:
            if i == n//i:
                l.append(i)
            else:
                l.append(i)
                l.append(n//i)
    l.sort()
    return l # list
L = divisor_all(N)
ans = 10**12
for i in range(len(L)):
    ans = min(ans,(L[i]+N//L[i]))
print(ans-2)

Submission status: Accepted

Solution code:
import math
n = int(input())
m = n**0.5
ans = 1
x = 0 #確認
#print(n)
#print(m)
for i in range(1,math.floor(m)+1):
    x += 1 #確認
    if n%i==0:
        ans = i

print(math.floor(ans+ n/ans -2))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = []
for i in range(1, int(sqrt(N))+1 ):
  if N%i == 0:
    A.append(i + N//i -2)

print(min(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

n = int(sys.stdin.readline().rstrip())

def main():
    res = divisors(n)
    print(res[-1] + res[-2] - 2)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))



n=input1()

for i in range(int(math.sqrt(n)) +1,-1,-1):
	if n%i==0: #iがnの約数
		print((i-1) + (n//i -1))
		break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n=int(input())

a=int(math.sqrt(n))+1

mindis=n

for i in range(1,a+1):
  if n%i==0:
    b=int(n/i)
    if mindis>(i+b-2):
      mindis=(i+b-2)
print(mindis)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    import math
    s = math.sqrt(n)
    i = 1
    mi_ = float('inf')
    while i <= s:
        if n % i == 0:
            mi_ = min(mi_,i + n//i - 2)
        i+=1
    print(mi_)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math

N = int(input())

s = int(math.sqrt(N))

while s >= 1:
    if N % s == 0:
        break;
    s -= 1

print((s-1) + (N//s-1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
t=int(math.sqrt(n))
while n%t!=0:
  t-=1
a=t
b=n//a

print(str(a+b-2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
import math

def L1_norm(x, y):
    return (x+y-2)

def solve(N: int):
    d = 10**12
    for i in range(1,int(math.sqrt(N))+1):
        if N%i==0:
            d = min(d, L1_norm(i, N//i))

    print(d)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

N = int(input())

minimum = None
for i in range(1, int(math.sqrt(N) + 1)):
    is_dividable = N % i == 0
    if is_dividable:
        j = int(N / i)
        foo = i + j - 2
        minimum = foo if minimum is None else min(minimum, foo)

print(minimum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def aaa(n):
    ans = 0
    for i in range(1, int(n ** (1/2)) + 1):
        if n % i == 0:
            ans = [i, n // i]
    return ans
            
n = int(input())
a, b = aaa(n)
print((a - 1) + (b - 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n =int(input())
a =10**12

for i in range(1,n):
    if n % i ==0:
        b =n//i
        a =min(a,b+i-2)
    if i*i >n:
        break
print(a)

Submission status: Accepted

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

print(MAX+N//MAX-2)

Submission status: Accepted

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

N = int(input())

ans = 0
for i in range(1, int(math.sqrt(N)) + 1):
    if not N % i:
        if ans:
            ans = min(ans, i + (N // i) - 2)
        else:
            ans = i + (N // i) - 2
    else:
        continue

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
i=1
l=[]
m=[]
while i<=n**0.5:
    if n%i==0:
        l.append(i)
        m.append(n//i)
        i+=1
    else:
        i+=1
o=len(l)
a=[]
for j in range(o):
    a.append(l[j]+m[j])
print(min(a)-2)

Submission status: Accepted

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

min = n + 1

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

print(min)

Submission status: Accepted

Solution code:
n = int(input())

a=100000000000000

for i in range(n):
	if i==0:
		continue
	if i*i>n:
		break
	if n % i == 0:
		b = n//i
		a = min(a,b+i-2)

print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
#  = list(map(int, input().split()))
#x = list(map(int, input().split()))
i = 1
l = []
while i*i <= n:
    if n%i==0:
        l.append(i)
        l.append(n//i)
    i+=1

l.sort()
a = l[len(l)//2]
b = l[len(l)//2-1]
print(a+b-2)
#print(l)

Submission status: Accepted

Solution code:
n = int(input())
ans = 10**12

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

Submission status: Accepted

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

factor_a = 0
for i in range(1, int(np.sqrt(n)) + 1):
    if n % i == 0:
       factor_a = i
       
factor_b = n // factor_a

#print(factor_a)
#print(num, factor_b)
print(factor_a - 1 + factor_b - 1)

Submission status: Accepted

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

for i in range(1, math.ceil(math.sqrt(n))+10):
     if n%i==0:
         ans = min(ans, i+(n//i)-2)
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

#m = len(divisors)

#a = divisors[m//2-1]
#b = divisors[m//2]

a= max(divisors)
b=n//a
#print(a)
#print(b)
print(a+b-2)

Submission status: Accepted

Solution code:
import math
N = int(input())
m = float('inf')
for i in range(1, int(math.sqrt(N)) + 1):
    if N % i == 0:
        m = min((i - 1) + (N // i) - 1, m)
print(m)

Submission status: Accepted

Solution code:
N = int(input())

ans = 1e12 + 5
for i in range(1, N + 1):
    j = N / i
    if (i > j):
        break
    if (N % i != 0):
        continue
    ans = min(ans, i + int(j) - 2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())

ans = N-1

sqN = int(math.sqrt(N))

for i in range(1,sqN+1):
    if N % i != 0:
        continue
    else:
        a = i
        b = N // i
        ans = min(ans, a+b-2)

print(ans)

Submission status: Accepted

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

n = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
x=int(n**.5+2)
for i in range(x,1,-1):
    if n%i == 0:
        print(i+(n//i)-2)
        exit()
else: 
    print(n-1)
    exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
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

def main():
    n=int(input())
    n_list=make_divisors(n)
    len_list=len(n_list)
    if len_list%2==1:print(2*n_list[len_list//2]-2)
    else:
        print(n_list[len_list//2-1]+n_list[len_list//2]-2)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
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

def main():
    n=int(input())
    n_list=make_divisors(n)
    len_list=len(n_list)
    if len_list%2==1:print(2*n_list[len_list//2]-2)
    else:
        print(n_list[len_list//2-1]+n_list[len_list//2]-2)

if __name__=='__main__':
    main()

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:
            syou,amari =divmod(n,i)
            divisors.append((syou,i))
            #if i != n // i:
            #    divisors.append(n//i)

    # divisors.sort()
    return divisors

yakusu =make_divisors(N) #商と割る数
ans =[]
for i in yakusu:
    ans.append(sum(i)-2) #（1，1）からの距離、２＜Nだから絶対値も不要 
print(min(ans))

Submission status: Accepted

Solution code:
import math

N = int(input())
max_a = math.sqrt(N)

r = N
for a in range(1, int(max_a) + 2):
    if (N % a == 0):
        r = min(int(N / a) + a - 2, r)
print(r)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def make_diviser(n):
  D=[]
  for i in range(1,int(n**0.5)+2):
    if n%i==0:
      D.append(i)
      D.append(n//i)
  D=sorted(set(D))
  l=len(D)
  if l%2==0:
    return D[(l//2)-1]+D[l//2]
  else:
    return D[l//2]*2
n=int(input())  
print(make_diviser(n)-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = int(n ** 0.5)
i = 0
while True:
    if n%(s + i) == 0:
        k = 1
        break
    if n%(s - i) == 0:
        k = -1
        break
    i += 1
s = s + i * k 
print((s + n // s) - 2)

Submission status: Accepted

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

print(x + (N//x) - 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)
ans = float("inf")
for i in range(len(p)):
    if i < len(p) - 1:
        if p[i] * p[i+1] == n:
            ans = min(ans, p[i]-1+p[i+1]-1)
    if p[i] * p[i] == n:
        ans = min(ans, (p[i] - 1) * 2)
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())
d = int(math.sqrt(N))
d_max = 0
for i in range(1,d+1):
  if N % i == 0:
    d_max = i
    
d_another = N / d_max
print(int(d_max+d_another)-2)

Submission status: Accepted

Solution code:
import math
N=int(input())
min=10**12
for i in range(1, math.floor((N)**(1/2)+1)):
    if N%i==0:
        min=i+(N//i)-2
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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