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 main():
    n = int(input())
    x = 10 ** 13
    i = 1
    while i * i <= n:
        if n % i == 0:
            j = n // i
            x = min(x, (i - 1) + (j - 1))
        i += 1
    print(x)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
leng=10**100

mx=int(n**0.5)+2

for i in range(1,mx):
    j=n/i
    if j.is_integer():
        tmp=i+j-2
        if tmp<leng:
            leng=tmp

print(int(leng))

Submission status: Accepted

Solution code:
import math

if __name__ == "__main__":
	N = int(input())
	ans = 1000000000000
	for i in range(int(math.sqrt(N)) + 1, 0, -1):
		if N % i == 0 and i + (N // i) - 2 < ans:
			ans = i + (N // i) - 2

	print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
root = math.sqrt(N)
pair = []

for i in range(math.floor(root)):
    I = i + 1
    if N % I == 0:
        pair.append([I, N/I])
        
wa = []
        
for i in range(len(pair)):
    wa.append((pair[i][0] - 1) + (pair[i][1] - 1))
else:
    print(int(min(wa)))

Submission status: Accepted

Solution code:
n=int(input())
l=set()
for i in range(1,int(n**(1/2))+1):
  if n%i==0:
    l.add(n//i+i)
print(min(list(l))-2)

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:
        ans = min(ans, i + N//i)

print(ans-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd
from collections import defaultdict

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)

res = N+1

for div in divs:
    res = min(N+1, div+N//div)

print(res-2)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- Coding: utf-8 -*-

import sys


input = sys.stdin.readline


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

    ary = []
    n = 1
    while n**2 <= N:
        if N % n == 0:
            ary.append(n)
        n += 1

    ans = 99999999999999999999999
    for i in ary:
        ans = min(ans, i+N//i-2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
ii0 = lambda: ii() - 1
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')

def gen_divs(n):
    for i in range(1, int(n**0.5)+1):
        if n%i == 0:
            yield i
            if i != n // i:
                yield n // i
    return


def main():
    N = ii()
    divs = [i for i in gen_divs(N)]
    divs.sort()
    if len(divs) % 2 == 1:
        print((divs[len(divs)//2]-1)*2)
    else:
        p = len(divs) // 2
        print(divs[p]-1 + divs[p-1]-1)




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

    # divisors.sort()
    return divisors

res = float('inf')
for x in make_divisors(N):
    res = min(res, x + N//x - 2)
print(res)

Submission status: Accepted

Solution code:
import math
n = int(input())
n1 = int(math.sqrt(n))
cnt = 10**18
for i in range(1, n1+1):
  if n % i == 0:
      j = (n/i) - 1
      ans = i-1
      cnt = min(cnt, j+ans)
      #print(cnt)
  else:
    continue
print(int(cnt))

Submission status: Accepted

Solution code:
N=int(input())
import copy
n=copy.deepcopy(N)
D={}
for i in range(2,10**6+1):
    while N%i==0:
        if i not in D:
            D[i]=1
            N//=i
        else:
            D[i]+=1
            N//=i
if N!=1:
    D[N]=1
D=list(D.items())
#print(D)
ANS=[1]
for i in range(len(D)):
    ans=[]
    for j in ANS:
        for k in range(D[i][1]+1):
            ans.append(j*(D[i][0]**k))
    #print(ans)
    ANS=ans
#print(ANS)

cnt=10**12
for i in ANS:
    if (i+(n//i))-2<cnt:
        cnt=(i+(n//i))-2
print(cnt)

Submission status: Accepted

Solution code:
N = float(input())
A = int(N**(0.5))
max = 10**12
for i in range(1,A+1):
    B = N/float(i)
    if B.is_integer():
        c = i+B-2
        if c<= max:
            max = c
print(int(max))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
#import numpy as np
#import operator
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
	n=int(input())
	nn=int(n**0.5)
	ans=[]
	
	for i in range(nn+1,0,-1):
		if n%i==0:
			ans.append(i)
			ans.append(n//i)
			break
	print(sum(ans)-2)
	
	




if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

ans = N-1
i = 2
while i*i <= N:
  if N % i == 0:
    ans = i + N // i - 2
  i += 1

print(ans)

Submission status: Accepted

Solution code:
import math
import sys

N = int(input())

m = int(math.sqrt(N))

for i in range(m+1, 0, -1):
    if N % i == 0:
        print(i-1 + N//i-1)
        sys.exit()

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = n-1

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

print(ans)

Submission status: Accepted

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

l = N // m
print(m+l-2)

Submission status: Accepted

Solution code:
N = int(input())
# res = |X - i| + |Y - j|
yakusu = []
for i in range(1, int(N ** (1 / 2))+1):
     if N % i == 0:
         yakusu.append(i)

res = 10**13
for y in yakusu:
    x = N//y
    dist = (y-1)+(x-1)
    res = min(res, dist)

print(res)

Submission status: Accepted

Solution code:
import math
import sys

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

for i in range(num):
    if n % (num-i) == 0:
        print((num-i) + (n//(num-i)) - 2)
        sys.exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

sN = int(N ** 0.5)

result = N - 1
for n in range(1, sN+1):
    if N % n == 0:
        result = min(result, n-1 + (N//n) -1)
print(result)

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())

div = make_divisors(n)
if len(div)%2 == 0:
    print(div[len(div)//2 - 1]+div[(len(div)//2)]-2)
else:
    print(div[(len(div)//2)]*2-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

    # divisors.sort()
    return divisors


k=make_divisors(n)
if len(k)==2:
    print(sum(k)-2)
else:
    print(k.pop()*2-2)

Submission status: Accepted

Solution code:
N = int(input())
s = N
i = 1
while i**2 <= N:
    if N % i == 0:
        s = i
    i += 1

ans = N//s + s - 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
root = int(N ** (1/2)) + 1
num = 1
for i in range(root, 0, -1):
    if N % i == 0:
        num = i
        break
num2 = N // num
print(num+num2-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

a = make_divisors(n)
a.sort()

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

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N = int(input())

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

Submission status: Accepted

Solution code:
import math

N = int(input())

ans = N - 1
for i in range(2, int(math.sqrt(N)) + 1):
    q = N // i
    if i * q == N:
        ans = min(ans, i + q - 2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

#約数
def divisor(n):
    a = []
    root_n = int(n**0.5)
    for i in range(root_n):
        if n % (i+1) == 0:
            a.append(i+1)
            if (i+1) != n // (i+1):
                a.append(n//(i+1))
    a.sort()
    return a

yaku = divisor(n)
ima = n + 2
for i in yaku:
    othe = n // i
    ima = min(ima,i + othe)
print(ima - 2)

Submission status: Accepted

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

f=True
for i in range(int(math.sqrt(N))):
    n=int(math.sqrt(N))-i
    if N%n==0:
        print(int(n+N/n-2))
        f=False
        break
if f:
    print(N-1)

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

import collections
import numpy as np

# = input()
# = int(input())
# = map(int, input().split())
# = list(map(int, input().split()))
#

N = int(input())

yakusu = make_divisors(N)
yakusu.sort()
ans = np.inf
for y in yakusu:
  if y + N // y < ans:
    ans = (y + N // y) - 2

print(ans)

Submission status: Accepted

Solution code:
def c_walk_on_multiplication_table():
    N = int(input())

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

print(c_walk_on_multiplication_table())

Submission status: Accepted

Solution code:
N = int(input())

def get_divisors(n, reverse=False):
    from math import sqrt
    divisors = []
    for num in range(1, int(sqrt(n))+1):
        if n % num == 0:
            divisors.append(num)
            divisors.append(n//num)
    return sorted(divisors, reverse=reverse)

divisors = get_divisors(N)

l = len(divisors)
ans = float('inf')
for i in range(l//2):
    ans = min(ans, divisors[i] + divisors[l - 1 - i])
print(ans-2)

Submission status: Accepted

Solution code:
from math import sqrt
from sys import exit

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())
ans_list = []
for i in range(1,10**6+1):
    if n % i == 0:
        ans_list.append(n//i+i)

print(min(ans_list)-2)

Submission status: Accepted

Solution code:
n = int(input())


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


ans = 10 ** 12 + 9
x, y = modif(n)
for i, j in zip(x, y):
    ans = min(ans, i + j - 2)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=999999999999
for i in range(1,1000001):
    if n/i==n//i:
        if ans>i-1+n//i-1:
            ans=i-1+n//i-1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l = make_divisors(N)
ll = [(v1 - 1) + (N / v1 - 1) for v1 in l]
print(int(min(ll)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())
index = 2
result = 10**13
while index * index <= n:
    if n % index == 0:
        curr = (n // index) - 1
        curr += (index - 1)
        result = min(curr, result)
    index += 1
if result == 10**13:
    result = n - 1
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())
INF = 10**15
m = int(N**.5)+1

ans = INF
for i in range(1,m):
    if N%i == 0:
        t = i + N//i
        if  t < ans:
            ans = t
print(ans-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,n // i])
    # divisors.sort()
    return divisors

n = int(input())
a = make_divisors(n)
print(a[-1][0]+a[-1][1]-2)

Submission status: Accepted

Solution code:
def make_divisors(n):
    for i in range(int(n**0.5)+1,0,-1):
        if n % i == 0:
            return i
            break


m = int(input())


k = make_divisors(m)
j = m / k

print(int((k-1)+(j-1)))

Submission status: Accepted

Solution code:
import sys


def main():
    n = int(sys.stdin.readline().strip())
    # n = 1000000000000

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

    divider = dividers[-1]
    ans = (n // divider - 1) + (divider - 1)
    print(ans)


main()

Submission status: Accepted

Solution code:
import math
N = int(input())
n1 = N
n2 = 1

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

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

N = INT()
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
l = make_divisors(N)
r = sorted(l)
l = sorted(l, reverse = True)
# print(r)
# print(l)

s = []
for i in range(len(r)):
  s.append(r[i]+l[i])
# print(s)
print(min(s)-2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
from math import factorial, ceil, floor
from bisect import bisect_right as bsr
from operator import itemgetter as ig
from collections import defaultdict as dd
from collections import deque, Counter as cnt

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]
def nlist(n, v):
    if not n: return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]

# エントリーポイント
def main():
    N = input(int)
    ans = N
    for n in range(int(N ** 0.5), 0, -1):
        if N % n == 0:
            temp = n + N // n
            if temp < ans:
                ans = temp
    if ans == N:
        print(ans - 1)
    else:
        print(ans - 2)
    return

if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())
#a,b,x = [int(i) for i in readline().split()]


ans = n+100
for i in range(1,1000001):
    if n%i == 0:
        c = i + n//i -2
        if c < ans: ans = c
    

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
nn = math.ceil(math.sqrt(N))
ans = 10e+13
for i in range(1, nn+1):
    if N % i == 0:
        tmp = N // i
        ans = min(ans, i + tmp -2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = int(N**(1/2)) + 1
if a*a > N:
    a -= 1
while(a>0):
    if N%a==0:
        break
    a -= 1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

import math
import collections


def divisors_kai(n):
    ret = 1
    for i in range(1, int(n ** 0.5) + 1):
        d, m = divmod(n, i)
        if m == 0:
            ret = i
            # ret.add(d)
    return ret


def solve(N: int):
    a = divisors_kai(N)
    b = N/a
    print(int(a + b - 2))
    return


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:
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())
ans_li = []
divisors = make_divisors(n)
for item in divisors:
  a = item + n//item
  ans_li.append(a)
print(min(ans_li)-2)

Submission status: Accepted

Solution code:
import numpy as np
a, = [list(map(int, s.split())) for s in open(0)][0]
min_ = a - 1
for i in range(2,int(np.sqrt(a)+1)):
    if a % i == 0:
        tmp = i + a//i - 2
        if tmp < min_:
            min_ = tmp
print(min_)

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())
dv1 = make_divisors(N)
dv2 = sorted(dv1, reverse=True)
dv = min([dv1[i]+dv2[i] for i in range(len(dv1))])
print(dv-2)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input().strip())

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    imax = int(N**0.5)
    for i in range(imax, 0, -1):
        if N % i == 0:
            j = N//i
            print(i + j - 2)
            return
    print('No')
    return

main()

Submission status: Accepted

Solution code:
import math

N = int(input())

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

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

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())
nums = make_divisors(N)
if len(nums) %2 == 1:
  print((nums[-1]-1)*2)
  exit()
else:
  print(nums[-1]-1+nums[-2]-1)

Submission status: Accepted

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
# ルートNから少しづつ増やして割り切れたらおk
import numpy as np
start = int(np.sqrt(N))
while N % start != 0:
    start -= 1
a, b = start, N//start
print(a+b-2)


# def prime_factorize(n):
#     a = []
#     while n % 2 == 0:
#         a.append(2)
#         n //= 2
#     f = 3
#     while f * f <= n:
#         if n % f == 0:
#             a.append(f)
#             n //= f
#         else:
#             f += 2
#     if n != 1:
#         a.append(n)
#     return a


# print(prime_factorize(N))

Submission status: Accepted

Solution code:
import math
n=int(input())
ans=10**12
sqrt_n=math.ceil(math.sqrt(n))
for i in range(1,sqrt_n+1):
    if(n%i==0 and ans > i+(n//i)-2):
        ans = i + (n//i) - 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())

    n = int(N ** 0.5)
    for i in range(n, 0, -1):
        d, m = divmod(N, i)
        if not m:
            print(d + i - 2)
            break


main()

Submission status: Accepted

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

print(int(r))

Submission status: Accepted

Solution code:
import math as m


def calc(N):
    for i in range(m.floor(m.sqrt(N)), 0, -1):
        if N % i == 0:
            return (i - 1) + (N // i - 1)


N = int(input())
print(calc(N))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

num = int(np.sqrt(n) // 1)

ans = 0
for i in range(1, num+1):
    if n % i == 0:
        ans = i

print(int(n/ans + ans - 2))

Submission status: Accepted

Solution code:
import math

n = int(input())

k = int(math.sqrt(n))
for i in range(k):
    a = k-i
    if n % a == 0:
        print(a -2 +n//a)
        break
    else:
        pass

Submission status: Accepted

Solution code:
#import sys

#input = sys.stdin.readline
import math
#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True


def main():
#    h,w,a,b = map(int, input().split())
    n=int(input())

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






if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#A=int(input())
#A=list(map(int,(input().split())))
A=list(map(int,(input().split())))
N=A[0]
def dou(ans):
	for x in range(int(N**0.5+1),0,-1):
		if ans%x==0:
			return x
ans=dou(N)
print(N//ans+ans-2)

Submission status: Accepted

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

    # divisors.sort()
    return divisors

N =  int(input())
divide = make_divisors(N)
divide.sort()
LEN = len(divide)
LENG = LEN//2

ans = divide[LENG]
an = N//ans
print(ans-1+an-1)

Submission status: Accepted

Solution code:
from bisect import bisect_left
import math
def divisors(n, sort=True, reverse=False):
    sqn = math.sqrt(n)
    sqn_int = int(sqn)
    div_list = []
    if sqn == sqn_int:
        div_list.append(sqn_int)
        loop = range(1,sqn_int)
    else:
        loop = range(1,sqn_int + 1)
    for i in loop:
        if n % i == 0:
            div_list.append(i)
            div_list.append(n//i)
    if sort:
        div_list.sort(reverse=reverse)
    return div_list

N = int(input())
div_list = divisors(N)
i = bisect_left(div_list, int(math.sqrt(N)))
a = div_list[i]
b = N//a
print(a+b-2)

Submission status: Accepted

Solution code:
def f(N):
  output=[]
  temp=int(pow(N, 1/2))+1
  for i in range(1, temp):
    if N%i==0:
      j=N//i
      output.append([i, j])
      
  return output
    

N=int(input())
nums=f(N)

temp=len(nums)
cand=[]
for temp in nums:
  cand.append(temp[0]+temp[1]-2)
  
print(min(cand))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import sqrt

n = int(input())
max_div = int(sqrt(n))

for d in range(max_div, 0, -1):
  if n % d == 0:
    print(d + n // d - 2)
    break

Submission status: Accepted

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

Submission status: Accepted

Solution code:


N=int(input())

i=1
minStep=0
while i*i<=N:
    if N%i==0:
        minStep=i+(N//i)
    i+=1
print(minStep-2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

N = int(input())

import math
a = int(math.sqrt(N))
while (a > 1):
    if (N % a) == 0:
        break
    a -= 1

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

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):
        x = int(i + N/i - 2)
        ans = min(ans, x)
print(ans)

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:
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split()) 
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
mn = 1234567890123
for i in range(1,10**6+1):
  if n%i > 0:
    continue
  if i + n//i < mn:
    mn = i + n//i
print(mn-2)

Submission status: Accepted

Solution code:
N=int(input())

#return divisor list
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


table=divisor(N)
tmp=1+N
for i in table:
    tmp=min(tmp,i+N//i)

print(tmp-2)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    ans = n-1
    for i in range(1,int(n**0.5//1+1)):
        if n%i==0:
            ans = min(ans,n//i+i-2)
    print(ans)
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:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


ans_list = make_divisors(N)

_len = len(ans_list)

# print(ans_list)
# print(_len)

if _len % 2 == 0:
    print(ans_list[-1]+ans_list[-2]-2)
else:
    print(ans_list[-1]*2-2)

Submission status: Accepted

Solution code:
def yaku(n):
    l=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            l+=[(i,n//i)]
    return l
        

def resolve():
    n=int(input())
    l=yaku(n)
    ans=[i[0]+i[1]-2 for i in l]
    print(min(ans))
resolve()