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 Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.collection.mutable.ArrayBuffer


object Main {
  def read() = {
    val sc = new Scanner(System.in)
    val n = sc.nextLong()
    n
  }


  def solve(n: Long): Long = {
    def isFavorite(k: Long): Boolean = k > 0 && n % k == n / k
    divisor(n).map(_ - 1).filter(isFavorite).sum
  }

  def experiment(n: Long): Unit = {
    def isFavorite(k: Int): Boolean = n % k == n / k
    for(k<- 1 to n.toInt){
      println(s"k=$k, (${n/k}, ${n%k}), ${isFavorite(k)}")
    }
  }

  def divisor(n: Long): IndexedSeq[Long] = {
    val buf = ArrayBuffer.empty[Long]
    for (i <- 1 to Math.sqrt(n).toInt) {
      if (n % i == 0) {
        buf.append(i)
        if (i * i != n) {
          buf.append(n / i)
        }
      }
    }
    buf.toIndexedSeq
  }

  def experiment2(n: Long): Unit = {
    def isFavorite(k: Int): Boolean = n % k == n / k
    def printInfo(k:Int):Unit = {
      println(s"k=$k, (${n/k}, ${n%k}), ${isFavorite(k)}")
    }
    (1 to n.toInt).filter(isFavorite).foreach(printInfo)

    println("divisor")
    divisor(n).sorted.foreach(println)
  }

  def main(args: Array[String]): Unit = {
    //experiment2(200)
    val n = read()
    println(solve(n))
  }
}