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

/*
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\\  =  /0
                    ___/`---'\\___
                  .' \\\\|     |// '.
                 / \\\\|||  :  |||// \\
                / _||||| -:- |||||- \\
               |   | \\\\\\  -  /// |   |
               | \\_|  ''\\---/''  |_/ |
               \\  .-\\__  '-'  ___/-. /
             ___'. .'  /--.--\\  `. .'___
          ."" '<  `.___\\_<|>_/___.' >' "".
         | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
         \\  \\ `_.   \\_ __\\ /__ _/   .-` /  /
     =====`-.____`.___ \\_____/___.-`___.-'=====
                       `=---='

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>

#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define pi 3.14159265358979323846
#define count_1(n) __builtin_popcountll(n)
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define mid(s, e) (s + (e - s) / 2)
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
#define print_upto(a, n)                                                       \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl

inline void solve() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll t = x / d;
  ll ans = 0;
  if (k <= t) {
    ans = x - k * d;
  } else {
    if (t % 2 == k % 2)
      ans = x - t * d;
    else
      ans = min(x - (t - 1) * d, abs(x - (t + 1) * d));
  }
  cout << ans << endl;
}

int main() {
  cout << fixed << setprecision(12);
  fastio;
  ll t = 1, tc = 1;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
#endif
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\\  =  /0
                    ___/`---'\\___
                  .' \\\\|     |// '.
                 / \\\\|||  :  |||// \\
                / _||||| -:- |||||- \\
               |   | \\\\\\  -  /// |   |
               | \\_|  ''\\---/''  |_/ |
               \\  .-\\__  '-'  ___/-. /
             ___'. .'  /--.--\\  `. .'___
          ."" '<  `.___\\_<|>_/___.' >' "".
         | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
         \\  \\ `_.   \\_ __\\ /__ _/   .-` /  /
     =====`-.____`.___ \\_____/___.-`___.-'=====
                       `=---='

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>

#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define pi 3.14159265358979323846
#define count_1(n) __builtin_popcountll(n)
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define mid(s, e) (s + (e - s) / 2)
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
#define print_upto(a, n)                                                       \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl

inline void solve() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  int t = x / d;
  if (k >= t) {
    x = x - (t * d);
    k = k - t;
  } else {
    cout << x - k * d << "\\n";
    return;
  }
  while (k--) {
    ll a = abs(x - d);
    ll b = abs(x + d);
    x = min(a, b);
  }
  cout << (ll)abs(x) << "\\n";
}

int main() {
  cout << fixed << setprecision(12);
  fastio;
  ll t = 1, tc = 1;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
#endif
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}