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: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n = int(input())
    for a in range(1,10**6+1):
        if n%a==0:
            b=n//a
        ans=a+b-2
    print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

num = set()
for n in range(2, 10**6):
    if N % n == 0:
        num.add(tuple(sorted([n, N//n])))
    if N == n:
        break
ans = (10**12, 10**12)
if not num:
    print(N - 1)
else:
    for s in num:
        if s[0]+s[1] < ans[0]+ans[1]:
            ans = s
    print(ans[0] + ans[1] - 2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from operator import mul
from functools import reduce
import itertools

N = int(input())

# N = i*jをみつける
# (i, j)


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

prime_list = prime_factorize(N)


#print(prime_list)


import copy

result = 10**12

for i in range(len(prime_list)//2):
    # i個抽出
    tmp = list(itertools.combinations(prime_list, i+1))
    tm = list(map(list, tmp))
    for _ in tm:
        temp_origin_list = copy.copy(prime_list)
        for item in _:
            temp_origin_list.remove(item)
        #print(_, temp_origin_list)
        result = min(result, reduce(mul, _) + reduce(mul, temp_origin_list) - 2)
print(result)

Submission status: Wrong Answer

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

def main():
    N = int(input())
    d = make_divisors(N)
    ans = float('inf')
    for i in range(N):
        if len(d) == 1:
            ans = min(ans, 2*(d.pop()-1))
            break
        if len(d) == 0:
            break
        a = d.pop()-1
        b = d.pop()-1
        ans = min(ans, a+b)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from operator import mul
from functools import reduce

N = int(input())

# N = i*jをみつける
# (i, j)


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

prime_list = prime_factorize(N)


fact1 = [1]
fact2 = [1]

#print(prime_list)


for i in range(len(prime_list)//2):
    left = i
    right = len(prime_list) - i - 1
    if i % 2 == 0 and len(prime_list) > 3:
        fact1.append(prime_list[left])
        fact1.append(prime_list[right])
    elif len(prime_list) > 3:
        fact2.append(prime_list[left])
        fact2.append(prime_list[right])
    else:
        fact1.append(prime_list[left])
        fact2.append(prime_list[right])
if len(prime_list)%2 == 1:
    if reduce(mul, fact1) > reduce(mul, fact2):
        fact2.append(prime_list[len(prime_list)//2])
    else:
        fact1.append(prime_list[len(prime_list)//2])

#print(fact1, fact2)
print(reduce(mul, fact1) + reduce(mul, fact2) - 2)

Submission status: Wrong Answer

Solution code:
from operator import mul
from functools import reduce

N = int(input())

# N = i*jをみつける
# (i, j)


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

prime_list = prime_factorize(N)


fact1 = [1]
fact2 = [1]

#print(prime_list)


for i in range(len(prime_list)//2):
    left = i
    right = len(prime_list) - i - 1
    if i % 2 == 0:
        fact1.append(prime_list[left])
        fact1.append(prime_list[right])
    else:
        fact2.append(prime_list[left])
        fact2.append(prime_list[right])
if len(prime_list)%2 == 1:
    if reduce(mul, fact1) > reduce(mul, fact2):
        fact2.append(prime_list[len(prime_list)//2])
    else:
        fact1.append(prime_list[len(prime_list)//2])

#print(fact1, fact2)
print(reduce(mul, fact1) + reduce(mul, fact2) - 2)

Submission status: Wrong Answer

Solution code:
from operator import mul
from functools import reduce

N = int(input())

# N = i*jをみつける: 行列


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

prime_list = prime_factorize(N)


fact1 = [1]
fact2 = [1]

#print(prime_list)


for i in range(len(prime_list)//2):
    left = i
    right = len(prime_list) - i - 1
    fact1.append(prime_list[left])
    fact2.append(prime_list[right])
if len(prime_list)%2 == 1:
    if reduce(mul, fact1) > reduce(mul, fact2):
        fact2.append(prime_list[len(prime_list)//2])
    else:
        fact1.append(prime_list[len(prime_list)//2])

print(reduce(mul, fact1) + reduce(mul, fact2) - 2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

n=int(input())

c=[]

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

if len(c)==0:
    print(n-1)
    sys.exit()
    
print(min(c))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from math import sqrt

def main():
    N = int(input())
    ans = 1e12 + 1
    for i in range(1, int(sqrt(N))):
        if N % i == 0:
            j = N // i
            ans = min(ans, i + j - 2)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

    return arr

q = factorization(n)
ans_list = []
for i in range(len(q)):
    for j in range(q[i][1]):
        ans_list.append(q[i][0])

if len(ans_list) == 1:
    print(ans_list[0]-1)
    exit()
        
ans_list = sorted(ans_list,reverse = True)

a = ans_list[0]
b = ans_list[1]
for i in range(len(ans_list)-2):
    if a <= b:
        a *= ans_list[i + 2]
    else:
        b *= ans_list[i+2]
        
print(a+b-2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 10**13

for x in range(1,1000000):
    y = N/x
    if y % 1 ==0:
        ans = x + y - 2
        a = min(a,ans)
print(int(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
m = 10**13
for x in range(1,1000001):
  y = n / x
  if y % 1 == 0:
    v = x + y -2
    m = min(m, v)
print(m)

Submission status: Wrong Answer

Solution code:
from math import sqrt
from functools import reduce

def bitsearch(m, pfs):
    minSoFar = 10*12+1
    for i in range(1 << m):
        current1 = 1
        current2 = 1
        for k in range(m):
            if i >> k & 1 == 1:
                current1 *= pfs[k]
            else:
                current2 *= pfs[k]
        minSoFar = min(minSoFar, current1-1+current2-1)
    return minSoFar
n = int(input())
n2 = n
pfs = []
isP = True
i = 2
while i <= int(sqrt(n)):
    while n % i == 0:
        pfs.append(i)
        n //= i
        isP = False
    i += 1
if n > 1:
	pfs.append(n)
if isP:
    ans =  (n2-1)
elif len(pfs) > 2:
    ans = bitsearch(len(pfs), pfs)
else:
    ans = pfs[-1]-1 + n2//pfs[-1]-1
print (ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt
from functools import reduce
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)

def lcm(a, b):
    return a*b//gcd(a, b)
n = int(input())
n2 = n
pfs = []
isP = True
i = 2
while i <= int(sqrt(n)):
    while n % i == 0:
        pfs.append(i)
        n //= i
        isP = False
    i += 1
pfs.append(n)

if isP:
    ans =  (n2-1)
elif len(pfs) > 2:
    l = reduce(lcm, pfs)
    ans = l-1 + n2//l-1
else:
    ans = pfs[-1]-1 + n2//pfs[-1]-1
print (ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt
from functools import reduce
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)

def lcm(a, b):
    return a*b//gcd(a, b)
n = int(input())
n2 = n
pfs = []
isP = True
i = 2
while i <= int(sqrt(n)):
    if n % i == 0:
        pfs.append(i)
        n //= i
        isP = False
    i += 1
pfs.append(n)
if isP:
    ans =  (n2-1)
elif len(pfs) > 2:
    l = reduce(lcm, pfs)
    ans = l-1 + n2//l-1
else:
    ans = pfs[-1]-1 + n2//pfs[-1]-1
print (ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

N = int(input())
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()
m=len(divisors)
print(divisors[m//2]+divisors[m//2-1]-2)

Submission status: Wrong Answer

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

n = int(math.ceil(math.sqrt(N)))

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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
X = prime_factorize(n)
X = list(set(X))
m = 1+n
for v in X:
    if v+(n//v)<m:
        m = v+(n//v)

print(m-2)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

m = 1+n
for v in prime_factorize(n):
    if v+(n//v)<m:
        m = v+(n//v)

print(m-2)

Submission status: Wrong Answer

Solution code:
from functools import reduce

def factorization(n):
    '''
    素因数分解の結果を返す
    例： n = 100 の時、[2, 2, 5, 5] 
    '''
    arr = []
    tmp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if tmp % i == 0:
            while tmp % i == 0:
                arr.append(i)
                tmp //= i
    if tmp != 1:
        arr.append(tmp)
    if arr == []:
        arr.append(n)
    return arr


def combination(l):
    res = []
    for i in range(1, 2**len(l)):
        tmp = []
        for j in range(len(l)):
            if (i >> j) &1 == 1:
                tmp.append(l[j])
        res.append(tmp)
    return res


def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]


def solve():
    N = int(input())
    F = factorization(N)
    ans = N - 1
    for c in get_unique_list(combination(F)):
        print(c)
        t = reduce(lambda x, y: x * y, c)
        ans = min(ans, N // t - 1 + t - 1)
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def factorization(n):
    '''
    素因数分解の結果を返す
    '''
    arr = []
    temp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append((i, cnt))
    if temp != 1:
        arr.append((temp, 1))
    if arr == []:
        arr.append((n, 1))
    return arr


def solve():
    N = int(input())
    F = factorization(N)
    t = 1
    ans = N - 1
    for f in F:
        p, q = f
        while q > 0:
            ans = min(ans, N // t - 1 + t - 1)
            t *= p
            q -= 1
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
mini = float('inf')

if int(math.sqrt(N)) < 2:
    if N == 2:
        print(1)
    elif N == 3:
        print(2)
    exit()

if int(math.sqrt(N)) == N // int(math.sqrt(N)):
    print(int(math.sqrt(N))*2 - 2)
    exit()

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

print(mini)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
mini = float('inf')

if int(math.sqrt(N)) < 2:
    if N == 2:
        print(1)
    elif N == 3:
        print(2)
    exit()

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

print(mini)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
mini = float('inf')

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

print(mini)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
max_quo = float('inf')
max_mod = 0

for i in range(1, int(math.sqrt(N))):
    if N%i == 0 and (N//i) < max_quo:
        max_quo = N//i
        max_mod = i
    elif max_quo < max_mod:
        break

print(max_quo + max_mod - 2)

Submission status: Wrong Answer

Solution code:
N = int(input())
max_quo = float('inf')
max_mod = 0

for i in range(1, N):
    if N%i == 0 and (N//i) < max_quo:
        max_quo = N//i
        max_mod = i
    elif max_quo < max_mod:
        break

print(max_quo + max_mod - 2)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
import math
n=int(input())
ans=n
for i in range(1,int(math.sqrt(n))):
    if n%i==0:
        a=i
        b=int(n/i)
        if a+b-2<ans:
            ans=a+b-2
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
import math
n=int(input())
ans0=0
ans1=n
for i in range(1,int(math.sqrt(n))):
    if n%i==0:
        if abs(ans1-ans0)>abs(int(n/i)-i):
            ans1,ans0=int(n/i),i
print(ans0+ans1-2)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
n=int(input())
ans0=0
ans1=n
for i in range(1,n//2+1):
    if n%i==0:
        if abs(ans1-ans0)>abs(int(n/i)-i):
            ans1,ans0=int(n/i),i
            break
print(ans0+ans1-2)

Submission status: Wrong Answer

Solution code:
N = int(input())
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
a = prime_factorize(N)
count=len(a)
if count>0:
  num = 1
  num1 = 1
  for i in range(count):
    if num<=num1:
      num = num*a[i]
    elif num>num1:
      num1 = num1*a[i]
  print(num1+num-2)

Submission status: Wrong Answer

Solution code:
import math


def isPrime(n):
  if n == 2:
    return True
  if n % 2 == 0:
    return False
  
  # nの平方根まで計算する
  m = math.floor(math.sqrt(n)) + 1
  for p in range(3, m, 2):
      if n % p == 0:
        return False
  return True

n = int(input())
ans = 10**15

if isPrime(n):
    print(n-1)
    exit()
for i in range(10**4+1):
    for j in range(i,10**4+1):
        if i*j == n:
            ans = min(ans,i+j-2)
print(ans)

Submission status: Wrong Answer

Solution code:
import math


def isPrime(n):
  if n == 2:
    return True
  if n % 2 == 0:
    return False
  
  # nの平方根まで計算する
  m = math.floor(math.sqrt(n)) + 1
  for p in range(3, m, 2):
      if n % p == 0:
        return False
  return True

n = int(input())
ans = 10**15

if isPrime(n):
    print(n-1)
    exit()
for i in range(10**3+1):
    for j in range(i,10**3+1):
        if i*j == n:
            ans = min(ans,i+j-2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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]

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

yaku_list = make_divisors(n)
index = int(len(yaku_list)/2)

result = yaku_list[index-1] - 1 + yaku_list[index] - 1 
print(result)

Submission status: Wrong Answer

Solution code:
import sys

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]

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

yaku_list = make_divisors(n)
print(yaku_list)
index = int(len(yaku_list)/2)

result = yaku_list[index-1] - 1 + yaku_list[index] - 1 
print(result)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=10**12
a=1
while n**0.5>a:
  if (n/a).is_integer():
    if a*(n//a)==n:
      ans=min(ans,a+n//a-2)
  a+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=10**12
a=1
while n**0.5>a:
  if a*(n//a)==n:
    ans=min(ans,a+n//a-2)
  a+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


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


p = prime_factorize(n)
ans = 10**13
p.append(1)
for pp in p:
    ans = min(ans, pp+n//pp-2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


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


p = prime_factorize(n)
ans = 10**13

for pp in p:
    ans = min(ans, pp+n//pp-2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


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


p = prime_factorize(n)
ans = 10**13
pr = 1
for pp in p:
    pr *= pp

pr0 = 1
for i in p:
    pr0 *= i
    pr //= i
    tmp = pr0+pr-2
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


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


p = prime_factorize(n)
ans = 10**13
pr = 1
for pp in p:
    pr *= pp
p.append(1)

pr0 = 1
for i in p:
    pr0 *= i
    pr //= i
    tmp = pr0+pr-2
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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


p = prime_factorize(n)

ans = 10**15
pr = 1
for pp in p:
    pr *= pp
p.append(1)

pr0 = 1
for i in p:
    tmp = pr0*i+pr//i-2
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
x = int(N**0.5) - 1
ans_1 = N

for i in range(x):
    if N % x == 0:
        ans_1 = x
        break
    else:
        x += 1

ans_2 = N//ans_1
print(ans_1 + ans_2 - 2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

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

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

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

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

n = I()

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]

ans = make_divisors(n)
# print(ans)

m = INF
for i in range(len(ans)//2):
    c = ans[i] - 1 + ans[-i-1] - 1
    # print(c)
    m = min(m,c)
print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
def eratos(n):
   prime_list =set ()
   num_list=set()
   for i in range(2, n+1):
       if i not in num_list:
           prime_list.add(i)
           for j in range(i*i, n+1, i):
               num_list.add(j)
   return(prime_list)
s=eratos(n)
x=int(n**0.5)
while 1:
    if x in s and n%x==0:
        break
    x-=1
print(x+(n//x)-2)

Submission status: Wrong Answer

Solution code:
n=int(input())
def eratos(n):
   prime_list =set ()
   num_list=set()
   for i in range(2, n+1):
       if i not in num_list:
           prime_list.add(i)
           for j in range(i*i, n+1, i):
               num_list.add(j)
   return(prime_list)
s=eratos(n)
x=int(n**0.5)
while 1:
    if x in s and n%x==0:
        break
    x-=1
    print(x)
print(x+(n//x)-2)

Submission status: Wrong Answer

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

pri_fact=prime_factorize(n)

def prod(x):
    kotae=1
    for i in x:
        kotae*=i
    return kotae

ans=10**120
for i in range(len(pri_fact)):
    ans=min((prod(pri_fact[0:i])-1)+(prod(pri_fact[i:])-1),ans)

print(int(ans))

Submission status: Wrong Answer

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

pri_fact=prime_factorize(n)

def prod(x):
    kotae=1
    for i in x:
        kotae*=i
    return kotae

ans=10**12
for i in range(len(pri_fact)):
    ans=min((prod(pri_fact[0:i])-1)+(prod(pri_fact[i:])-1),ans)

print(int(ans))

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

sys.setrecursionlimit(1000000)
dire, dire8 = [[1, 0], [0, 1], [-1, 0], [0, -1]], [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets, ALPHABETS = "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD, INF = 1000000007, float("inf")

def factors(n):
    d = []
    a = 2
    while n >= a ** 2:
        if n % a == 0:
            n //= a
            d.append(a)
        else:
            a += 1
    d.append(n)
    return d

def main():
    N = I()
    i, j = 1, N
    ans = i + j - 2

    for div in factors(N):
        i *= div
        j //= div
        ans = min(ans, i + j - 2)

    print(ans)



if __name__ == '__main__':
    main()