Problem p02881 (C++)

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

typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef vector<ii> vpii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long int ll;
typedef unsigned long long int ull;

#define pb push_back

#define Add1(a, b) ady[a].pb(b)
#define Add1c(a, b, c) ady[a].pb({b, c})
#define AddS1(a, b) ady[a].insert(b)
#define AddS1c(a, b, c) ady[a].insert({b, c})

#define Add2(a, b) ady[a].pb(b), ady[b].pb(a)
#define Add2c(a, b, c) ady[a].pb({b, c}), ady[b].pb({a, c})
#define AddS2(a, b) ady[a].insert(b), ady[b].insert(a)
#define AddS2c(a, b, c) ady[a].insert({b, c}), ady[b].insert({a, c})

#define f first
#define s second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))

#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define DRT()                                                                  \
  int t;                                                                       \
  si(t);                                                                       \
  while (t--)

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define TRACE

#ifdef TRACE
#define trace cerr << "Hola mundo" << endl;
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

const int maxn = 1e5 + 1;
const int maxe = 20;
const int oo = 1 << 30;

int main() {
  long long n;
  cin >> n;

  long long sol = INFINITY;
  for (long long i = n / 2; i >= 1; i--) {
    if (i * (n / i) == n)
      sol = min(sol, (i - 1) + (n / i) - 1);
  }

  cout << sol;
}


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

typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef vector<ii> vpii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long int ll;
typedef unsigned long long int ull;

#define pb push_back

#define Add1(a, b) ady[a].pb(b)
#define Add1c(a, b, c) ady[a].pb({b, c})
#define AddS1(a, b) ady[a].insert(b)
#define AddS1c(a, b, c) ady[a].insert({b, c})

#define Add2(a, b) ady[a].pb(b), ady[b].pb(a)
#define Add2c(a, b, c) ady[a].pb({b, c}), ady[b].pb({a, c})
#define AddS2(a, b) ady[a].insert(b), ady[b].insert(a)
#define AddS2c(a, b, c) ady[a].insert({b, c}), ady[b].insert({a, c})

#define f first
#define s second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))

#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define DRT()                                                                  \
  int t;                                                                       \
  si(t);                                                                       \
  while (t--)

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define TRACE

#ifdef TRACE
#define trace cerr << "Hola mundo" << endl;
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

const int maxn = 1e5 + 1;
const int maxe = 20;
const int oo = 1 << 30;

int main() {
  long long n;
  cin >> n;

  long long sol = n - 1;
  for (long long i = 2; i <= (int)sqrt(n); i++) {
    if (i * (n / i) == n)
      sol = min(sol, (i - 1) + (n / i) - 1);
  }

  cout << sol;
}