prithivMLmods/Coder-Stat huggingface dataset:
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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

  ll ans = 0;
  ll anss = 0;
  ll count = 0;
  ll i, j;

  ll K;
  vector<ll> a;

  ll Q, N;
  string S;
  cin >> N;
  ll flag = 0;
  ll t1 = 0;
  ll t2 = 0;

  for (i = (int)(sqrt(N) + 0.9); i < N + 1; i++) {
    if (N % i == 0) {

      cout << i + N / i - 2 << endl;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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

  ll ans = 0;
  ll anss = 0;
  ll count = 0;
  ll i, j;

  ll K;
  vector<ll> a;

  ll Q, N;
  string S;
  cin >> N;
  ll flag = 0;
  ll t1 = 0;
  ll t2 = 0;

  for (i = (int)(sqrt(N)); i > 0; i--) {
    if (N % i == 0) {

      cout << i + N / i - 2 << endl;
      return 0;
    }
  }

  return 0;
}
Problem p02881 (C++)

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

int32_t main() {

  int n;
  cin >> n;
  int ans = n - 1;
  for (int i = 1; i < n; ++i) {
    if (n % i == 0)
      ans = min(ans, i + n / i - 2);
  }

  cout << ans << endl;

  return 0;
}

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

int32_t main() {

  int n;
  cin >> n;
  int ans = n - 1;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0)
      ans = min(ans, i + n / i - 2);
  }

  cout << ans << endl;

  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
long long chmin(long long a, long long b) {
  if (a > b) {
    return b;
  }
  return a;
}

