Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n)                                                      \
  for (int i = 0; i < m; i++) {                                                \
    for (int j = 0; j < n; j++) {                                              \
      cout << a[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define show(a, n)                                                             \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  NL
#define fast_io()                                                              \
  cin.sync_with_stdio(false);                                                  \
  cout.sync_with_stdio(false);                                                 \
  cin.tie(NULL);

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
int main() {
  fast_io()

      ll n;
  cin >> n;
  ll m = ceil(sqrt(n));
  for (int i = m; i >= 1; i++) {
    if (n % i == 0) {
      cout << (n / i) - 2 + i;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n)                                                      \
  for (int i = 0; i < m; i++) {                                                \
    for (int j = 0; j < n; j++) {                                              \
      cout << a[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define show(a, n)                                                             \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  NL
#define fast_io()                                                              \
  cin.sync_with_stdio(false);                                                  \
  cout.sync_with_stdio(false);                                                 \
  cin.tie(NULL);

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
int main() {
  fast_io()

      ll n;
  cin >> n;
  ll m = ceil(sqrt(n));
  for (int i = m; i >= 1; i--) {
    if (n % i == 0) {
      cout << (n / i) - 2 + i;
      return 0;
    }
  }

  return 0;
}