Problem:
Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.

Solution in Scala (2.13.1) (judge verdict: Time Limit Exceeded)

import java.util.Scanner

object Main extends App {

  def readALine(c:Int, ac:List[Long]=Nil):List[Long] =
    if (c > 0) readALine(c-1, sc.nextLong::ac) else ac.reverse
  // def readLines(r:Int, c:Int, ac:List[List[Int]]):List[List[Int]] =
  //   if (r > 0) readLines(r-1, c, readALine(c, Nil)::ac) else ac.reverse
  val sc = new Scanner(System.in)
  val N = sc.nextInt

  def makePrime():(()=>Long, ()=>Unit) = {
    def eratosthenes(sieve:List[Long], primes:List[Long]):List[Long] = {
      if (sieve.isEmpty) primes.reverse
      else eratosthenes(sieve.tail.filter(_ % sieve.head != 0), sieve.head::primes)
    }
    val primes:Array[Long] = eratosthenes(List.range(3, 9999, 2), List(2)).toArray
    // println(primes.mkString(","))
    var num = 0
    val limit = primes.length
    (() => { if (num < limit) {num +=1; primes(num-1)} else throw new NoSuchElementException },
     () => { num = 0})
  }

  val (generator, resetGenerator) = makePrime()
 
  def factorize(n:Long):List[(Long, Long)] = {
    require(n > 0, "n must be a positive integer.")

    def iter(n:Long, ac:List[(Long, Long)]):List[(Long, Long)] = {
      def isDivided(n:Long, d:Long):Boolean = n % d == 0
      def iterDev(n:Long, d:Long, c:Long):(Long, Long) =
        if (isDivided(n, d)) iterDev(n/d, d, c+1) else (n, c)
      
      val div = generator()
      // println(s"n:$n ac:$ac div:$div")
      n match {
        case 1 => ac
        case n if n < div*div => (n, 1L)::ac
        case _ => {
          val (q, c) = iterDev(n, div, 0)
          if (c > 0) iter(q, (div, c)::ac) else iter(q, ac)
        }
      }
    }

    resetGenerator()
    iter(n, List()).reverse  
  }

  def countDivisor(ls:List[(Long,Long)], count:Long):Long = {
    // println(s"ls:$ls count:$count")
    if (ls.isEmpty) count else {
      val (p, e) = ls.head
      countDivisor(ls.tail, count*(e+1)) 
    }
  }
  def solver(n:Long, sum:Long):Long = 
    if (n > 0) solver(n -1, sum + n*countDivisor(factorize(n), 1)) else sum

  println(solver(N, 0))

}