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.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let n = i.read::<usize>();
    writeln!(o, "{}", solve(n)).unwrap();
}
fn solve(n: usize) -> usize {
    let mut ans = 0;
    divisors(n, |x| {
        let m_1 = n / x;
        let m = m_1 - 1;
        if x < m {
            ans += m;
        }
    });
    ans
}
fn divisors<F: FnMut(usize)>(n: usize, mut f: F) {
    fn divisors_by<F: FnMut(usize)>(fs: &[(usize, usize)], mut n: usize, f: &mut F) {
        if fs.is_empty() {
            f(n);
            return;
        }
        let (p, c) = fs[0];
        for _ in 0..c + 1 {
            divisors_by(&fs[1..], n, f);
            n *= p;
        }
    }
    divisors_by(&factorize(n), 1, &mut f)
}
fn factorize(mut n: usize) -> Vec<(usize, usize)> {
    assert!(n != 0);

    fn try_push_factor(n: &mut usize, f: usize, fs: &mut Vec<(usize, usize)>) -> bool {
        let mut c = 0;
        while *n % f == 0 {
            *n /= f;
            c += 1;
        }
        if c != 0 {
            fs.push((f, c));
            true
        } else {
            false
        }
    }
    let mut fs = Vec::new();

    try_push_factor(&mut n, 2, &mut fs);
    let mut f = 3;
    let mut end = (n as f64).sqrt() as usize + 1;
    while f < end {
        if try_push_factor(&mut n, f, &mut fs) {
            end = (n as f64).sqrt() as usize + 1;
        }
        f += 2;
    }
    if n != 1 {
        fs.push((n, 1));
    }
    fs
}


mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }

    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}