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:
n = int(input())

import math
a = int(math.sqrt(n))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

i = 1
ele = []
while i * i <= n:
    if n % i == 0:
        ele.append([i,n//i])
    i += 1

ans = 10**13
for i in range(len(ele)):
    ans = min(ans,ele[i][0]+ele[i][1]-2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
from itertools import combinations_with_replacement

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


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

    # divisors.sort()
    return divisors


def solve():
    n = II()
    divisors = make_divisors(n)

    ans = 1001001001001001
    for a, b in combinations_with_replacement(divisors, 2):
        if a * b != n:
            continue
        step = (a-1) + (b-1)
        ans = min(step, ans)
    print(ans)



if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()

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

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
m=math.ceil(math.sqrt(n))
list=[]

for i in range(1,m+1):
  if n%i==0:
    s=n//i
    t=s+i
    list.append(t)
list.sort()
print(list[0]-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)
  if len(divisors) % 2 != 0:
    return divisors[-1]*2
  else:
    return divisors[-1] + divisors[-2]

n = int(input())

print(max(make_divisors(n)-2,0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

k = int(N**0.5) + 1
ans = N

for a in range(2, k):
    tmp = 0
    if N % a == 0:
        tmp = N//a + a
        if ans > tmp:
            ans = tmp

if ans < N:
    print(ans - 2)
else:
    print(N - 1)

Submission status: Accepted

Solution code:
import math

n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

ans = n + 1
for x in range(1, int(n ** 0.5) + 2):
    if n % x != 0:
        continue
    y = n // x
    ans = min(ans, x + y - 2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input()) # 入力が1つ
# map(int, input().split()) # 入力が複数

MAX = 1
i = 1
while i <= n ** 0.5:
    if n % i == 0:
        MAX = max(MAX, i)
    i += 1
ans = abs(n // MAX + MAX - 2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

max_i = 1
i = 1
while i*i <= n:
    if (n//i)*i == n:
        max_i = i
    i += 1

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

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
i=int(math.sqrt(n))
while i>0:
    if n%i==0:
        ans=i+n//i-2
        break
    i-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
# root n に近い約数だと移動が45°に近く，移動回数も少なそう。
ca = int(n ** (1 / 2))
while True:
    if n % ca == 0:
        ca_b = n // ca
        break
    ca -= 1

print(ca - 1 + ca_b - 1)

Submission status: Accepted

Solution code:
n = int(input())
stop = int(n**0.5) + 1
min_count = 10 ** 100
for a in range(1, stop):
    if n % a != 0:
        continue
    else:
        b = n // a
    count = a + b - 2
    if count < min_count:
        min_count = count
print(min_count)

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

for i in range(1,int(N**0.5)+1)[::-1]:
    if N % i == 0:
        x,y = N//i,i
        break

print((x-1) + (y-1))

Submission status: Accepted

Solution code:
import math
n = int(input())
m = int(math.sqrt(n))


min = n
for i in range(1,m+1):
  if n%i == 0:
    waru = i
    shou = n//i
    
    if (waru+shou-2) < min:
      min = waru+shou-2
print(min)

Submission status: Accepted

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

a = 1
c = N - 1
while a <= math.sqrt(N):
  if N % a == 0:
    c = min(a + N//a - 2,c)
  a +=1
  
print(c)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

ans = 1000000000000000
for i in range(1, int(np.sqrt(N)) + 2):
    if N % i != 0:
        continue
    num = i + N // i - 2
    if ans > num:
        ans = num
print(ans)

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)
B = max(A)
C = N // B
print(B+C-2)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(1,n):
    sur = n%i
    ans = n/i
   # print(i,ans,sur)
    if ans < i:
        break
    elif sur == 0:
        l.append(i)
l.sort(reverse=True)
max = l[0]
max_op = n/max
ite = max-1+max_op-1
print(int(ite))

Submission status: Accepted

Solution code:
import math
n = int(input())
mn = 10 ** 12
for a in range(1, math.ceil(math.sqrt(n)) + 1):
    b = n // a
    if a + b < mn and a * b == n : mn = a + b
print(mn - 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

times = []

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

print(min(times))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial, log, log10, log2
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
from datetime import datetime as dt

def main():
  n = int(readline())
  n2 = ceil(sqrt(n)) + 1
  ans = INF
  for i in range(1,n2):
    j = n//i
    if i*j==n:
      ans = min(ans, i+j-2)
  print(ans)
  
  
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math
N = int(input())
n = round(math.sqrt(N))
counter = 0
for i in range(n):
		if N%(n-i) == 0 and counter == 0:
			counter+=1
			var1=(n-i)
			var2=N/(n-i)
            
print(int(var1+var2-2))

Submission status: Accepted

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

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

print(int(ans))

Submission status: Accepted

Solution code:
import math

N = int(input())
n = 0
m = 0

for i in range(int(math.sqrt(N)), 0, -1):
    if N % i == 0:
        n = i
        m = int(N / n)
        break
    

print(n + m - 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())
divisors=make_divisors(n)
if divisors[-1]**2==n:
    print(divisors[-1]*2-2)
else:
    print(divisors[-1]+divisors[-2]-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    m = int(N ** .5)
    while N % m != 0:
        m -= 1
    else:
        print(int(N / m + m - 2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
ans = float('inf')
for i in range(1, int(n ** 0.5) + 1):
    x, y = divmod(n, i)
    if y == 0:
        ans = min(ans, x + i)
print(ans - 2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

ans = [10 ** 14, 10 ** 14]
for i in range(1, int(pow(n, 0.5)) + 1):
    if n % i == 0:
        tmp = n // i
        if tmp + i < sum(ans):
            ans = [i, tmp]
print(sum(ans) - 2)

Submission status: Accepted

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

n = int(input())
p = divisor(n)
ans = n + 10
for x in p:
  if x*x > n:
    break
  y = n//x
  if ans > y+x:
    ans = y+x
print(ans - 2)

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template
N = int(input())
ans = 10**13
for i in range(1,int(N**(1/2))+1):
    if N % i == 0:
        d = (i-1) + (N//i - 1)
        ans = min(ans,d)
print(ans)

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, acos, atan, asin, log, log10
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 STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(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()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    ans=n+2
    for d in range(1,n+1):
        if d**2>n:break
        if n%d==0:
            ans=min(ans,d+n//d)
    print(ans-2)

main()

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
def make_divisors(n: int) -> list:
    """自然数nの約数を列挙したリストを出力する
    計算量: O(sqrt(N))
    入出力例: 12 -> [1, 2, 3, 4, 6, 12]
    """
    divisors = []
    for k in range(1, int(n ** 0.5) + 1):
        if n % k == 0:
            divisors.append(k)
            if k != n // k:
                divisors.append(n // k)
    divisors = sorted(divisors)
    return divisors


n = int(input())
div = make_divisors(n)
ans = 10 ** 18
for i in div:
    ans = min(ans, i + n // i - 2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
divisors = []
for i in range(1, int(pow(n, 0.5)) + 1):
    if n % i == 0:
        divisors.append([i, n // i])
divisors = sorted(divisors, key= lambda x:x[1])
res = 0

for i in divisors[0]:
    res += i - 1

print(res)

Submission status: Accepted

Solution code:
def div(n):
    div_pair = []
    for i in range(1,  int(n ** 0.5 +1)):
        if n % i == 0:
            diff = abs(n // i - i)
            div_pair.append([diff, i, n // i])
    return div_pair


n = int(input())
l = sorted(div(n))
ans = l[0][2] + l[0][1] - 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)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math 

N=int(input())
A=math.sqrt(N)
tmp=float("inf")
for i in range(1,int(A)+1):
	if N%i==0:
		tmp=min(tmp,N//i+i)
print(tmp-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

answer=0

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

Submission status: Accepted

Solution code:
n = int(input())


div = 0

for p in range(1, int(n**0.5)+1):
    if n % p == 0:
        div = p

print(div+(n//div)-2)

Submission status: Accepted

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

n = int(input())

ans = 10 ** 18

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N = int(input())
    res = INF
    for i in range(1, int(math.sqrt(N))+1):
        if N%i==0:
            res = min(res, i + N//i - 2)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ans = 10**12
for i in range(1,n+1):
    if i*i > n:break
    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())
l=make_divisors(n)
ans=1<<40
for i in range(len(l)//2+1):
    ans=min(ans,l[i]-1+n//l[i]-1)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    Move = 10**20
    for i in range(1, N + 1):
        if i ** 2 > N: break
        if N % i == 0:
            Move = min((i - 1) + (N // i - 1), Move)
    print(Move)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    from math import sqrt
    ans = 10**12
    for i in range(1, int(sqrt(N))+2):
        if N % i:
            continue
        j = N//i
        ans = min(ans, i+j-2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    for i in range(int(n**0.5)+2)[::-1]:
        if not n%i:
            print(n//i+i-2)
            return
    return

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

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

Submission status: Accepted

Solution code:
N= int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
#C
import math
n = int(input())
sho_lst = []
for i in range(1,int(math.sqrt(n))+1):
    amari = n % i  
    if amari == 0:
        sho = n // i
        sho_lst.append(sho - 1 + i - 1)
        
print(min(sho_lst))

Submission status: Accepted

Solution code:
import math
n=int(input())
m=math.floor(math.sqrt(n))
i=1
bk=[0,0]
while i<=m:
  wk=n/i
  if math.floor(wk)==math.ceil(wk):
    bk[0]=wk
    bk[1]=i
  i+=1
next
print(math.floor(sum(bk))-2)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

f=2
cf =1
flag =0
while flag == 0:
	if n % f ==0:
		if f*f <= n:
			cf = f
	if f*f > n:
		flag =1
	else:
		f = f+1
#print(cf)
ans = (cf-1)+(n//cf-1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N=int(input())
x=[]
min=10000000000000000
ans=0
sqN=math.ceil(math.sqrt(N))
for i in range(1,sqN+1):
  if N%i==0:
    j=int(N/i)
    a=[i,j]
    x.append(a)
for i,j in x:
  if i+j<=min:
    ans=i+j-2
    min=i+j
print(ans)

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = float('inf')
    I = int(math.sqrt(N)) * 10
    for i in range(1, I):
        if N%i == 0:
            j = N//i
            ret = min(ret, j + i - 2)

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
import math
a=math.floor(math.sqrt(n))
while a>0:
  if n%a==0:break
  a-=1

print(a-2+n//a)

Submission status: Accepted

Solution code:
#ABC144.C
N = int(input())
cnt = N-1
for i in range(2,int(N**0.5)+2):
    if N%i == 0:
        S = N//i + i -2
        if S <= cnt:
            cnt =S
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
c=10**13
for i in range(1,int(n**0.5)+1):
    if n%i==0:
        j=int(n/i)
        if i+j < c:
            c=int(i+j)

print(c-2)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def make_divisors(n): # 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 = ir()
D = make_divisors(N)
answer = 10 ** 12
for d in D:
    e = N // d
    if e < d:
        break
    result = (d - 1) + (e - 1)
    if result < answer:
        answer = result

print(answer)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def make_divisors(n): # 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 = ir()
D = make_divisors(N)
answer = 10 ** 12
for d in D[:(len(D)+1)//2]:
    e = N // d
    result = (d - 1) + (e - 1)
    if result < answer:
        answer = result

print(answer)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def make_divisors(n): # 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 = ir()
D = make_divisors(N)
answer = 10 ** 12
for d in D:
    e = N // d
    result = (d - 1) + (e - 1)
    if result < answer:
        answer = result

print(answer)

Submission status: Accepted

Solution code:
n=int(input())
ans=10**15
for i in range(1,int(n**0.5)+10):
    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(2, int(N**0.5)+2):
    if N % i == 0:
        tmp = (i-1) + N//i-1
        if tmp < ans:
            ans = tmp

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

pf.sort()

t = len(pf)
print(pf[t//2 - 1] + pf[t//2] -2)

Submission status: Accepted

Solution code:
from sys import exit
from math import sqrt

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

Submission status: Accepted

Solution code:
import math
a =int(input())
list=[]
for i in range(1,int(math.sqrt(a))+1):
  if a%i == 0:
    list.append(i+a//i)

print(min(list)-2)

Submission status: Accepted

Solution code:
import math

N = int(input())

ans = 10**15
for k in range(1, int(math.sqrt(N))+1):
    if N % k > 0:
        continue
    ans = min(ans, k + N//k - 2)
print(ans)

Submission status: Accepted

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

print(s-2)

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    T = int(math.sqrt(N))
    ret = float('inf')
    for i in range(1, T+1):
        if N % i == 0:
            ret = min(ret, N//i+i-2)
    print(ret)


if __name__ == '__main__':
    main()