Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  ll n;
  cin >> n;
  vector<ll> div;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    div.push_back(i);
    if (n / i != i)
      div.push_back(n / i);
  }
  div.push_back(n);
  ll res = 0;
  for (ll m : div) {
    --m;
    if (n / m == n % m)
      res += m;
  }
  cout << res << endl;
}

Fixed submission:
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  ll n;
  cin >> n;
  vector<ll> div;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    div.push_back(i);
    if (n / i != i)
      div.push_back(n / i);
  }
  if (n != 1)
    div.push_back(n);
  ll res = 0;
  for (ll m : div) {
    --m;
    if (n / m == n % m)
      res += m;
  }
  cout << res << endl;
}