int main() {
  long long n;
  cin >> n;
  long long min = n - 1;
  for (long long i = 1; i <= (int)sqrt(n); i++) {
    for (long long j = i; j <= n; j++) {
      if (i * j > n)
        break;
      if (i * j == n) {
        min = chmin(min, i - 1 + j - 1);
      }
    }
  }
  cout << min << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
long long chmin(long long a, long long b) {
  if (a > b) {
    return b;
  }
  return a;
}

int main() {
  long long n;
  cin >> n;
  long long min = n - 1;
  for (long long i = 1; i <= (int)sqrt(n); i++) {
    long long j = n / i;
    if (n % i == 0) {
      min = chmin(min, i - 1 + j - 1);
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>

using namespace std;

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

  long dmax = sqrt(n);
  while (dmax--)
    if (n % dmax == 0)
      break;
  cout << dmax + n / dmax - 2 << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

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

  long dmax = sqrt(n) + 1;
  while (dmax--)
    if (n % dmax == 0)
      break;
  cout << dmax + n / dmax - 2 << endl;
}
Problem p02881 (C++)

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

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

  ll ans = n - 1;
  for (ll i = 1; i <= n; i++) {
    ll j = (ll)(n / i);
    if (i * j == n) {
      if (i + j - 2 < ans) {
        ans = i + j - 2;
      }
    }
  }

  cout << ans << endl;
}

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

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

  ll ans = n - 1;
  for (ll i = 1; i <= sqrt(n); i++) {
    ll j = (ll)(n / i);
    if (i * j == n) {
      if (i + j - 2 < ans) {
        ans = i + j - 2;
      }
    }
  }

  cout << ans << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

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

  ll ans = 10000000000000;

  for (ll i = 1; i <= n; i++) {

    if (n % i == 0) {
      ll x = i;
      ll y = n / i;
      ans = min(ans, x + y - 2);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

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

  ll ans = 10000000000000;

  for (ll i = 1; i <= sqrt(n); i++) {

    if (n % i == 0) {
      ll x = i;
      ll y = n / i;
      ans = min(ans, x + y - 2);
    }
  }

  cout << ans << endl;
}
Problem p02881 (C++)

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

int main() {
  long int i, j, N, min, flag;
  flag = 0;
  cin >> N;
  if (N > 1000000) {
    for (i = 1; i <= N / 1000; i++) {
      if (0 == N % i) {
        j = N / i;
        // 初回
        if (flag == 0) {
          min = (j + i) - 2;
          flag = flag + 1;
        }
        // ２回目以降
        if (min > (j + i) - 2) {
          min = (j + i) - 2;
        }
      }
    }
  } else {
    for (i = 1; i <= N; i++) {
      if (0 == N % i) {
        j = N / i;
        // 初回
        if (flag == 0) {
          min = (j + i) - 2;
          flag = flag + 1;
        }
        // ２回目以降
        if (min > (j + i) - 2) {
          min = (j + i) - 2;
        }
      }
    }
  }
  cout << min << endl;
}


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

int main() {
  long int i, j, N, min, flag;
  flag = 0;
  cin >> N;
  if (N > 1000000) {
    for (i = 1; i <= N / 10000; i++) {
      if (0 == N % i) {
        j = N / i;
        // 初回
        if (flag == 0) {
          min = (j + i) - 2;
          flag = flag + 1;
        }
        // ２回目以降
        if (min > (j + i) - 2) {
          min = (j + i) - 2;
        }
      }
    }
  } else {
    for (i = 1; i <= N; i++) {
      if (0 == N % i) {
        j = N / i;
        // 初回
        if (flag == 0) {
          min = (j + i) - 2;
          flag = flag + 1;
        }
        // ２回目以降
        if (min > (j + i) - 2) {
          min = (j + i) - 2;
        }
      }
    }
  }
  cout << min << endl;
}
Problem p02881 (C++)

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

int main() {
  long long int n, a, b, res;
  cin >> n;
  res = n + 1;
  for (int i = 2; i < n / 2; i++) {
    if (n % i == 0) {
      b = n / i;
      a = i;
      res = min(res, a + b);
    }
  }
  cout << res - 2;
  return 0;
}


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

int main() {
  long long int n, a, b, res;
  cin >> n;
  res = n + 1;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      b = n / i;
      a = i;
      res = min(res, a + b);
    }
  }
  cout << res - 2;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>

int main() {

  long N, ans = 0;

  scanf("%ld", &N);

  for (long i = sqrt(N); i < N; ++i) {
    if (N % i == 0) {
      ans = N / i + i - 2;
      break;
    }
  }

  printf("%ld", ans);
}

Fixed submission:
#include <math.h>
#include <stdio.h>

int main() {

  long N, ans = 0;

  scanf("%ld", &N);

  for (long i = sqrt(N); i > 0; --i) {
    if (N % i == 0) {
      ans = N / i + i - 2;
      break;
    }
  }

  printf("%ld", ans);
}
Problem p02881 (C++)

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

#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long int
#define f(i, g, n) for (int i = g; i <= n; i++)
#define rf(i, g, n) for (int i = n; i >= g; i--)

const int N = 1e3 + 1;

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  return 0;
}

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

#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long int
#define f(i, g, n) for (int i = g; i <= n; i++)
#define rf(i, g, n) for (int i = n; i >= g; i--)

const int N = 1e3 + 1;

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

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

  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  long long N;
  cin >> N;
  long long a = 1, b = N;
  long long M = a + b;
  for (long long i = N / 2; i > 1; i--) {
    if ((N % i == 0) && ((i + N / i) < M)) {
      a = i;
      b = N / i;
      M = a + b;
    }
  }
  if (a == 1 && b == 1)
    cout << "0" << endl;
  else
    cout << a + b - 2 << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long long N;
  cin >> N;
  long long a = 1, b = N;
  long long M = a + b;
  for (long long i = 2; i * i <= N; i++) {
    if ((N % i == 0) && ((i + N / i) < M)) {
      a = i;
      b = N / i;
      M = a + b;
    }
  }
  if (a == 1 && b == 1)
    cout << "0" << endl;
  else
    cout << a + b - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
typedef long long ll;

using namespace std;

int main() {
  ll N;

  cin >> N;
  ll a = 1000000000000;
  for (ll i = 1; i < (N / 2 + 1); i++) {
    if (N % i == 0) {
      if (a > (i + (N / i) - 2)) {
        a = i;
        a += (N / i) - 2;
      }
    }
  }

  cout << a;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
typedef long long ll;

using namespace std;

int main() {
  ll N;

  cin >> N;
  ll a = 10000000000000;
  for (ll i = 1; i < 5000001; i++) {
    if (N % i == 0) {
      if (a > (i + (N / i) - 2)) {
        a = i;
        a += (N / i) - 2;
      }
    }
  }

  cout << a;

  return 0;
}
Problem p02881 (C++)

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

using ll = long long;
using pp = pair<ll, ll>;
const ll amari = 1e9 + 7;
const char BC = 'A' - 'a';
#define ben(a) a.begin(), a.end()   // 昇順
#define enb(a) a.rbegin(), a.rend() // 降順
#define pb(a, b) a.push_back(b)
// fixed << setprecision(20)

int main() {
  ll n;
  cin >> n;
  ll a = sqrt(n);
  ll maxi = 0;
  for (ll i = 1; i < a; i++) {
    if (n % i == 0) {
      maxi = max(i, maxi);
    }
  }
  cout << maxi + (n / maxi) - 2 << endl;
  return 0;
}


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

using ll = long long;
using pp = pair<ll, ll>;
const ll amari = 1e9 + 7;
const char BC = 'A' - 'a';
#define ben(a) a.begin(), a.end()   // 昇順
#define enb(a) a.rbegin(), a.rend() // 降順
#define pb(a, b) a.push_back(b)
// fixed << setprecision(20)

int main() {
  ll n;
  cin >> n;
  ll a = sqrt(n);
  ll maxi = 0;
  for (ll i = 1; i <= a; i++) {
    if (n % i == 0) {
      maxi = max(i, maxi);
    }
  }
  cout << maxi + (n / maxi) - 2 << endl;
  return 0;
}
Problem p02881 (C++)

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

int main() {
  long N;
  cin >> N;
  long a = N, b = 0;
  for (long i = 1; i <= N / 2; i++) {
    if (N % i == 0) {
      b = (i - 1) + (N / i - 1);
      a = min(a, b);
    }
  }
  cout << a << endl;
}

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

int main() {
  long N;
  cin >> N;
  long a = N, b = 0;
  for (long i = 1; i <= sqrt(N); i++) {
    if (N % i == 0) {
      b = (i - 1) + (N / i - 1);
      a = min(a, b);
    }
  }
  cout << a << endl;
}
Problem p02881 (C++)

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

using namespace std;

int main() {
  long long int num, n;
  int digits;
  vector<long long int> vec;
  cin >> n;
  digits = int(floor(log10(n) + 1));
  digits = (digits / 2);
  for (int i = 1; i <= (int(pow(10, digits)) * int(pow(10, 2))); i++) {
    if (n % i == 0 && vec.size() == 0) {
      vec.push_back(i);
      vec.push_back((n / i));
    } else if (n % i == 0 && vec.size() != 0) {
      vec.push_back(i);
      vec.push_back((n / i));
    }
  }
  sort(vec.begin(), vec.end());
  num = (vec.size() / 2);
  cout << (vec[num - 1] - 1) + (vec[num] - 1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int num, n;
  int digits;
  vector<long long int> vec;
  cin >> n;
  digits = int(floor(log10(n) + 1));
  digits = (digits / 2);
  for (int i = 1; i <= int(pow(10, (digits + 1))); i++) {
    if (n % i == 0 && vec.size() == 0) {
      vec.push_back(i);
      vec.push_back((n / i));
    } else if (n % i == 0 && vec.size() != 0) {
      vec.push_back(i);
      vec.push_back((n / i));
    }
  }
  sort(vec.begin(), vec.end());
  num = (vec.size() / 2);
  cout << (vec[num - 1] - 1) + (vec[num] - 1) << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout << s << endl
using namespace std;
typedef long long ll;
string Yes(bool b) {
  if (b) {
    return "Yes";
  } else {
    return "No";
  }
}
string YES(bool b) {
  if (b) {
    return "YES";
  } else {
    return "NO";
  }
}
//================================================

bool primejudge(const ll x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  for (int i = 3; i * i <= x; i += 2)
    if (x % i == 0)
      return false;
  return true;
}

int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;

int main() {
  ll n;
  cin >> n;
  if (primejudge(n)) {
    cout << n - 1 << endl;
    return 0;
  }
  int flag = 1;
  for (int i = sqrt(n); i > 0; i--) {
    if (n % i == 0) {
      flag = i;
      break;
    }
  }
  cout << flag + n / flag - 2 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout << s << endl
using namespace std;
typedef long long ll;
string Yes(bool b) {
  if (b) {
    return "Yes";
  } else {
    return "No";
  }
}
string YES(bool b) {
  if (b) {
    return "YES";
  } else {
    return "NO";
  }
}
//================================================

bool primejudge(const ll x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  for (int i = 3; i * i <= x; i += 2)
    if (x % i == 0)
      return false;
  return true;
}

int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;

int main() {
  ll n;
  cin >> n;
  int flag = 1;
  for (int i = sqrt(n); i > 0; i--) {
    if (n % i == 0) {
      flag = i;
      break;
    }
  }
  cout << flag + n / flag - 2 << endl;

  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N;
  long long int ans = MOD * MOD;
  for (int i = 1; i * i <= N; i++) {
    if (N % i)
      continue;
    ans = min(ans, i + N / i);
  }
  cout << ans - 2 << endl;
}


Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N;
  long long int ans = MOD * MOD;
  for (long long int i = 1; i * i <= N; i++) {
    if (N % i)
      continue;
    ans = min(ans, i + N / i);
  }
  cout << ans - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
long long MOD = 1e9 + 7;
using namespace std;

int main() {
  ll N;
  cin >> N;

  vector<ll> ya;

  ll ku = 0;
  if (N % 2 == 0) {
    ku = N / 2;
  } else {
    ku = (N + 1) / 2;
  }
  for (ll i = 1; i < ku + 1; i++) {
    if (N % i == 0) {
      ya.push_back(i);
    }
  }

  ll res = 1e12 + 1;

  for (const ll &e : ya) {
    res = min(res, e + (N / e) - 2);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
long long MOD = 1e9 + 7;
using namespace std;

int main() {
  ll N;
  cin >> N;

  vector<ll> ya;

  for (ll i = 1; i * i < N + 1; i++) {
    if (N % i == 0) {
      ya.push_back(i);
    }
  }

  ll res = 1e12 + 1;

  for (const ll &e : ya) {
    res = min(res, e + (N / e) - 2);
  }
  cout << res << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
// f
int main(void) {
  long long n;
  cin >> n;
  long long ans = n;
  for (long long i = 1; i * i <= n; i++) {
    for (long long j = i; i * j <= n; j++) {
      if (i * j == n) {
        ans = min(ans, i - 1 + j - 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
// f
int main(void) {
  long long n;
  cin >> n;
  long long ans = n;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans = min(ans, i - 1 + n / i - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifndef LOCAL
#pragma GCC optimize("O2")
#endif
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  long long int N;
  cin >> N;
  long long int score;
  score = 1e9;
  for (int i = (int)sqrt(N); i <= N; i++) {
    if (N % i == 0) {
      // cout << score << "\n";
      score = min(i + N / i - 2, score);
    }
  }

  cout << score;
}

Fixed submission:
#ifndef LOCAL
#pragma GCC optimize("O2")
#endif
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  long long int N;
  cin >> N;
  long long int score;
  score = __LONG_LONG_MAX__;
  for (int i = 1; i <= (int)sqrt(N) + 1; i++) {
    if (N % i == 0) {
      // cout << score << "\n";
      score = min(i + N / i - 2, score);
    }
  }

  cout << score;
}
Problem p02881 (C++)

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
int main(int argc, char const *argv[]) {
  ll n;
  ll ans;
  scanf("%lld", &n);
  ans = n;
  for (register int i = 1; i * i <= n; i++)
    if (n % i == 0)
      ans = min(ans, i + n / i - 2);
  printf("%lld\n", ans);
  return 0;
}

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
int main(int argc, char const *argv[]) {
  ll n;
  ll ans;
  scanf("%lld", &n);
  ans = n;
  for (register ll i = 1; i * i <= n; i++)
    if (n % i == 0)
      ans = min(ans, i + n / i - 2);
  printf("%lld\n", ans);
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
using namespace std;
void fastio() {
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int p(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}
signed main() {
  fastio();
  int n, c = 0, a;
  cin >> n;
  int x = sqrt(n);
  if (p(n) == 1)
    cout << n - 1;
  else {
    for (int i = x; i <= x + 100000; i++) {
      if (n % i == 0) {
        a = i;
        break;
      }
    }
    int zz = n / a;
    cout << (a + zz) - 2;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
using namespace std;
void fastio() {
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int p(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}
signed main() {
  fastio();
  int n, c = 0, a;
  cin >> n;
  int x = sqrt(n);
  if (p(n) == 1)
    cout << n - 1;
  else {
    for (int i = x; i >= 1; i--) {
      if (n % i == 0) {
        a = i;
        break;
      }
    }
    int zz = n / a;
    cout << (a + zz) - 2;
  }
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
  long long int n;
  cin >> n;
  long long int a;
  long long int c;
  for (long long int i = 0; i < 1000000; i++) {
    if (n <= i * i) {
      a = i;
      break;
    }
  }

  long long int b;
  for (long long int i = a; i > 0; i--) {
    if (n % i == 0) {
      b = i;
      break;
    }
  }
  c = n / b;
  cout << c + b - 2;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
  long long int n;
  cin >> n;
  long long int a;
  long long int c;
  for (long long int i = 0; i < 1000001; i++) {
    if (n <= i * i) {
      a = i;
      break;
    }
  }

  long long int b;
  for (long long int i = a; i > 0; i--) {
    if (n % i == 0) {
      b = i;
      break;
    }
  }
  c = n / b;
  cout << c + b - 2;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define inf INT_MAX
#define sc(a) scanf("%d", &a)
#define scc(a, b) scanf("%d %d", &a, &b)
#define sccc(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pf(a) printf("%d", a)
#define pff(a, b) printf("%d %d", a, b)
#define pfff(a, b, c) printf("%d %d %d", a, b, c)
#define pb(a) push_back(a)
#define em empty()
#define fr front()
#define cl clear()
#define sz size()
#define mp(a, b) make_pair(a, b)
#define fri(b) freopen(b, "r", stdin)
#define fro(b) freopen(b, "w", stdout)
/*
ID: uslusam1
TASK: test
LANG: C++
*/
using namespace std;

int main() {
  // fri("a.gir");
  // fro("a.cik");
  long long n;
  cin >> n;
  long long x = (int)sqrt(n);
  while (n % x != 0) {
    x++;
  }
  long long y = n / x;
  cout << x + y - 2;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define inf INT_MAX
#define sc(a) scanf("%d", &a)
#define scc(a, b) scanf("%d %d", &a, &b)
#define sccc(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pf(a) printf("%d", a)
#define pff(a, b) printf("%d %d", a, b)
#define pfff(a, b, c) printf("%d %d %d", a, b, c)
#define pb(a) push_back(a)
#define em empty()
#define fr front()
#define cl clear()
#define sz size()
#define mp(a, b) make_pair(a, b)
#define fri(b) freopen(b, "r", stdin)
#define fro(b) freopen(b, "w", stdout)
/*
ID: uslusam1
TASK: test
LANG: C++
*/
using namespace std;

int main() {
  // fri("a.gir");
  // fro("a.cik");
  long long n;
  cin >> n;
  long long x = (int)sqrt(n);
  while (n % x != 0) {
    x--;
  }
  long long y = n / x;
  cout << x + y - 2;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
long long n, a, b, c = 1;
int main() {
  cin >> n;
  for (a = 2; a * a <= n && c; a++)
    c = (n % a != 0);
  if (!c) {
    c = n;
    for (a = 2; a * 2 <= n; a++) {
      if (n % a == 0) {
        b = n / a;
        if (a + b - 2 < c)
          c = a + b - 2;
      }
    }
    cout << c << '\n';
  } else
    cout << n - 1 << '\n';
}

Fixed submission:
#include <iostream>
using namespace std;
long long n, a, b, c = 1;
int main() {
  cin >> n;
  for (a = 2; a * a <= n && c; a++)
    c = (n % a != 0);
  if (!c) {
    c = n;
    for (a = 2; a * a <= n; a++) {
      if (n % a == 0) {
        b = n / a;
        if (a + b - 2 < c)
          c = a + b - 2;
      }
    }
    cout << c << '\n';
  } else
    cout << n - 1 << '\n';
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long

int n;
vector<int> v;

void check(int a, int b) {
  if (a * b == n)
    v.push_back(a + b - 2);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    check(i, n / i);
  }
  cout << *min_element(v.begin(), v.end());
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long

int n;
vector<int> v;

void check(int a, int b) {
  if (a * b == n)
    v.push_back(a + b - 2);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= sqrt(n); ++i) {
    check(i, n / i);
  }
  cout << *min_element(v.begin(), v.end());
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b[1000], c = 0, d;
  cin >> a;
  for (int i = 1; i <= 1000000; i++) {
    if (a % i == 0) {
      d = a / i;
      b[c] = d + i - 2;
      c++;
    }
  }
  sort(b, b + c);
  cout << b[0] << endl;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b[10000000], c = 0, d;
  cin >> a;
  for (int i = 1; i <= 1000000; i++) {
    if (a % i == 0) {
      d = a / i;
      b[c] = d + i - 2;
      c++;
    }
  }
  sort(b, b + c);
  cout << b[0] << endl;
  return (0);
}
Problem p02881 (C++)

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

vector<long long> division(long long n) {
  vector<long long> v;

  for (long long d = 1; d <= n / 2; d += 1)
    if (n % d == 0)
      v.push_back(d);
  v.push_back(n);
  return v;
}

int main() {
  long long n;
  cin >> n;
  vector<long long> v = division(n);
  vector<long long> m;
  if (v.size() == 2) {
    cout << n - 1;
    return 0;
  }
  for (long long i = 0; i < v.size(); i++)
    m.push_back(n / v[i] + v[i] - 2);
  cout << *min_element(m.begin(), m.end());
  return 0;
}

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

vector<long long> division(long long n) {
  vector<long long> v;

  for (long long d = 1; d * d <= n; d += 1)
    if (n % d == 0)
      v.push_back(d);
  v.push_back(n);
  return v;
}

int main() {
  long long n;
  cin >> n;
  vector<long long> v = division(n);
  vector<long long> m;
  if (v.size() == 2) {
    cout << n - 1;
    return 0;
  }
  for (long long i = 0; i < v.size(); i++)
    m.push_back(n / v[i] + v[i] - 2);
  cout << *min_element(m.begin(), m.end());
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  long long n;

  cin >> n;
  long long m = sqrt(n);
  long long i;
  for (i = m; i <= n; i++) {
    if (n % i == 0)
      break;
  }
  cout << i + n / i - 2;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  long long n;

  cin >> n;
  long long m = sqrt(n);
  long long i;
  for (i = m; i >= 1; i--) {
    if (n % i == 0)
      break;
  }
  cout << i + n / i - 2;
}
Problem p02881 (C++)

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

int main(void) {
  long int n;
  cin >> n;
  long int ans = n;

  for (int i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    long int a = (i - 1) + (n / i - 1);
    ans = min(ans, a);
  }
  cout << ans << endl;
}

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

int main(void) {
  long int n;
  cin >> n;
  long int ans = n;

  for (long int i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    long int a = (i - 1) + (n / i - 1);
    ans = min(ans, a);
  }
  cout << ans << endl;
}
Problem p02881 (C++)

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

int main() {
  int64_t n;
  cin >> n;
  int64_t j = 1;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      j = i;
    }
  }
  cout << j + (n / j) - 2 << endl;
}

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

int main() {
  int64_t n;
  cin >> n;
  int64_t j = 1;
  int z = sqrt(n);
  for (int i = 1; i < z + 1; i++) {
    if (n % i == 0) {
      j = i;
    }
  }
  cout << j + (n / j) - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdint.h>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int64_t A;
  cin >> A;

  vector<pair<int64_t, int64_t>> v;
  for (int64_t i = 1; i <= A / 2; i++) {
    if (A % i == 0) {
      v.push_back(pair<int64_t, int64_t>(i, A / i));
    }
  }

  int64_t min = 1e12;
  int64_t tmp = 0;
  for (int64_t j = 0; j < v.size(); j++) {
    tmp = v[j].first + v[j].second - 2;
    if (tmp < min)
      min = tmp;
  }
  cout << min << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdint.h>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int64_t A;
  cin >> A;

  vector<pair<int64_t, int64_t>> v;
  for (int64_t i = 1; i * i <= A; i++) {
    if (A % i == 0) {
      v.push_back(pair<int64_t, int64_t>(i, A / i));
    }
  }

  int64_t min = 1e12;
  int64_t tmp = 0;
  for (int64_t j = 0; j < v.size(); j++) {
    tmp = v[j].first + v[j].second - 2;
    if (tmp < min)
      min = tmp;
  }
  cout << min << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()
const long long INF = 1e15;
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll ans = INF;
  for (int i; i * i <= n; i++) {
    if (n % i == 0) {
      ll j = n / i;
      ans = min(ans, i + j - 2);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()
const long long INF = 1e15;
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll ans = INF;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ll j = n / i;
      ans = min(ans, i + j - 2);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
  cin >> n;
  for (int i = 1; i * i <= n; i++)
    if (n % i == 0)
      ans = i + n / i;
  cout << ans - 2;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
  cin >> n;
  for (int i = 1; 1ll * i * i <= n; i++)
    if (n % i == 0)
      ans = i + n / i;
  cout << ans - 2;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define P pair<int, int>
using namespace std;

int main() {
  ll n;
  cin >> n;
  ll ans = 1e15;
  for (int i = 1; i * i <= n; i++) {
    long long int d = n / i;
    if (d * i == n) {
      ans = min(ans, (i - 1) + (ll)(d - 1));
      // cout << ans << " " << i << " " << d << endl;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define P pair<int, int>
using namespace std;

int main() {
  ll n;
  cin >> n;
  ll ans = 1e15;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans = min(ans, (i - 1) + (ll)(n / i - 1));
      // cout << ans << " " << i << " " << d << endl;
    }
  }
  cout << ans << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
  vector<ll> mass_i;
  vector<ll> mass_j;
  ll N, best_score;
  cin >> N;
  for (ll i = 1; i <= N; i++) {
    if (N % i == 0) {
      mass_i.push_back(i);
      mass_j.push_back(N / i);
    }
  }
  for (ll i = 0; i < mass_i.size(); i++) {
    if (best_score > mass_i[i] + mass_j[i] - 2 || best_score == 0) {
      best_score = mass_i[i] + mass_j[i] - 2;
    }
  }
  cout << best_score;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
  vector<ll> mass_i;
  vector<ll> mass_j;
  ll N, best_score;
  cin >> N;
  for (ll i = 1; i <= N; i++) {
    if (i > N / i)
      break;
    if (N % i == 0) {
      mass_i.push_back(i);
      mass_j.push_back(N / i);
    }
  }
  for (ll i = 0; i < mass_i.size(); i++) {
    if (best_score > mass_i[i] + mass_j[i] - 2 || best_score == 0) {
      best_score = mass_i[i] + mass_j[i] - 2;
    }
  }
  cout << best_score;
}
Problem p02881 (C++)

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

int main() {
  int64_t N;
  cin >> N;
  int j = 1;
  for (int i = 1; i * i <= N; i++)
    if (N % i == 0)
      j = i;
  cout << j + N / j - 2 << endl;
}

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

int main() {
  int64_t N;
  cin >> N;
  int j = 1;
  for (int i = 1; i <= N / i; i++)
    if (N % i == 0)
      j = i;
  cout << j + N / j - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pair;
const int inf = 1000000000;          // 10^9
const ll inff = 1000000000000000000; // 10^18

int main() {
  ll n;
  cin >> n;
  ll ans = 1e18;
  for (ll i = 0; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll j = n / i;
    ans = min(ans, i + j - 2);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pair;
const int inf = 1000000000;          // 10^9
const ll inff = 1000000000000000000; // 10^18

int main() {
  ll n;
  cin >> n;
  ll ans = 1e18;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll j = n / i;
    ans = min(ans, i + j - 2);
  }
  cout << ans << endl;

  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<

int main() {
  ll n;
  cin >> n;
  ll mi = INF;
  for (ll i = 1; i <= n; i++) {
    if (n % i == 0) {
      mi = min(mi, (n / i) + i);
    }
  }
  cout << mi - 2 << endl;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<

int main() {
  ll n;
  cin >> n;
  ll mi = 9223372036854775807;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      mi = min(mi, (n / i) + i);
    }
  }
  cout << mi - 2 << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll ans = 1ll * 10000 * 10000 * 10000;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans = min(ans, n / i + i - 2);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll ans = 1ll * 10000 * 10000 * 10000;
  for (int i = 1; 1ll * i * i <= n; i++) {
    if (n % i == 0) {
      ans = min(ans, n / i + i - 2);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    Zeolim - An AC a day keeps the bug away
*/

// #pragma GCC optimize(2)
// #pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for (int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
void re(ll &x) {
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9')
    s = getchar();
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
}
void wr(ll x) {
  if (x > 9)
    wr(x / 10);
  putchar(x % 10 + '0');
}
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 386910137;
const ull P = 13331;
const int MAXN = 1e6 + 100;

int main() {
  // ios::sync_with_stdio(0);
  // cin.tie(0); cout.tie(0);
  // freopen("d:\out.txt","w",stdout);
  // freopen("d:\in.txt","r",stdin);

  ll n, m;

  cin >> n;

  ll ans = INF;

  for (int i = 1; i * i <= n; ++i) {
    ll ry = n / i;
    if (ry * i == n) {
      ans = min((i - 1 + ry - 1), ans);
    }
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
/*
    Zeolim - An AC a day keeps the bug away
*/

// #pragma GCC optimize(2)
// #pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for (int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
void re(ll &x) {
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9')
    s = getchar();
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
}
void wr(ll x) {
  if (x > 9)
    wr(x / 10);
  putchar(x % 10 + '0');
}
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 386910137;
const ull P = 13331;
const int MAXN = 1e6 + 100;

int main() {
  // ios::sync_with_stdio(0);
  // cin.tie(0); cout.tie(0);
  // freopen("d:\out.txt","w",stdout);
  // freopen("d:\in.txt","r",stdin);

  ll n, m;

  cin >> n;

  ll ans = INF;

  for (ll i = 1; i * i <= n; ++i) {
    ll ry = n / i;
    if (ry * i == n) {
      ans = min((i - 1 + ry - 1), ans);
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long

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

  ll ans = 1e18;
  for (int i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll j = n / i;
    ans = min(ans, i + j - 2);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long

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

  ll ans = 1e18;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll j = n / i;
    ans = min(ans, i + j - 2);
  }
  cout << ans << endl;
  return 0;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f(i, x, n) for (ll int i = x; i < n; i++)
#define re(j, x, n) for (ll int j = x; j >= n; j--)
#define f1(k, x, n) for (ll int k = x; k <= n; k++)
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define MAX 1000001
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int mn = n - 1, f = 0;
  f1(i, 2, sqrt(n)) {
    if (n % i == 0) {
      f = 1;
      break;
    }
  }
  if (f == 0) {
    cout << n - 1;
    return 0;
  }
  f1(i, 2, n / 2) {
    if (n % i == 0) {
      if (mn < (i - 1) + ((n / i) - 1))
        break;
      mn = min(mn, (i - 1) + ((n / i) - 1));
    }
  }
  cout << mn;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f(i, x, n) for (ll int i = x; i < n; i++)
#define re(j, x, n) for (ll int j = x; j >= n; j--)
#define f1(k, x, n) for (ll int k = x; k <= n; k++)
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define MAX 1000001
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int mn = n - 1, f = 0;
  f1(i, 2, sqrt(n)) {
    if (n % i == 0) {
      f = 1;
      break;
    }
  }
  if (f == 0) {
    cout << n - 1;
    return 0;
  }
  f1(i, 2, sqrt(n)) {
    if (n % i == 0) {
      if (mn < (i - 1) + ((n / i) - 1))
        break;
      mn = min(mn, (i - 1) + ((n / i) - 1));
    }
  }
  cout << mn;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  ll x = 1, y;
  for (int i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      x = i;
      y = N / i;
    }
  }
  cout << (x - 1) + (y - 1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  ll x = 1, y;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      x = i;
      y = N / i;
    }
  }
  cout << (x - 1) + (y - 1) << endl;
}
Problem p02881 (C++)

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

using namespace std;

int main() {

  long long int n, i, min = 10000000000000000;
  scanf("%lld", &n);

  for (i = 1; i <= n / 2; ++i) {
    if (n % i == 0) {
      if (min > (i - 1 + n / i - 1))
        min = (i - 1 + n / i - 1);
    }
  }

  printf("%ld", min);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {

  long long int n, i, min = 10000000000000000;
  scanf("%lld", &n);

  for (i = 1; i <= sqrt(n); ++i) {
    if (n % i == 0) {
      if (min > (i - 1 + n / i - 1))
        min = (i - 1 + n / i - 1);
    }
  }

  printf("%ld", min);
}
Problem p02881 (C++)

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

int main() {
  int64_t N, a = 10000000000000;
  cin >> N;
  for (int i = 1; i * i < N; i++) {
    if (N % i == 0) {
      a = min(a, N / i + i);
    }
  }
  cout << a - 2 << endl;
}

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

int main() {
  int64_t N, a = 10000000000000;
  cin >> N;
  for (int64_t i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      a = min(a, N / i + i);
    }
  }
  cout << a - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  long long minimum = N - 1;
  for (long long i = 1; i < N / 2; i++) {
    if (N % i == 0) {
      minimum = min(minimum, N / i + i - 2);
    }
  }
  cout << minimum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  long long minimum = N - 1;
  for (long long i = 1; i <= sqrt(N); i++) {
    if (N % i == 0) {
      minimum = min(minimum, N / i + i - 2);
    }
  }
  cout << minimum << endl;
}
Problem p02881 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;

int main() {
  long long N;
  cin >> N;
  vector<long long> factor(1000000, 1000000000003);
  for (long long i = 1ll; i * i <= N; ++i) {
    if (N % i == 0) {
      factor.at(i) = i + N / i;
    }
  }
  sort(factor.begin(), factor.end());
  cout << factor.at(0) - 2 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;

int main() {
  long long N;
  cin >> N;
  vector<long long> factor(1000001, 1000000000003);
  for (long long i = 1ll; i * i <= N; ++i) {
    if (N % i == 0) {
      factor.at(i) = i + N / i;
    }
  }
  sort(factor.begin(), factor.end());
  cout << factor.at(0) - 2 << endl;
}
Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  unsigned long long n, minimum;
  cin >> n;
  minimum = n - 1;

  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      // if (((n / i) - 1) + (i - 1) < minimum)
      //     minimum = ((n / i) - 1) + (i - 1);
      minimum = min(((n / i) - 1) + (i - 1), minimum);
    }
  }
  cout << minimum;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  unsigned long long n, minimum;
  cin >> n;
  minimum = n - 1;

  for (unsigned long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      // if (((n / i) - 1) + (i - 1) < minimum)
      //     minimum = ((n / i) - 1) + (i - 1);
      minimum = min(((n / i) - 1) + (i - 1), minimum);
    }
  }
  cout << minimum;

  return 0;
}