Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

// while( !(succeed == try()) );

#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;

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

#define int long long int
#define loop(i, s, e) for (int i = s; i < e; i++)
#define endl '\\n'
#define vii vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define N 100001
#define pb push_back
#define INF LLONG_MAX
#define inf INT_MAX

void INP();

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

void alpha() {
  int x, k, d;
  cin >> x >> k >> d;

  x = abs(x);

  int t = k;
  k = max(0LL, k - (x / d));

  if (k > 0)
    x = x % d;
  else
    x -= t * d;

  // bug(x, k);

  int t1 = x, t2 = abs(x - d);

  if (t2 <= t1 and k > 0) {
    if ((k - 1) % 2 == 0) {
      cout << t2 << endl;
      return;
    } else {
      cout << t1 << endl;
      return;
    }
  } else {
    if (k % 2 == 0) {
      cout << t1 << endl;
      return;
    } else {
      cout << t2 << endl;
      return;
    }
  }
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // INP();
  int t = 1;
  // cin>>t;

  while (t--)
    alpha();

  return 0;
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}


Error: TLE

Fixed submission:

// while( !(succeed == try()) );

#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;

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

#define int long long int
#define loop(i, s, e) for (int i = s; i < e; i++)
#define endl '\\n'
#define vii vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define N 100001
#define pb push_back
#define INF LLONG_MAX
#define inf INT_MAX

void INP();

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

void alpha() {
  int x, k, d;
  cin >> x >> k >> d;

  x = abs(x);

  while (k > 0 and x - d > 0) {
    x = x - d;
    k--;
  }

  int ans;
  int t1 = x, t2 = abs(x - d);

  if (t2 <= t1 and k > 0) {
    if ((k - 1) % 2 == 0) {
      cout << t2 << endl;
      return;
    } else {
      cout << t1 << endl;
      return;
    }
  } else {
    if (k % 2 == 0) {
      cout << t1 << endl;
      return;
    } else {
      cout << t2 << endl;
      return;
    }
  }
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // INP();
  int t = 1;
  // cin>>t;

  while (t--)
    alpha();

  return 0;
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}