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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::input;

fn main() {
    input! {
        n: usize
    }

    let ans = {
        let mut ans = 0;
        for &x in divisors(n).iter().rev() {
            if x*(x-1) > n {
                ans += x-1;
            } else {
                break;
            }
        }
        ans
    };

    println!("{}",ans)
    
}

pub fn divisors(n: usize) -> Vec<usize> {
    let fs = factor(n);
    let mut ret = vec![];
    gen_divisors(&fs[..], 1, &mut ret);
    ret
}

fn gen_divisors(s: &[(usize, usize)], cur: usize, ret: &mut Vec<usize>) {
    if s.is_empty() {
        ret.push(cur);
        return;
    }

    let mut cur = cur;
    for _ in 0..=s[0].1 {
        gen_divisors(&s[1..], cur, ret);
        cur = cur.wrapping_mul(s[0].0);
    }
}

pub fn factor(n: usize) -> Vec<(usize, usize)> {
    let mut n = n;
    let mut ret = vec![];
    let ps = primes((n as f64).sqrt() as usize + 2);
    for p in ps {
        let mut cnt = 0;
        while n % p == 0 {
            n /= p;
            cnt += 1;
        }
        if cnt > 0 {
            ret.push((p, cnt));
        }
    }
    if n > 1 {
        ret.push((n, 1));
    }
    ret
}

pub fn primes(n: usize) -> Vec<usize> {
    let mut tbl = vec![true; n];
    let mut ps = vec![];
    tbl[0] = false;
    tbl[1] = false;
    for i in 2..n {
        if tbl[i] {
            ps.push(i);
            let mut j = i + i;
            while j < n {
                tbl[j] = false;
                j += i;
            }
        }
    }
    ps
}