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 Kotlin (1.3.71) (judge verdict: Accepted)

import kotlin.system.measureTimeMillis
import kotlin.time.measureTime

class Sieve(n: Int) {
    private val f = IntArray(n + 1)
    private val primes = mutableListOf<Int>()

    init {
        f[0] = -1
        f[1] = -1
        for (i in 2..n) {
            if (f[i] > 0) continue
            primes.add(i)
            f[i] = i
            if (i.toLong() * i.toLong() > n) continue
            for (j in i * i..n step i) {
                if (f[j] == 0) f[j] = i
            }
        }
    }

    fun factor(x: Int): MutableMap<Int, Int> {
        val r = mutableMapOf<Int, Int>()
        var x1 = x
        while (x1 != 1) {
            r[f[x1]] = r.getOrElse(f[x1]) { 0 } + 1
            x1 /= f[x1]
        }
        return r
    }
}

fun main() {
    val sieve = Sieve(10000000)
    val n = readLine()!!.toInt()
    var ans = 0L
    for (i in 1..n) {
        var x = 1L
        for ((k, v) in sieve.factor(i)) {
            x *= (v + 1)
        }
        ans += i * x
    }
    println(ans)
}