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: Accepted)

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

namespace diverta2019D
{
    class Program
    {
        static void Main(string[] args)
        {
            long N = CIN.Long();

            Dictionary<long, int> soinsuu = new Dictionary<long, int>();
            long temp = N;
            long a = 2;
            double Root = Math.Sqrt(N);
            while (temp > 1)
            {
                if (temp % a == 0)
                {
                    if (soinsuu.ContainsKey(a))
                    {
                        soinsuu[a]++;
                        temp /= a;
                        continue;
                    }

                    soinsuu.Add(a, 1);
                    temp /= a;
                    continue;
                }

                a++;
                if (a > Root)
                {
                    soinsuu.Add(temp, 1);
                    break;
                }
            }

            long flag = 1;
            foreach (KeyValuePair<long, int> sosuu in soinsuu)
            {
                flag *= sosuu.Value + 1;
            }

            Dictionary<long, bool> aa = new Dictionary<long, bool>();
            long ans = 0;
            for (int i = 0; i < flag; i++)
            {
                int l = i;
                long K = 1;
                foreach (var keyValuePair in soinsuu)
                {
                    int ko = l % (keyValuePair.Value + 1);
                    K *= (long) Math.Pow(keyValuePair.Key, ko);
                    l /= keyValuePair.Value + 1;
                }

                if (K > N / K + 1 && !aa.ContainsKey(K))
                {
                    aa.Add(K, true);
                    ans += K - 1;
                }
            }

            Console.WriteLine(ans);
        }
    }

    internal static class CIN
    {
        public static string String()
        {
            return Console.ReadLine();
        }

        public static int Int()
        {
            return int.Parse(Console.ReadLine());
        }

        public static int[] IntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }

        public static long Long()
        {
            return long.Parse(Console.ReadLine());
        }

        public static long[] LongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] DoubleArray()
        {
            return Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
        }
    }
}