alexjercan/bugnet huggingface dataset:
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

long long solve() {
  long long x, k, d;
  std::cin >> x >> k >> d;

  /* 最小値になるのに必要な回数 */
  long long need_num = std::abs(x) / d;

  long long k_new = std::min(k, need_num);

  /* 減らす回数は偶数回 */
  if (k_new != k && (k - k_new) % 2 == 1)
    ++k_new;

  long long ans = std::abs(std::abs(x) - k_new * d);

  return ans;
}

int main() {
  std::cout << solve() << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

long long solve() {
  long long x, k, d;
  std::cin >> x >> k >> d;

  /* 最小値を求めるのに必要な移動回数を計算 */
  long long k_new = std::min(k, (std::abs(x) + d - 1) / d);

  /* 減らす回数は偶数回 */
  if (k_new != k && (k - k_new) % 2 != 0)
    ++k_new;

  long long ans = std::abs(x);

  /* k回繰り返す */
  while (k_new--) {
    if (x >= 0) {
      ans = std::abs(x - d);
      x -= d;
    } else if (x <= 0) {
      ans = std::abs(x + d);
      x += d;
    }
  }
  return ans;
}

int main() {
  std::cout << solve() << std::endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ABS(ll a) {
  if (a < 0)
    return -a;
  else
    return a;
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  ll i = min(ABS(X) / D, K);
  X = ABS(X) - D * i;
  for (; i < K;) {
    if (ABS(X + D) > ABS(X - D)) {
      X = X - D;
    } else
      X = X + D;

    i++;
    if (ABS(X) <= D) {
      if ((K - i) % 2 == 0)
        break;
      else {
        if (ABS(X + D) > ABS(X - D)) {
          X = X - D;
        } else
          X = X + D;
        break;
      }
    }
  }
  cout << ABS(X) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ABS(ll a) {
  if (a < 0)
    return -a;
  else
    return a;
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  for (ll i = 0; i < K;) {
    if (ABS(X + D) > ABS(X - D)) {
      X = X - D;
    } else
      X = X + D;

    i++;
    if (ABS(X) <= D) {
      if ((K - i) % 2 == 0)
        break;
      else {
        if (ABS(X + D) > ABS(X - D)) {
          X = X - D;
        } else
          X = X + D;
        break;
      }
    }
  }
  cout << ABS(X) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int MAX_N = 1e5 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll flag = 0;
  if (x / d > k) {
    flag = x - d * k;
  } else {
    int y = x / d;
    k = k - y;
    if (k % 2 == 0)
      flag = x - y * d;
    else {
      flag = min((x - (y * d) + d), (x - (y * d) - d));
    }
  }

  cout << abs(flag) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int MAX_N = 1e5 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll x, k, d;
  cin >> x >> k >> d;
  ll m, n;
  while (k--) {
    m = x - d;
    n = x + d;
    if (abs(m) > abs(n)) {
      x = x + d;
    } else {
      x = x - d;
    }
  }
  cout << abs(x) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

// Radhe Radhe
#include <bits/stdc++.h>
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define ll long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define en '\\n'
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, a, b) for (ll i = a; i <= b; i++)
#define repVect(i, n) for (ll i = 0; i < sum.size(); i++)
#define fixed cout.setf(ios::fixed);
#define Precise cout.precision(1);
#define yes cout << "YES" << '\\n'
#define no cout << "NO" << '\\n'
using namespace std;

int main() {
  godspeed
      //  ll  t,n,i,j;
      ll i,
      t, n, j, k, d, x, l;
  cin >> x >> k >> d;
  x = abs(x);
  ll mov = min(x / d, k);
  k -= mov;
  x -= mov * d;
  if (k % 2)
    x = d - x;
  cout << x << endl;

  return 0;
}


Error: 0

Fixed submission:

// Radhe Radhe
#include <bits/stdc++.h>
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define ll long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define en '\\n'
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, a, b) for (ll i = a; i <= b; i++)
#define repVect(i, n) for (ll i = 0; i < sum.size(); i++)
#define fixed cout.setf(ios::fixed);
#define Precise cout.precision(1);
#define yes cout << "YES" << '\\n'
#define no cout << "NO" << '\\n'
using namespace std;

int main() {
  godspeed ll t, n, i, j, k, d, x, l, pos, neg;
  cin >> x >> k >> d;
  neg = x;
  if (k > 2) {
    if (k % 2 == 0)
      k = 2;
    else
      k = 1;
  }
  vector<ll> v;
  while (k--) {
    neg -= d;
    if (abs(neg) != 0)
      v.pb(abs(neg));
  }

  cout << *min_element(v.begin(), v.end()) << en;

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void) {
  lli X, K, D;
  cin >> X >> K >> D;
  X = abs(X);
  lli tmpX = X;
  lli tmpK = K;
  K -= (lli)(X / D);
  X -= (lli)(X / D) * D;
  if (K == 0) {
    cout << X << endl;
    return 0;
  } else if (K < 0) {
    cout << tmpX - tmpK * D << endl;
    return 0;
  } else {
    if (K % 2 == 0) {
      cout << X << endl;
    } else {
      cout << abs(X - D) << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void) {
  lli X, K, D;
  cin >> X >> K >> D;
  lli now = X;
  lli tmp = 0;
  if (X == D && K % 2 == 0) {
    cout << X << endl;
    return 0;
  }
  /*for (lli i = 1; i <= K; i++)
  {
      tmp = now;
      now = min(abs(now - D), abs(now + D));
      if (tmp == now && (K - i) % 2 == 0)
      {
          break;
      }
      else if (tmp == now && (K - i) % 2 == 1)
      {
          now = min(abs(now - D), abs(now + D));
          break;
      }
  }*/
  for (lli i = 1; i <= (lli)(K / X); i++) {
    tmp = now;
    now = min(abs(now - D * i), abs(now + D * i));
  }
  cout << now << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long x, k, d;
int main() {
  cin >> k >> x >> d;
  k = abs(k);
  if (x * d > 0 && x * d <= k) {
    cout << k - x * d << endl;
    return 0;
  }
  if ((x - k / d) % 2 == 0) {
    cout << k % d << endl;
  } else {
    cout << d - k % d << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long x, k, d;
int main() {
  cin >> k >> x >> d;
  k = abs(k);
  if (x * d > 0 && x * d < k) {
    cout << k - x * d << endl;
    return 0;
  }
  if ((x - k / d) % k == 0) {
    cout << k % d << endl;
  } else {
    cout << d - k % d << endl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll ATCODER_AMARI = 1000000007; // 10^9 + 7
const ll INF = 10000000000000;

// 階乗関数　ATCODER_AMARIに注意
ll factorial(ll k) {
  ll sum = 1;
  for (int i = 1; i < k + 1; i++) {
    sum *= i;
    // sum = sum % ATCODER_AMARI;
  }
  return sum;
}

int main() {
  ll x, k, d;
  cin >> x >> k >> d;

  x = abs(x);

  // ll count = 0;
  // ll com1,com2;
  //
  // ll com1_co,com2_co;

  ll x_sho = x / d;
  ll x_amari = x % d;
  ll x_ir = abs(x_amari - d);

  if (x_sho >= k) {
    cout << x - (k * d) << endl;
    return 0;
  }

  k = k % 2;
  x_sho = x_sho % 2;

  if (x_sho == k)
    cout << x_amari << endl;
  else
    cout << x_ir << endl;
  ;

  // rep(i,k) {
  //   com1 = x;
  //   x -= d;
  //
  //   com1_co = count;
  //
  //   if(x <= 0) {
  //     com2 = x;
  //     com2_co = count+ 1;
  //     break;
  //   }
  //
  //   if( i == k - 1) {
  //     cout << x << endl;
  //     return 0;
  //   }
  //
  //   count++;
  // }
  //
  // com1_co %= 2;
  // com2_co %= 2;
  // k %= 2;
  //
  // if(com1_co == k) {
  //   cout << abs(com1) << endl;
  // }
  // else {
  //   cout << abs(com2) << endl;
  // }
}


Error: TLE

Fixed submission:

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

using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll ATCODER_AMARI = 1000000007; // 10^9 + 7
const ll INF = 10000000000000;

// 階乗関数　ATCODER_AMARIに注意
ll factorial(ll k) {
  ll sum = 1;
  for (int i = 1; i < k + 1; i++) {
    sum *= i;
    // sum = sum % ATCODER_AMARI;
  }
  return sum;
}

int main() {
  ll x, k, d;
  cin >> x >> k >> d;

  x = abs(x);

  ll count = 0;
  ll com1, com2;

  ll com1_co, com2_co;

  rep(i, k) {
    com1 = x;
    x -= d;

    com1_co = count;

    if (x <= 0) {
      com2 = x;
      com2_co = count + 1;
      break;
    }

    if (i == k - 1) {
      cout << x << endl;
      return 0;
    }

    count++;
  }

  com1_co %= 2;
  com2_co %= 2;
  k %= 2;

  if (com1_co == k) {
    cout << abs(com1) << endl;
  } else {
    cout << abs(com2) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
const long long INF = numeric_limits<long long>::max();
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
vector<ll> getNums(ll N) {
  vector<ll> num(N);
  REP(i, N) { cin >> num[i]; }
  return num;
}
int main() {
  ll X, K, D;
  cin >> X >> K >> D;

  ll ans = INF;

  ll begin = max(min(K / 2, abs(X) / D) - 1000000000, (ll)0);
  if (K % 2 == 0) {
    if (X >= 0) {
      for (ll i = begin; i <= K / 2; ++i) {
        ll tmp = X - D * 2 * i;
        chmin(ans, abs(tmp));
        if (tmp < 0)
          break;
      }
    }

    if (X <= 0) {
      for (ll i = begin; i <= K / 2; ++i) {
        ll tmp = X + D * 2 * i;
        chmin(ans, abs(tmp));
        if (tmp > 0)
          break;
      }
    }
  } else {
    if (X >= 0) {
      for (ll i = begin; i <= (K) / 2; ++i) {
        ll tmp = X - D * (i * 2 + 1);
        chmin(ans, abs(tmp));
        if (tmp < 0)
          break;
      }
    }

    if (X <= 0) {
      for (ll i = begin; i <= (K) / 2; ++i) {
        ll tmp = X + D * (i * 2 + 1);
        chmin(ans, abs(tmp));
        if (tmp > 0)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
const long long INF = numeric_limits<long long>::max();
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
vector<ll> getNums(ll N) {
  vector<ll> num(N);
  REP(i, N) { cin >> num[i]; }
  return num;
}
int main() {
  ll X, K, D;
  cin >> X >> K >> D;

  ll ans = INF;

  ll begin = max(min(K / 2, X / D - 1), (ll)0);
  if (K % 2 == 0) {
    if (X >= 0) {
      for (ll i = begin; i <= K / 2; ++i) {
        ll tmp = X - D * 2 * i;
        chmin(ans, abs(tmp));
        if (tmp < 0)
          break;
      }
    } else {
      for (ll i = begin; i <= K / 2; ++i) {
        ll tmp = X + D * 2 * i;
        chmin(ans, abs(tmp));
        if (tmp > 0)
          break;
      }
    }
  } else {
    if (X >= 0) {
      for (ll i = begin; i <= (K) / 2; ++i) {
        ll tmp = X - D * (i * 2 + 1);
        chmin(ans, abs(tmp));
        if (tmp < 0)
          break;
      }
    } else {
      for (ll i = begin; i <= (K) / 2; ++i) {
        ll tmp = X + D * (i * 2 + 1);
        chmin(ans, abs(tmp));
        if (tmp > 0)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  //
  long long x, k, d;

  cin >> x >> k >> d;
  x = abs(x);
  long long tmp;

  if (x / d > k) {
    cout << x - (k * d);
  } else {
    tmp = (x / d);
    k -= tmp;
    x -= tmp * d;
    if (k % 2 == 0)
      cout << abs(x);
    else
      cout << abs(x - d);
  }
}


Error: WA

Fixed submission:

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

int main() {
  //
  long long x, k, d;

  cin >> x >> k >> d;
  x = abs(x);
  long long tmp = x;

  if (x > k * d) {
    cout << x - (k * d);
  } else {
    // k = x / k;
    // x = x % d;
    cout << (k * d) % x;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;

int main() {
  long long X;
  long long K;
  long long D;
  cin >> X;
  cin >> K;
  cin >> D;

  long long x = abs(X);
  long long k = K;

  long long t = (x / D);
  if (t > k) {
    t = k;
  }
  x -= D * t;
  k -= t;

  if (k % 2 == 1) {
    x -= D;
  }
  cout << abs(x) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;

int main() {
  long long X;
  long long K;
  long long D;
  cin >> X;
  cin >> K;
  cin >> D;

  long long x = X;
  long long k = K;

  if (x > 0) {
    int t = (x / D);
    if (t > k) {
      t = k;
    }
    x -= D * t;
    k -= t;
  } else {
    int t = (abs(x) / D);
    if (t > k) {
      t = k;
    }
    x += D * t;
    k -= t;
  }

  while (k > 0) {
    if (abs(x) > D) {
      if (x > 0) {
        x -= D;
      } else {
        x += D;
      }
      --k;
    } else {
      if (k % 2 == 1) {
        if (x > 0) {
          x -= D;
        } else {
          x += D;
        }
      }
      break;
    }
  }
  cout << abs(x) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
#define mod 1000000007
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1, 0, -1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll ans;
  if (x < 0)
    x = -x;
  ll num = min(k, x / d);
  x -= d * num;
  k -= num;
  if (k == 0) {
    cout << x << endl;
    return 0;
  }
  if (k % 2 == 0)
    ans = x;
  else
    ans = abs(x - d);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
#define mod 1000000007
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1, 0, -1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll ans = LINF;
  if (x > 0) {
    while (x > 0 && k > 0) {
      x -= d;
      k--;
    }
    if (k == 0) {
      ans = abs(x);
      cout << ans << endl;
      return 0;
    }
    ll x1 = x;
    if (k % 2 == 0)
      ans = x1;
    else
      ans = x1 + d;
    ans = abs(ans);
  } else {
    while (x < 0 && k > 0) {
      x += d;
      k--;
    }
    if (k == 0) {
      ans = abs(x);
      cout << ans << endl;
      return 0;
    }
    ll x2 = x;
    if (k % 2 == 0)
      ans = x2;
    else
      ans = x2 - d;
    ans = abs(ans);
  }
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll cnt = abs(x) / d;
  if (k <= cnt)
    cout << abs(x) - d * k << endl;
  else {
    ll now = abs(x) - d * cnt;
    if ((k - cnt) & 1)
      cout << abs(now - d) << endl;
    else
      cout << now << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x == 0 && k % 2 == 0)
    cout << 0 << endl;
  else if (x == 0 && k % 2 == 1)
    cout << d << endl;
  else {
    if (x < 0) {
      while (abs(x) >= d && k != 0) {
        x += d;
        k--;
      }
      if (k == 0)
        cout << abs(x) << endl;
      else {
        if (abs(x) > abs(x + d)) {
          x += d;
          k--;
        }
        if (k % 2 == 0)
          cout << abs(x) << endl;
        else {
          cout << min(abs(x + d), abs(x - d));
        }
      }
    } else if (x > 0) {
      while (abs(x) >= d && k != 0) {
        x -= d;
        k--;
      }
      if (k == 0)
        cout << abs(x) << endl;
      else {
        if (abs(x) > abs(x - d)) {
          x -= d;
          k--;
        }
        if (k % 2 == 0)
          cout << abs(x) << endl;
        else {
          cout << min(abs(x + d), abs(x - d));
        }
      }
    }
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll ans;
  if (x / d >= k) {
    ans = x - d * k;
  } else {
    ll e = x / d;
    k -= e;
    x -= e * d;
    if (k % 2 == 1)
      x = abs(x - d);
    ans = x;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  rep(i, k) {
    ll ex1 = abs(x - d);
    ll ex2 = abs(x + d);
    x = min(ex1, ex2);
  }
  cout << x << endl;
  return 0;
}