Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

10

Sample Output 1

5

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

Sample Input 2

50

Sample Output 2

13

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

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.



Sample input:
10


Sample output:
5

Submission status: Accepted

Solution code:

import math
n = int(input())
ans = 10**12+1
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:
import math
a = int(input())
sum = []

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


print(int(min(sum)-2))

Submission status: Accepted

Solution code:
import math

N = int(input())
ans = float("inf")


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

print(ans)

Submission status: Accepted

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

# 素数がどうか判定
flag = 0
for i in range(2, int(math.sqrt(N))+1, 1):
  if N % i == 0:
    flag = 1
    break
if flag == 0:
  print(N-1)
else:
  res = 0
  idx = int(math.sqrt(N))
  for i in range(idx, 0, -1):
    if N % i == 0:
      res = i + int(N/i) - 2
      print(res)
      break

Submission status: Accepted

Solution code:
import math
N=int(input())
sqn=math.floor(math.sqrt(N))

for fac in reversed(range(sqn+1)):
    if N%fac==0:
        print(int(fac+N/fac-2))
        break

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = []
i = 2
a = int()
b = int()
ans = 10**12+1
for i in range(2,int(np.sqrt(N))+1):
    if N%i ==0:
        A.append(i)

if len(A) == 0:
    print(N-1)
else:
    for j in range(len(A)):
        a = A[j]-1
        b = (N/A[j])-1
        if a+b <= ans:
            ans = a+b
    print(int(ans))

Submission status: Accepted

Solution code:
import math

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

