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.

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

public static class Program
{
    public static void Main(string[] args)
    {
        var n = long.Parse(Console.ReadLine());
        var factors = Factoring(n).ToList();
        factors.Add(1);
        long minValue = long.MaxValue;
        for(var i = 1; i < factors.Count; i++)
        {
            foreach (var x in Combinations(factors, i))
            {
                var y = new List<long>(factors);
                foreach (var e in x)
                {
                    y.Remove(e);
                }
                minValue = Math.Min(minValue, x.Aggregate((now, next) => now * next) + y.Aggregate((now, next) => now * next));
            }
        }
        Console.WriteLine(minValue - 2);
    }

    private static IEnumerable<long> Factoring(long number)
    {
        if (number == 1)
            yield break;
        // 最小の素数をリストに
        var primes = new List<long>() { 2 };
        var tmp = number;
        // 割れなくなったらやめる
        while (tmp != 1)
        {
            foreach (var prime in primes)
            {
                // 剰余が発生するまで割り続けて、終わったら次の素数に
                while (tmp % prime == 0)
                {
                    tmp = tmp / prime;
                    yield return prime;
                }
            }
            if (tmp == 1)
                // 割れなくなったらやめる
                yield break;
            var maxPrime = primes.Max();
            if (Math.Sqrt(tmp) < maxPrime)
            {
                // 最大の素数が残っている数の平方より大きいなら残っている数は素数
                primes.Add(tmp);
                continue;
            }
            // 次の素数を求める
            primes.Add(LongRange(maxPrime, tmp - maxPrime)
                .First(s1 => !primes.Any(s2 => s1 % s2 == 0)));
        }
    }

    private static IEnumerable<int> CountPrime(int min = 0, int max = int.MaxValue)
    {
        return Enumerable.Range(2, max - 1)
            .Where(s1 => Enumerable.Range(2, (int)Math.Sqrt(s1))
                .All(s2 => s1 == s2 || s1 % s2 != 0))
            .SkipWhile(s => s < min);
    }

    private static IEnumerable<long> LongRange(long startNum, long count)
    {
        for (var i = startNum; i < startNum + count; i++)
            yield return i;
    }

    private static IEnumerable<List<long>> Combinations(List<long> src, int n)
    {
        return Enumerable.Range(0, n - 1)
            .Aggregate(
                Enumerable.Range(0, src.Count() - n + 1)
                    .Select(num => new List<int>() { num }),
                (list, _) => list.SelectMany(c =>
                    Enumerable.Range(c.Max() + 1, src.Count() - c.Max() - 1)
                        .Select(num => new List<int>(c) { num })
                    )
                )
            .Select(c => c
                .Select(num => src[num])
                .ToList()
                );
    }
}