Problem p02696 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
auth - @sportykush
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// #define mp make_pair
#define bug(v) cerr << #v << " = " << (v) << endl;
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue<int>
#define khtm "\n"
#define F first
#define S second
#define fr(i, a, n) for (i = a; i < n; i++)
#define mem(arr, l) memset(arr, l, sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i, a, n) for (i = n - 1; i >= a; i--)
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define testcase                                                               \
  ll T;                                                                        \
  cin >> T;                                                                    \
  while (T--)

const int MAX = 1e6 + 2;
const ll inf = 1e18;
const int N = 1010;
// const ll mod = 998244353 ;
const ll mod = 1e9 + 7;
inline ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
inline ll mul(ll a, ll b) { return ((a % mod) * 1ll * b) % mod; }
inline ll power(ll a, ll b) {
  ll rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}

/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i, j, k, l, m, r, n;
ll a[MAX], b[MAX];

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // cout << setprecision(15) << fixed;
  fast

          cin >>
      l >> r >> n;
  ll ans = 0;
  frr(i, sqrt(n), n + 1) {
    ll tmp1 = (l * i) / r;
    ll tmp2 = i / r;
    k = (tmp1 - l * tmp2);
    ans = max(k, ans);
    // bug(k)
  }
  cout << ans;

#ifndef ONLINE_JUDGE
  cout << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}

Fixed submission:
/*
auth - @sportykush
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// #define mp make_pair
#define bug(v) cerr << #v << " = " << (v) << endl;
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue<int>
#define khtm "\n"
#define F first
#define S second
#define fr(i, a, n) for (i = a; i < n; i++)
#define mem(arr, l) memset(arr, l, sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i, a, n) for (i = n - 1; i >= a; i--)
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define testcase                                                               \
  ll T;                                                                        \
  cin >> T;                                                                    \
  while (T--)

const int MAX = 1e6 + 2;
const ll inf = 1e18;
const int N = 1010;
// const ll mod = 998244353 ;
const ll mod = 1e9 + 7;
inline ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
inline ll mul(ll a, ll b) { return ((a % mod) * 1ll * b) % mod; }
inline ll power(ll a, ll b) {
  ll rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}

/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i, j, k, l, m, r, n;
ll a[MAX], b[MAX];

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // cout << setprecision(15) << fixed;
  fast

          cin >>
      l >> r >> n;
  ll ans = 0;

  if (r > n) {
    ll tmp1 = (l * n) / r;
    ll tmp2 = n / r;
    ans = (tmp1 - l * tmp2);
  } else {

    n = r - 1;
    ll tmp1 = (l * n) / r;
    ll tmp2 = n / r;
    ans = (tmp1 - l * tmp2);
  }
  cout << ans;

#ifndef ONLINE_JUDGE
  cout << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}