# 例題3だと割り切れないからこのまま
minimum=n-1
for i in range(1,n2+1):
    # 割り切れるなら以下の処理
    # 例題だとi=2の時割り切れるので
    if(n%i==0):
        # 例題だと
        # 10/3-1+3-1
        # 50/6-1+6-1
        minimum=min(minimum,(n//i-1)+(i-1))

print(minimum)

Submission status: Accepted

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

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, n//i + i -2)

print(ans)

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

Submission status: Accepted

Solution code:
N = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

num_li = make_divisors(N)
n = len(num_li)

if n == 2: #素数の時
    print(num_li[1]-1)
elif n%2 == 1:
    print((num_li[(n-1)//2]-1)*2)
else:
    print((num_li[n//2]-1)+(num_li[n//2-1]-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

def make_divisor_list(num):
    i = 1
    table = []
    while i * i <= num:
        if num%i == 0:
            table.append(i)
            table.append(num//i)
        i += 1
    table = list(set(table))
    return table

N = int(input())

divisors = sorted(make_divisor_list(N))
center = int(len(divisors) / 2)

if len(divisors) % 2 == 1:
    ans = (divisors[center] - 1) * 2
else:
    ans = divisors[center] - 1 + divisors[center-1] - 1

print(ans)

Submission status: Accepted

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

ans_list = []
ans = 0

for i in range(N_s):
  if(N%(i+1) == 0):
    ans = N/(i+1) + (i+1) - 2
    ans_list.append(int(ans))
    
ans_list = sorted(ans_list)
print(ans_list[0])

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Walk on Multiplication Table
https://atcoder.jp/contests/abc144/tasks/abc144_c

"""
import sys


def divisor(n):
    divisors = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            divisors.add(n // i)
    return divisors


def solve(N):
    ans = float('inf')
    for d in divisor(N):
        q = N // d
        ans = min(ans, d-1 + q-1)
    return ans


def main(args):
    N = int(input())
    ans = solve(N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

start=int(math.sqrt(N))+1

end=0
while start>0:
    if N%start==0:
        end=N//start
        break
    else:
        start-=1

print(end-1+start-1)

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    root = []
    for i in range(1, int(math.sqrt(n))+1):
        if n % i == 0:
            root.append(i+(n//i)-2)
    print(min(root))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

N = int(input())
l = make_divisors(N)


ans = float("inf")
if len(l)%2:
    ans = l[len(l)//2]*2-2
else:
    for i in range(len(l)//2):
        ans = min(ans, l[i]-1+N//l[i]-1)
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

A = math.floor(N**0.5)
B = math.ceil(N**0.5)

while True:
    if N % A == 0:
        B = N // A
        break
    elif N % B == 0:
        A = N // B
        break
    else:
        A -= 1
        B += 1

print(A + B - 2)

Submission status: Accepted

Solution code:

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

print(res)

Submission status: Accepted

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


def main():
    n = int(input())
    divs = sorted(divisor(n))
    if len(divs) % 2 == 0:
        left = len(divs)//2 - 1
        right = len(divs)//2
    else:
        left, right = len(divs)//2, len(divs)//2

    ans = divs[left] + divs[right] - 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.buffer.readline().rstrip()
rs = lambda: sys.stdin.buffer.readline().split()
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.buffer.readline().rstrip()
rs = lambda: sys.stdin.buffer.readline().split()
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

m = float('inf')
n = ri()
for i in range(1, int(n**0.5) + 1):
  a = n//i
  if a*i == n:
    if m > a+i:
      m = a+i
else:
  print(m-2)

Submission status: Accepted

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

A=[]

for i in range(1,int(math.sqrt(N))+1):
  if N % i ==0:
    A.append(i)
    A.append(int(N/i))
    
A.sort()
base = int(len(A)/2)

print(A[base]-1 +A[base-1] -1)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
ans = n + 1 - 2
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())
ans = 100000000000000
for i in range(1, int(N**(1/2)+1)):
    if N % i == 0:
        ans = min(ans, i + N//i - 2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

out = n - 1
q, mod = divmod(n, 2)
if mod == 0 and q < out:
    out = q  # (q - 1) + 2 -  1
if n > 2:
    for m in range(3, int(n ** 0.5) + 1):
        q, mod = divmod(n, m)
        if mod == 0 and (q - 1) + (m - 1) < out:
            out = q - 1 + m - 1

print(out)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

sqrt_N = N ** 0.5
ans = 10**18

for i in range(1, int(sqrt_N)+1):
    if N % i == 0:
        move_num = (i-1) + (N//i - 1)
        ans = min(move_num, ans)

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = float('inf')

for i in range(1, math.ceil(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())

a = 0
b = 0

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

Submission status: Accepted

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(10**6)

n=int(input())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            upper_divisors.append(n//i)
        i += 1
    return lower_divisors,upper_divisors
a,b=make_divisors(N)
print(a[-1]+b[-1]-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc144/tasks/abc144_c C - Walk on Multiplication Table
N=int(input())
S_List=[i for i in range(1,int(N**0.5)+1,1) if N % i ==0]

Answer = 10**12
for i in range(len(S_List)):
    Current_Answer = int(S_List[i] + (N/S_List[i]) -2)
    if Current_Answer < Answer:
        Answer = Current_Answer

print(Answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
import math
x = math.ceil(math.sqrt(n))
for i in range(x,0,-1):
  if n % i == 0:
    xy = [i,n//i]
    break

print(sum(xy)-2)

Submission status: Accepted

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

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

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

print(ans - 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
b = int(N**0.5)
while True:
    if N%b != 0:
        b -= 1
    else:
        break

print(int(b+N/b-2))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

U = 10**6 + 100
x = np.arange(1,U, dtype=np.int64)
div = x[N%x==0]
ans = (div + N//div).min() - 2
print(ans)

Submission status: Accepted

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

ans = list()
for i in range(int(math.sqrt(N))):
    i += 1
    if N % i ==0:
        j = N//i
        ans.append(i + j -2) 
    else:
        pass

print(min(ans))

Submission status: Accepted

Solution code:

N = int(input())
b = N**0.5//1.0
while True:
    if N%b != 0:
        b -= 1
    else:
        break
print(int(b+N/b-2))

Submission status: Accepted

Solution code:
n = int(input())
sqrtN = n**(1/2)
if sqrtN.is_integer():
    Pos = [int(sqrtN), int(sqrtN)]
#    print(Pos)
else:
    for i in range(int(sqrtN), 0, -1):
#        print(i)
        if n%i == 0:
            Pos = [i, n//i]
#            print(Pos)
            break
print(sum(Pos) - 2)

Submission status: Accepted

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

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

Submission status: Accepted

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

a[1] = int(n/a[0])

print(ans)

Submission status: Accepted

Solution code:
def get_nearest_div_pair(n):
    for i in range(int(n ** 0.5), 0, -1):
        if n % i != 0:
            continue
        return i + n // i - 2


print(get_nearest_div_pair(int(input())))

Submission status: Accepted

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


pairs = enum_div_pairs(int(input()))
print(min(i + j - 2 for i, j in pairs))

Submission status: Accepted

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

    st = [N + 1]  # (1,N)
    d = 2
    while d * d <= N:
        if N % d == 0:
            st.append(d + N // d)  # (d, N // d)
        d += 1

    print(min(st) - 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
d = 1 
m = +float('inf')
while(d * d <= n):
	if n % d == 0:
		m = min(m, n/d -1 + d -1)
	d+=1
print m

Submission status: Accepted

Solution code:
n = int(input())
ans = n-1
d = 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:
import math

n=int(input())
ans=1+n-2
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:
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())
lst = make_divisors(N)
lst.sort()
for i in range(N):
    if lst[i] * lst[i+1] >= N:
        break
if lst[i] ** 2 == N:
    print(lst[i] * 2 - 2)
else:
    print(lst[i+1]-1+lst[i]-1)

Submission status: Accepted

Solution code:
import sys
import math
N=int(input())
c=math.sqrt(N)
c=int(c)
L=range(1,c+1)
R=sorted(L,reverse=True)
for i in R:
  if N//i==N/i:
    k=N//i
    print(k+i-2)
    sys.exit()

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


l = make_divisors(n)
x = int(l[len(l) // 2] + n / l[len(l) // 2] - 2)

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
ls = [[i, N//i] for i in range(1, int(N**0.5)+1) if N % i ==0]

ans = float("inf")
for i in range(len(ls)):
    x, y = ls[i]
    ans = min(ans, x + y - 2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

n = int(input())
l = sorted(divisor(n))

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

Submission status: Accepted

Solution code:
import sys
import math
input = lambda: sys.stdin.readline().rstrip()

INF = 100100100100100


def solve():
    N = int(input())
    n_m = int(math.sqrt(N) + 1)

    r = INF
    for i in range(1, n_m + 1):
        if N % i == 0:
            r = min(r, i + N // i)

    print(r - 2)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())

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

    divisors.sort()
    return divisors

divisors = make_divisors(N)
l = len(divisors)

a = divisors[(l-1)//2]
b = N // a

print(a+b-2)

Submission status: Accepted

Solution code:
N = int(input())
destination = (1, N)

i = int(N ** 0.5)
while 1 < i:
    if N % i == 0:
        destination = (i, N//i)
        break
    i -= 1

print(destination[0] + destination[1] - 2)

Submission status: Accepted

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

    return divisors

def walk_on_multiplication_table(n):

    ans = 10**13
    yaku = make_divisors(n)
    yaku.sort()
    count = len(yaku)
    for i in range(int(count//2)):
        ans = min(ans, (yaku[i]+yaku[count-i-1]-2))

    if count % 2 == 0:
        return ans
    return min(ans, (yaku[int(count//2)]*2)-2)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

    # divisors.sort()
    return divisors


def main():
    N = int(readline())
    target = make_divisors(N)[-2:]
    print(target[0] + target[1] - 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
N = int(input())
min_num = N+1

for i in range(1,int(math.sqrt(N))+1):

    if N % i == 0:
        if (i + (N // i)) < min_num:
            min_num = (i + (N // i))
print(min_num - 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(reverse=True)
    return divisors


N = int(input())
D = make_divisors(N)
ans = 10 ** 13
for d in D:
    ans = min(ans, d + N // d - 2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())

    i = int(n ** 0.5)

    while i >= 1:
        if n % i == 0:
            j = n // i
            print(i + n // i - 2)
            exit()
        i -= 1


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())

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


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())

    min = 10 ** 15

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

    print(min)


if __name__ == "__main__":
    setrecursionlimit(10000)
    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[-1]


N = int(input())
a = make_divisors(N)
b = N // a

print(a + b - 2)

Submission status: Accepted

Solution code:
N = int(input())

def getDivisors(x):
    anss = set()
    for d in range(1, int(x**0.5)+1):
        if x%d == 0:
            anss.add(d)
            anss.add(x//d)
    return sorted(anss)

divs = getDivisors(N)

ans = N
for div in divs:
    k = N//div
    ans = min(ans, div+k-2)

print(ans)

Submission status: Accepted

Solution code:
s=int(input())
n=int(s**.5)+1
ans=float("inf")
for i in range(1,n):
  a=s//i
  if s%i==0:
    ans=min(a+i-2,ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = 1
for i in range(1,int(n**0.5) + 1):
    if n%i == 0:
        c = max(c,i)

print(n//c +c -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())
L = len(make_divisors(n))
i,j = make_divisors(n)[L//2-1], make_divisors(n)[L//2]

if (n**0.5).is_integer():
    print(int(2*(n**0.5-1)))
else:
    print(i+j-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
minc=float('INF')
ans = [1,1]
for i in range(1,10**6+1):
    if n%i==0:
        x,y = i,n//i
        sumn = x+y-2
        if(minc>sumn):
            minc=sumn
            
print(minc)

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


N = inp()

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

Submission status: Accepted

Solution code:

import math
n = int(input())

ans=10**13
for i in range(1, int(math.sqrt(n))+1):
    if n%i==0:
        tmp=i+n/i-2
        if tmp < ans:
            ans=tmp
    else:
        continue

print(int(ans))

Submission status: Accepted

Solution code:
import math
n=int(input())
m=int(math.sqrt(n))+1
mini=n-1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

ans = 1+n-2

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

print(ans)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes) if b else print(no)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
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=n0()
l=make_divisors(n)
l.sort()
m=len(l)
if m%2==1:
    print(l[m//2]*2-2)
else:
    print(l[m//2-1]+l[m//2]-2)

Submission status: Accepted

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

for k in range(1,int((n)**0.5)+2):
    if n%k == 0 :
        m = n//k + k - 2
        if ans > m:
            ans = m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
def sosu(x):
    a=1
    for i in range(2,int(math.sqrt(x))+1):
        if x%i==0:
            a=i#素数でない場合は最後のaが出力される
    return a
a=sosu(n)
if a==1:
    print(n-1)
else:
    b=n//a
    ans=(a-1)+(b-1)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

t = int(n**0.5+1)
ans = 10**13
for i in range(1, t):
  if n%i == 0 and i+n//i-2 < ans:
    ans = i+n//i-2
print(ans)

Submission status: Accepted

Solution code:
def solution(N):
    p, q = N, 1
    min_sum = p + q

    for i in range(2, int(N ** 0.5) + 1):
        m, r = divmod(N, i)

        if r == 0 and m + i < min_sum:
            min_sum = m + i
            p, q = m, i

    return p + q - 2

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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