Problem:
Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.

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

using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            // var line = consoleReader.SplitInt32();
            var N = consoleReader.Int64();
            if (N < 3) return 0;

            var factoring = Factoring(N).ToArray();
            var yakusu = new HashSet<long> { 1 };
            foreach (var f in factoring)
            {
                foreach (var y in yakusu.ToArray())
                {
                    yakusu.Add(y * f);
                }
            }

            long sum = 0;
            foreach (var y in yakusu.OrderBy(l => l))
            {
                var x = N / y - 1;
                if (x < 2)
                    break;
                if (x <= y)
                    continue;
                sum += x;
            }
            return sum;
        }

        // 参考 https://qiita.com/keidrumfreak/items/09c64998b7cd6eb59991
        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;
                }
                for (long l = maxPrime; l < tmp; l++)
                {
                    if (primes.All(s2 => l % s2 != 0))
                    {
                        primes.Add(l);
                        break;
                    }
                }
            }
        }

        private readonly ConsoleReader consoleReader;
        public Program() : this(new ConsoleReader()) { }
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program().Calc());
        public static string MultiLine<T>(IEnumerable<T> source) => string.Join("\n", source);
    }

    public class ConsoleReader
    {
        public virtual string String() => Console.ReadLine(); public int Int32() => int.Parse(String()); public long Int64() => long.Parse(String()); public ulong UInt64() => ulong.Parse(String()); public double Double() => double.Parse(String()); public string[] Split() => String().Split().ToArray(); public int[] SplitInt32() => String().Split().Select(int.Parse).ToArray(); public long[] SplitInt64() => String().Split().Select(long.Parse).ToArray(); public ulong[] SplitUInt64() => String().Split().Select(ulong.Parse).ToArray(); public double[] SplitDouble() => String().Split().Select(double.Parse).ToArray(); public IEnumerable<string> Repeat(int count) { for (int i = 0; i < count; i++) yield return String(); }
        public IEnumerable<int> RepeatInt32(int count) { for (int i = 0; i < count; i++) yield return Int32(); }
        public IEnumerable<long> RepeatInt64(int count) { for (int i = 0; i < count; i++) yield return Int64(); }
        public IEnumerable<string[]> RepeatSplit(int count) { for (int i = 0; i < count; i++) yield return Split(); }
        public IEnumerable<int[]> RepeatSplitInt32(int count) { for (int i = 0; i < count; i++) yield return SplitInt32(); }
    }
}