Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, N) for (int i = 0; i < (N); ++i)
#define for1n(i, N) for (int i = 1; i <= (N); ++i)
#define for1(i, N) for (int i = 1; i < (N); ++i)
#define forn(i, N) for (int i = 0; i <= (N); ++i)
#define forx(i, N, x) for (int i = (x); i < (N); ++i)
#define forl(i, N, x, a)                                                       \
  for (int i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;

map<ll, ll> pf(ll x) {
  ll y = x;
  ll a = 2;
  map<ll, ll> m;
  while (a * a <= x) {
    if (y % a == 0) {
      y /= a;
      ++m[a];
    } else {
      ++a;
    }
  }
  if (y != 1)
    ++m[y];

  return m;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  long long N;

  cin >> N;

  long long result = 1e16;

  // for(auto &&i : pf(N)){
  //     for (ll j = 1; j <= i.second; ++j)
  //         result =
  //             (result < 0) ?
  //             (N / (ll)pow(i.first, j) + (ll)pow(i.first, j)) :
  //             min(result, N / (ll)pow(i.first, j) + (ll)pow(i.first, j));
  // }

  // cout << result - 2;

  for0(i, sqrt(N) + 1) {
    if (N % i == 0) {
      result = min(result, N / i + i);
    }
  }

  cout << result - 2;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, N) for (int i = 0; i < (N); ++i)
#define for1n(i, N) for (int i = 1; i <= (N); ++i)
#define for1(i, N) for (int i = 1; i < (N); ++i)
#define forn(i, N) for (int i = 0; i <= (N); ++i)
#define forx(i, N, x) for (int i = (x); i < (N); ++i)
#define forl(i, N, x, a)                                                       \
  for (int i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;

map<ll, ll> pf(ll x) {
  ll y = x;
  ll a = 2;
  map<ll, ll> m;
  while (a * a <= x) {
    if (y % a == 0) {
      y /= a;
      ++m[a];
    } else {
      ++a;
    }
  }
  if (y != 1)
    ++m[y];

  return m;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  long long N;

  cin >> N;

  long long result = 1e16;

  // for(auto &&i : pf(N)){
  //     for (ll j = 1; j <= i.second; ++j)
  //         result =
  //             (result < 0) ?
  //             (N / (ll)pow(i.first, j) + (ll)pow(i.first, j)) :
  //             min(result, N / (ll)pow(i.first, j) + (ll)pow(i.first, j));
  // }

  // cout << result - 2;

  for1(i, sqrt(N) + 1) {
    if (N % i == 0) {
      result = min(result, N / i + i);
    }
  }

  cout << result - 2;

  return 0;
}