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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define REP(i,N) for(i=0;i<N;i++)
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
  typedef long long ll;
  typedef pair<int,int> PII;
  typedef pair<char,char> PCC;
  typedef pair<ll,ll> PLL;
  typedef pair<char,int> PCI;
  typedef pair<int,char> PIC;
  typedef pair<ll,int> PLI;
  typedef pair<int,ll> PIL;
  typedef pair<ll,char> PLC;
  typedef pair<char,ll> PCL;
int M=10;
map<ll,vector<ll> > memo;

vector<ll> dfs(ll N, ll cur){
  if(memo.count(cur)) return memo[cur];
  vector<ll> v;
  if(cur <= M){
    for(int i=2;i<=cur;i++){
      if(N/i == N%i) v.push_back(i);
    }
    memo[cur] = vector<ll>(0);
    return v;
  }
  ll pre = 0;
  for(int i=1; i<=M; i++){
    ll n = cur/i -1;
    if(n==pre)continue;
    if(n==0)break; 
    if(N/n == N%n)v.push_back(n);
    pre = n;
  }
  int i;REP(i,v.size()-1){
    if(v[i] == cur-1) continue;
    vector<ll> v2 = dfs(N, v[i]+1);
    v.insert(v.end(),v2.begin(),v2.end());
  }
  memo[cur] = vector<ll>(0);
  return v;
}

int main(void){
  ll N;cin >> N;
  vector<ll> v = dfs(N,N);
  sort(v.begin(),v.end());
  ll pre = 0;
  ll sum = 0;
  int i;REP(i,v.size()){
    if( pre!=v[i] ){
      sum += v[i]; 
    }
    pre = v[i];
  }
  cout << sum << endl;
  return 0;
}