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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.util.Arrays
import java.util.ArrayList

const val MOD: Long = (1e9+7).toLong()
fun Double.format(digits: Int) = String.format("%.${digits}f", this)

fun primes(n: Int) : IntArray {
    /** Return a list of primes not more than n **/
    val primes: ArrayList<Int> = arrayListOf()

    val isPrime = Array(n+1, {true})
    for (i in 2..n) {
        if (isPrime[i]) {
            primes.add(i)
            var buf = i * 2
            while (buf <= n) {
                isPrime[buf] = false
                buf += i
            }
        }
    }
    return primes.toIntArray()
}

fun prime_fact(n: Long) : Map<Int, Int> {
    /** Return a map of the numbers of prime factors **/
    var cntprime: MutableMap<Int, Int> = mutableMapOf()

    var m = n
    for (i in primes(Math.sqrt(n.toDouble()).toInt())) {
        while (m % i == 0L) {
            cntprime[i] = cntprime.getOrElse(i, {0}) + 1
            m /= i
        }
    }
    if (m != 1L) {
        cntprime[m.toInt()] = 1
    }

    return cntprime
}

fun main(args: Array<String>) {
    val n = readLine()!!.toLong()

    val facts = prime_fact(n)
    var res = 0L
    var list: ArrayList<Long> = arrayListOf(1)
    for ((k, v) in facts) {
        val list2 = Array(v+1, {Math.pow(k.toDouble(), it.toDouble()).toLong()})
        val list3: ArrayList<Long> = arrayListOf()
        for (e in list2) {
            list.forEach{
                list3.add(e * it)
            }
        }
        list = list3
    }
    for (e in list) {
        if (n/e < e-1) {
            res += e-1
        }
    }
    println(res)
}