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(int(n ** 0.5), 0, -1):
    if n % i == 0:
      divisors.append(i)
#      if i != n // i:
#        divisors.append(n // i)
      divisors.append(n // i)
      break
  return divisors

N = int(input())
l = make_divisors(N)
a = l[0]
b = l[1]
print(a + b - 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.append(n // i)
  return divisors

N = int(input())
l = make_divisors(N)
a = l[len(l) - 1]
b = l[len(l) - 2]
print(a + b - 2)

Submission status: Accepted

Solution code:
n=int(input())
r=n**(1/2)
w=0
for i in range(1,int(r)+1):
  if n%i==0:
    w=i
x,y=w,n//w
if x!=1 and y!=1:
  print(x+y-2)
else:
  print(max(x,y)-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

# 素因数分解（辞書型で返ってくるので今回は使いにくい）
"""
pf={}
n = int(input())
for i in range(2,int(n**0.5)+1):
    while n % i == 0:
        pf[i] = pf.get(i,0)+1
        n//= i
if n > 1 : pf[n] = 1
print(pf)
"""
"""
# Return a list of the prime factors for a natural number bigger than 1
def trial_division(n):
    if n < 2:
        return []

    prime_factors = []

    for i in range(2, int(n**0.5)+1):
        # nがi（の累乗）で割り切れるかを調べる
        while n % i == 0:
            # nがiで割り切れる場合、iを素因数としてリストに追加する
            prime_factors.append(i)
            # nをiで割った商で更新しておく
            n //= i
    # nの平方根より大きい素因数が存在する場合
    if n > 1:
        prime_factors.append(n)

    return prime_factors

n = int(input())
# print(trial_division(n)) # 結果がリストで返ってくる
"""

""" TLE
n=int(input())

for i in range(1,10000):
    for j in range(1,i+1):
        seki=i*j
        if seki==n:
            print(i+j-2)
            exit()
"""

# 対称性よりNの√を取り、1~√nまで全探索
import sys
input = sys.stdin.readline
 
N=int(input())
ans=10**12
for i in range(1,int(N**0.5)+1):
    if N%i==0: # 割り切れる場合
        ans=min(ans,i+N//i) # N//iはもう一個の解 N=i*N//i
print(ans-2) # -2はもともと(1,1)にいるからその分の差し引き

Submission status: Accepted

Solution code:
import itertools
n = int(input())
def divisor(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
d = divisor(n)
l = float('inf')
for i in range(len(d)):
    l = min(n // d[i] + d[i] - 2,l)
print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
divs = []
for i in range(1, int(n ** 0.5) + 1):
  if n % i == 0:
    divs.append(i)
    if i != n // i:
      divs.append(n // i)
sdivs = sorted(divs)
lsdivs = len(sdivs)
if lsdivs % 2 == 0:
  a = sdivs[lsdivs // 2 - 1]
  b = sdivs[lsdivs // 2]
  print(a + b - 2)
else:
  a = sdivs[lsdivs // 2]
  print(a * 2 - 2)

Submission status: Accepted

Solution code:
import math


n = int(input())
a = int(math.sqrt(n))
while a > 0:
  if n%a == 0:
    s = a
    t = n//a    
    break
  a -= 1
print(s+t-2)

Submission status: Accepted

Solution code:
def divisors(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)
    d.sort()
    return d

n = int(input())
li = divisors(n)
if len(li) % 2 == 0:
    a,b = li[len(li)//2-1],li[len(li)//2]
    print(a-1+b-1)
else:
    a = li[len(li)//2]
    print(2*a-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n=int(input())

x=int(math.sqrt(n))

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

Submission status: Accepted

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

main()

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

def main():
    N = int(input())
    N_divisors = make_divisors(N)
    mi = N_divisors[0] + N // N_divisors[0]
    for i in range(len(N_divisors)):
        c_mi = N_divisors[i] + N // N_divisors[i]
        if (c_mi <= mi):
            mi = c_mi
    print(mi-2)
    return 0
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
i1 = round(N**0.5)
while True:
  if N%i1==0: break
  i1 -= 1

print(i1+round(N/i1)-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans-1 + n//ans-1)

Submission status: Accepted

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

Submission status: Accepted

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

num = n + 1

for i in range(1,int(math.sqrt(n) + 1)):
  if(n/i == n//i):
    num = min(num, i - 1 + n//i - 1)

print(num)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
sqrtn = sqrt(n)
for i in reversed(range(floor(sqrtn)+1)):
    if n % i == 0:
        a = int(n/i)
        print(a+i-2)
        exit()

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
import math
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n=int(readline())

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

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
left = 1
right = n
ans_l = 1
while left <= right:
    left += 1
    right = n/left
    if n/left %1==0:
        right = int(n/left)
        ans_l = left
ans_r = n // ans_l
print(ans_l+ans_r-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import numpy as np
import sys
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(1000)

ten = 10 ** 6

def solve(N):
    # prime factor
    ans = 2000000000000 + 10
    for i in range(1,ten+1):
        if N % i == 0:
            a = i
            b = N // i
            ans = min( ans , a + b - 2)
        
        if i > N :
            break
    
    print(ans)

if __name__ == "__main__":
    N = int(input())
    solve(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

for i in range(int(N**0.5), 0, -1):
    if (N / i).is_integer():
        a = N // i
        b = N // a
        break

print(a+b-2)

Submission status: Accepted

Solution code:
# C - Walk on Multiplication Table

N = int(input())
start = [1, 1]
end = [0, 0]
for i in range(1, int(N ** (1 / 2)) + 1):
    if N % i == 0:
        end[0] = i
        end[1] = N // i

print((end[0] - start[0]) + (end[1] - start[1]))

Submission status: Accepted

Solution code:
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

n = int(input())
t = divisor(n)
ans = float('inf')
for num in t:
  ans = min(ans, num + n//num - 2)
print(ans)

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])
    # divisors.sort()
    return divisors

div_lst = make_divisors(N)

min_num = float("inf")
for d in div_lst:
    tmp = d[0]+d[1]
    if tmp < min_num:
        min_num = tmp

print(min_num-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=int(N**.5)
while N%a!=0:
    a-=1
print(N//a+a-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    m = math.sqrt(n)
    ans = []
    for i in range(1, int(m) + 1):
        div = n / i
        if not float.is_integer(div):
            continue
        ans.append((div+i)-2)

    print(int(min(ans)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(min(ans)-2)

Submission status: Accepted

Solution code:
N = int(input())
ans = []
for i in range(1,int(pow(N,0.5))+1):
    j = N//i
    if i*j == N:
        ans.append(i+j)

print(min(ans)-2)

Submission status: Accepted

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

Submission status: Accepted

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

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:
            A=N//i+i-2
            if ans>A:
                ans=A
print(ans)

Submission status: Accepted

Solution code:
n = int(input())


from math import sqrt

def divisor(num):
    max_num = int(sqrt(num))
    divisor_list = []
    for i in range(1, max_num + 1):
        if num % i == 0:
            divisor_list.append(i)
            divisor_list.append(num//i)
    sorted_divisor_list = sorted(divisor_list)
    return sorted_divisor_list

def main():
    l = divisor(n)
    n_i = l[len(l)//2 - 1]
    n_j = l[len(l)//2]
    dist = n_i - 1 + n_j - 1
    print(dist)

main()

Submission status: Accepted

Solution code:
N=int(input())
move=10**12
for n in range(1,int(N**.5)+1):
  if N%n==0:
    move=min(move,N//n+n-2)
print(move)

Submission status: Accepted

Solution code:
import math
n = int(input())
m = n
t = math.sqrt(n)
s = math.floor(t)
for i in range(1,s+1)[::-1]:
    if n % i == 0:
        n //= i
        break
a = m // n
print(a + n - 2)

Submission status: Accepted

Solution code:
n = int(input())

d = 1
ans = n - 1
while d*d <= n:
    if n % d == 0:
        e = n // d
        ans = min(ans, d - 1 + e - 1)
    d += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
from math import sqrt
root = int(sqrt(n))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    m = 1
    min_value = N**2
    while m**2 <= N:
        if N % m == 0:
            n = N // m
            value = (n - 1) + (m - 1)
            min_value = min([min_value, value])
        m += 1
    print(min_value)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
sN = int(math.sqrt(N))
Answer = N
for i in range(2,sN+1):
    if N % i == 0:
        A1 = (N//i) + i
        Answer = min(Answer,A1)
    else:
        continue
if Answer ==N:
    print(Answer-1)
else:
    print(Answer-2)

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc144/tasks/abc144_c

import math

N = int(input())

mi = -1

for i in range(1, int(math.sqrt(N)) + 1):
    a = i
    if N % a != 0:
        continue

    b = N // a
    count = a + b - 2
    
    if mi <= -1 or mi > count:
        mi = count

print(mi)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

print(N - 1)

Submission status: Accepted

Solution code:
N=int(input())

from itertools import combinations
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

d=make_divisors(N)

if math.sqrt(N) in d:
    d.append(int(math.sqrt(N)))

dist=float('inf')
for i in d:
    j = N//i
    if i * j ==N:
        dist = min(dist,i+j-2)

print(dist)

Submission status: Accepted

Solution code:
N=int(input())

from itertools import combinations
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

d=make_divisors(N)

if math.sqrt(N)**2 ==N:
    d.append(int(math.sqrt(N)))

dist=float('inf')
for i in d:
    j = N//i
    if i * j ==N:
        dist = min(dist,i+j-2)

print(dist)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
import math as m

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

for i in range(q,0,-1):
    if n%i == 0:
        k = i
        l = int(n/i)
        break

print(int(k+l-2))

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')
#nの公約数を求める。そのルートに近しいものが答え
for i in range(1,math.floor(n**0.5)+1):
    if n%i==0:
        tmp = i+n//i-2
        if tmp<ans:
            ans=tmp
print(ans)

Submission status: Accepted

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

mod = 10 ** 9 + 7
N = int(input())


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


ans = N-1
for i in range(1, int(np.sqrt(N) + 1)):
    if (N % i == 0):
        j = N//i
    ans = min(ans, i+j-2)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

    j = 1
    ans = N
    for i in range(1, int(N**0.5) + 1):
        if N % i == 0:
            j = int(N/i)
        tmp = i - 1 + j - 1
        if tmp < ans:
            ans = tmp            
    print(ans)


main()

Submission status: Accepted

Solution code:
N = int(input())

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

ans = float('inf')
for a,b in ans_list:
    ans = min(ans, a+b-2)

print(ans)

Submission status: Accepted

Solution code:
import math

ii = lambda:int(input())
li = lambda:list(map(int,input().split()))
lix = lambda x:list(li() for _ in range(x))
##########
def main(n):
    res = n+1
    ran = math.floor(math.sqrt(n))
    for x in range(1,ran+1):
        y,mod = divmod(n,x)
        if mod == 0:
            res = min(res, calc(x,y))
    return res

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

n = ii()
print(main(n))

Submission status: Accepted

Solution code:
N = int(input())

def make_div(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//make_div(N)[-1] + make_div(N)[-1] - 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt
N = int(input())
Nsq = int(sqrt(N))
div = 1
for i in range(Nsq,1,-1):
    if N % i == 0:
        div = i
        break

print(div + N // div - 2)

Submission status: Accepted

Solution code:
import math
import itertools
n = int(input())
x = 1
for i in reversed(range(1, math.ceil(math.sqrt(n))+1)):
    if n % i == 0:
        x = n / i
        break

print(math.floor(x - 1 + n / x - 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
A = int(math.sqrt(N))
B = 10**12
ans = B
for i in range(1,A+1):
    if N % i == 0:
        C = i + (N//i) - 2
        if C < ans:
            ans = C
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=0

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
MIN = 10**12 + 1

for i in range(1,int(np.sqrt(N)) + 100):
    if N % i == 0:
        a = N //i
        MIN = min(MIN, a +i)
print(MIN-2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
ans = float('inf')
# √nから１ずつ減らしていく
for i in range(int(n**0.5), 0, -1):
    # a*b=nになるところで、移動最初内なら更新
    if n%i == 0:
        ans = min(ans, (i - 1)+ (n // i - 1))  # a-1 + b-1が移動回数
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)
    divisors.sort()
    return divisors

n = int(input())
ls = make_divisors(n)
d = 10 ** 13 + 1
for i in ls:
    d = min(d, (i - 1) + (n // i - 1))

print(d)

Submission status: Accepted

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

Submission status: Accepted

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

n = int(math.sqrt(N))
M = 0
for i in range(1,n+1):
  if N%i == 0:
    M = max(M,i)

ans = N//M + M -2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
def is_prime(num):
    import math
    if num == 2:
        return True
    elif num <  3 or num % 2 == 0:
        return False
    for i in range(3, int(math.sqrt(num)) + 1, 2):
        if num % i == 0:
            return False
    return True

if is_prime(n):
    print(n-1)
    exit()

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

Submission status: Accepted

Solution code:
from math import sqrt

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

Submission status: Accepted

Solution code:
N = int(input())

def divisors(n, sort=False):
    """
    nの約数をO(√n)で列挙する
    """
    d = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            d.append((i, n//i))  #タプル括弧の中に数字が二つはいる
    if sort:
        d.sort()
    return d
l = []
for d1, d2 in divisors(N):
    m = d1 + d2
    l.append(m)
print(min(l) - 2)

Submission status: Accepted

Solution code:
import math

if __name__ == '__main__':
    n = int(input())
    ans_div = 0
    limit = math.ceil(math.sqrt(n))
    for i in range(1, limit + 1):
        if n % i == 0:
            ans_div = max(ans_div, i)
    print(ans_div + n // ans_div - 2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

x = int(input())

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

for i in range(1,n+1):
    if x%i == 0:
        temp = i

ans = temp + x/temp -2
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

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

for i in range(maxn):
    j = maxn - i
    if n % j == 0:
        a = j
        b = int(n/j)
        break

print(a + b - 2)

Submission status: Accepted

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

upper = int(np.sqrt(n))+1

ans = 1000000000000000000
for i in range(1,upper+1):
    if n % i == 0:
        buf = i + n//i-2
        if buf < ans:
            ans = buf

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = 10000000019
N = 50
N = 10
N = int(input())

result = []
for i in range(1, math.ceil(math.sqrt(N))+1):
    if N % i == 0:
        result.append((i + (N // i)) - 2)

print(min(result))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

num = int(n**0.5)+1
x = 0
x_min = 10**13
for i in range(1,num):
    if n%i==0:
        x = n//i+i-2
        if x_min > x:
            x_min = x
print(x_min)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())

n = int(N**0.5) + 1

for a in range(1, n):
    if N % a == 0:
        b = N // a
        ans = (a - 1) + (b - 1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

n = int(N**0.5) + 1
A = 0
B = 0
b = 0
for a in range(1, n):
    if N % a == 0:
        b = N // a
        A = a
        B = b
ans = (A - 1) + (B - 1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
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:
N = int(input())
ans = 10**12
for i in range(1,10**6+1):
  if N % i == 0 and (N//i + i -2) < ans:
    ans = (N//i + i -2)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N = I()

ans = INF
for n in range(1, int(math.sqrt(N))+1):
    if N % n == 0:
        ans = min(ans, int((n-1)+(N/n-1)))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
def main():
    N = int(input())
    mi = 10**13
    for i in range(1, math.floor(math.sqrt(N))+1):
        if N % i == 0:
            m = i - 1 + (N//i) - 1
            if mi > m:
                mi = m
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
from math import sqrt

N = int(input())
step = deque()

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

print(min(step))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
#import itertools
N = int(input())
n = int(N**(0.5))
for i in range(n):
  c = n-i
  if N % c == 0:
    a = N/c
    break;
print(int(a+c-2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

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

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