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 C++14 (Clang 3.8.0) (judge verdict: Accepted)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
#include <complex>
#include <omp.h>
using namespace std;
#define eps 0.000001
#define LONG_INF 100000000000000
#define GOLD 1.61803398874989484820458
#define seg_size 65536*4
#define REP(i,n) for(long long i = 0;i < n;++i)
unsigned long xor128() {
	static unsigned long x = time(NULL), y = 362436069, z = 521288629, w = 88675123;
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
int main() {
	long long n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<pair<long long, long long>> input;
	long long geko = n;
	long long topper = 1;
	for (int q = 2; q <= sqrt(geko); ++q) {
		if (geko % q == 0) {
			int cnt = 0;
			while (geko % q == 0) {
				cnt++;
				geko /= q;
			}
			topper *= cnt + 1LL;
			input.push_back(make_pair(q, cnt));
		}
	}
	if (geko != 1) {
		input.push_back(make_pair(geko, 1));
		topper *= 2LL;
	}
	long long ans = 0;
	for (int i = 1; i < topper; ++i) {
		long long now = i;
		long long geko = 1;
		for (int q = 0; now != 0; ++q) {
			REP(j, now % (input[q].second + 1)) {
				geko *= input[q].first;
			}
			now /= (input[q].second + 1LL);
		}
		long long m = geko - 1LL;
		if (n / m == n % m) {
			ans += m;
		}
	}
	cout << ans << endl;
	return 0;
}