Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
#define int ll
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pi;
#define trace(x) cout << #x << "=" << x << "\n";
#define llp 1000000007
#define mod 1000000007

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;
int ans = 0;
void fun(int n) {
  for (int i = 1; i * i <= (n); i++) {
    if (n % i == 0) {
      if (n % i == i) {
        if (i != 1 && (n / (i - 1)) == (n % (i - 1))) {
          ans += i - 1;
        }
      } else {
        if (i != 1 && (n / (i - 1)) == (n % (i - 1))) {
          ans += i - 1;
        }

        if ((n / i) != 0 && (n / ((n / i) - 1)) == (n % ((n / i) - 1))) {
          ans += (n / i) - 1;
        }
      }
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  fun(n);
  // int sum=0;
  // for(int i=1;i<=n;i++){
  //     if((n/i)==(n%i)){
  //         deb(i);
  //         sum+=i;
  //     }
  // }
  // cout<<sum<<"\n";
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
#define int ll
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pi;
#define trace(x) cout << #x << "=" << x << "\n";
#define llp 1000000007
#define mod 1000000007

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;
int ans = 0;
void fun(int n) {
  for (int i = 1; i * i <= (n); i++) {
    if (n % i == 0) {
      if (n % i == i) {
        if (i != 1 && (n / (i - 1)) == (n % (i - 1))) {
          ans += i - 1;
        }
      } else {
        if (i != 1 && (n / (i - 1)) == (n % (i - 1))) {
          ans += i - 1;
        }

        if ((n / i) != 0 && (n / ((n / i) - 1)) == (n % ((n / i) - 1))) {
          ans += (n / i) - 1;
        }
      }
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  if (n == 1) {
    cout << 0 << "\n";
    return 0;
  }

  fun(n);
  // int sum=0;
  // for(int i=1;i<=n;i++){
  //     if((n/i)==(n%i)){
  //         deb(i);
  //         sum+=i;
  //     }
  // }
  // cout<<sum<<"\n";
  cout << ans << "\n";

  return 0;
}