Problem p03050 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef double ld;
const long long INF = (1LL << 62);
const double inf = pow(10, 10);
double pi = 3.141592653589793;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
const ll MOD = 1000000007;
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
template <class T> vector<ll> countVector(vector<T> v, T guard = 0) {
  ll size = v.size();
  v.push_back(guard);
  vector<ll> ret;
  ll count = 1;
  for (int i = 0; i < size; i++) {
    if (v[i] == v[i + 1]) {
      count++;
    } else {
      ret.push_back(count);
      count = 1;
    }
  }
  return ret;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int main() {
  init();
  ll N;
  cin >> N;
  ll ans = 0;
  for (int k = 1; k * k <= N; k++) {
    if (N % k != 0)
      continue;
    ll m = N / k - 1;
    if (k < m)
      ans += m;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef double ld;
const long long INF = (1LL << 62);
const double inf = pow(10, 10);
double pi = 3.141592653589793;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
const ll MOD = 1000000007;
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
template <class T> vector<ll> countVector(vector<T> v, T guard = 0) {
  ll size = v.size();
  v.push_back(guard);
  vector<ll> ret;
  ll count = 1;
  for (int i = 0; i < size; i++) {
    if (v[i] == v[i + 1]) {
      count++;
    } else {
      ret.push_back(count);
      count = 1;
    }
  }
  return ret;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int main() {
  init();
  ll N;
  cin >> N;
  ll ans = 0;
  for (ll k = 1; k * k <= N; k++) {
    if (N % k != 0)
      continue;
    ll m = N / k - 1;
    if (k < m)
      ans += m;
  }
  cout << ans << endl;
}