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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
// constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

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

signed main() {
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#define INF 100000000000
#define N 10000000
#define ll long long
using namespace std;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x *= -1;
  ll p = x;
  ll count1 = 0;
  while (p > 0 && count1 < k) {
    p -= d;
    count1++;
  }

  if (p > 0)
    cout << p << endl;
  else {
    ll q = k - count1;
    q = q % 2;
    for (ll i = 0; i < q; i++) {
      if (p > 0)
        p -= d;
      else
        p += d;
    }
    if (p > 0)
      cout << p << endl;
    else
      cout << p * -1 << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long a, d, c;
  cin >> a >> d >> c;
  a = abs(a);
  long long step = min(d, a / c);
  d -= step;
  a -= step * c;
  if (d % 2 == 0)
    cout << a << endl;
  else
    cout << c - a << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long a, d, c;
  cin >> a >> d >> c;
  for (long long i = 0; i < d; i++) {
    if (abs(a) <= c / 2) {
      i += ((d - i - 1) / 2) * 2;
    }
    if (a > 0)
      a -= c;
    else
      a += c;
  }
  cout << abs(a) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MAX 100000000000000000
int main() {

  // freopen("inputfile.txt","r",stdin);
  IOS;
  ll x, d, k;
  cin >> x >> k >> d;
  x = abs(x);
  if (x > 0 && (x / d) >= k) {
    cout << x - (d * k) << endl;
    return 0;
  }
  ll y = (x / d);
  x -= (d * y);
  k -= y;
  if (k % 2 == 0)
    cout << abs(x) << endl;
  else
    cout << min(abs(x - d), abs(x + d)) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MAX 100000000000000000
int main() {

  // freopen("inputfile.txt","r",stdin);
  IOS;
  ll a, b, c;
  cin >> a >> b >> c;
  ll ans = MAX;
  while (b--) {
    ans = min(abs(a + c), abs(a - c));
    if (min(abs(a + c), abs(a - c)) == abs(a - c))
      a -= c;
    else
      a += c;
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  X = abs(X);
  if (X / D >= K) {
    cout << X - K * D << "\\n";
    return 0;
  }
  ll t = X / D;
  if ((K - t) % 2 == 0) {
    cout << X - D * t << "\\n";
  } else {
    cout << (t + 1) * D - X << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  X = abs(X);
  if (X / D >= K) {
    cout << 0 << "\\n";
    return 0;
  }
  ll t = D / X;
  if (X % D < D - X % D) {
    if ((K - t) % 2 == 0) {
      cout << X % D << "\\n";
    } else {
      cout << D - X % D << "\\n";
    }
  } else {
    if ((K - t) % 2 == 1) {
      cout << D - X % D << "\\n";
    } else {
      cout << X % D << "\\n";
    }
  }

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> //AtCoder
// #include <iostream> //Codeforces
// #include <vector> //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a / gcd(a, b) * b; }     // 最小公倍数

/****************************** START ******************************/

signed main() {
  int X, K, D;
  cin >> X >> K >> D;
  if (X < 0) {
    X = -X;
  }
  int ans;

  if (X / D >= K) {
    ans = X - K * D;
  } else {
    K -= X / D;
    int A = X % D;
    if (K % 2 == 0) {
      ans = A;
    } else {
      ans = D - A;
    }
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h> //AtCoder
// #include <iostream> //Codeforces
// #include <vector> //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a / gcd(a, b) * b; }     // 最小公倍数

/****************************** START ******************************/

signed main() {
  int X, K, D;
  cin >> X >> K >> D;

  rep(i, 0, K) {
    if (X >= 0) {
      X -= D;
    } else if (X < 0) {
      X += D;
    }
  }
  cout << abs(X);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
int main() {
  long long X, K, D;
  long long ans;
  cin >> X >> K >> D;
  if (X < 0)
    X = -1 * X;
  long long temp = X / D;
  if (K < X / D) {
    ans = X - K * D;
    cout << ans << endl;
    return 0;
  }
  if ((K - temp) % 2 == 0)
    ans = X - D * temp;
  else
    ans = (X - D * (temp + 1)) * -1;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;

  long long ans_1 = x, ans_2 = x, ans = 1e18;
  for (long long i = 0; i < k; i++) {
    ans_1 += d;
    ans_2 -= d;
    ans = abs(ans_1) < abs(ans_2) ? abs(ans_1) : abs(ans_2);
    ans_1 = ans;
    ans_2 = ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

constexpr long long INFLL = 1e18;

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  map<ll, ll> check;
  bool c = false;
  check[x] = k;
  if (abs(x) / d > 0) {
    ll count = abs(x) / d;
    if (k >= count) {
      if (x > 0) {
        x = x - d * count;
      } else {
        x = x + d * count;
      }
      k -= count;
    } else {
      if (x > 0) {
        x = x - d * k;
      } else {
        x = x + d * k;
      }
      k = 0;
    }
  }
  while (k > 0) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (check[x] == 0) {
      check[x] = k;
    } else {
      c = true;
      k--;
      break;
    }

    k--;
  }
  if (c) {
    if (k % 2 == 0) {
      cout << abs(x) << endl;
    } else {
      if (x > 0) {
        x -= d;
      } else {
        x += d;
      }
      cout << abs(x) << endl;
    }
  } else {
    cout << abs(x) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

constexpr long long INFLL = 1e18;

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  map<ll, ll> check;
  bool c = false;
  check[x] = k;
  if (abs(x) / d > 0) {
    ll count = abs(x) / d;
    if (k >= count) {
      if (x > 0) {
        x = x - d * count;
      } else {
        x = x + d * count;
      }
      k -= count;
    }
  }
  while (k > 0) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (check[x] == 0) {
      check[x] = k;
    } else {
      c = true;
      k--;
      break;
    }

    k--;
  }
  if (c) {
    if (k % 2 == 0) {
      cout << abs(x) << endl;
    } else {
      if (x > 0) {
        x -= d;
      } else {
        x += d;
      }
      cout << abs(x) << endl;
    }
  } else {
    cout << abs(x) << endl;
  }

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;

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

  x = abs(x);

  bool ok = true;

  if (k * d > x)
    ok = false;
  if (log10(k) + log10(d) > 17)
    ok = false;

  if (ok) {
    ll ans = x;
    rep(i, k) ans -= d;
    cout << ans << endl;
    return 0;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;

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

  x = abs(x);

  bool ok = true;
  ll tmp = 0;
  rep(i, k) {
    tmp += d;
    if (tmp >= x) {
      ok = false;
      break;
    }
  }

  if (ok) {
    ll ans = x;
    rep(i, k) ans -= d;
    cout << ans << endl;
    return 0;
  }

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

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

int main() {
  long long int X, K, D;
  cin >> X >> K >> D;
  long long int first = X;
  long long int k = 0;
  // cout << X/D << endl;

  if (abs(X) >= D) {
    if (X > 0) {
      k = X / D;
      X %= D;
      if (k > K) {
        k = K;
        X = first - D * K;
      }

    } else {
      k = abs(X) / D;
      X = -(abs(X) % D);
      if (k > K) {
        k = K;
        X = first + D * K;
      }
    }
  }
  if ((K - k) % 2 == 1) {
    if (X > 0) {
      X -= D;
    } else if (X <= 0) {
      X += D;
    }
  }

  cout << abs(X) << endl;
}

Error: TLE

Fixed submission:

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

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

    } else {
      if ((K - (i + 1)) % 2 == 0) {
        if (X > 0) {
          X -= D;
          break;
        } else {
          X += D;
          break;
        }
      } else {
        break;
      }
    }
  }

  cout << abs(X) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#define N 1010
#define ll long long
#define mo ((ll)1e9 + 7)
// 10000000000037ll
// 21993833369ll
#define db double
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define drep(i, a, b) for (int i = a; i >= b; --i)
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
bool cmp(int a, int b) { return a > b; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);
  if (x < 0)
    x *= -1ll;
  ll tmp = min(k, x / d);
  k -= tmp, x -= tmp * 1ll * d;
  if (k & 1)
    x -= d;
  printf("%lld\\n", (ll)fabs(x));
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  long long x, k, d;
  cin >> x >> k >> d;
  long long ax = abs(x);
  while (k--) {
    long long res1 = abs(ax - d);
    long long res2 = abs(ax + d);
    if (res1 < res2) {
      ax = res1;
    } else {
      ax = res2;
    }
  }
  cout << ax;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  long X, K, D;
  cin >> X >> K >> D;
  int rem;
  if (X >= 0) {
    rem = 1;
  } else {
    rem = -1;
  }
  long N = X * rem / D;
  if (N > K) {
    X = X * rem - D * K;
  } else {
    X = X * rem - D * N;
    if ((K - N) % 2 == 1) {
      X -= D;
    }
  }
  if (X < 0) {
    X *= -1;
  }
  cout << X << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long X, K, D;
  cin >> X >> K >> D;
  int rem;
  if (X >= 0) {
    rem = 1;
  } else {
    rem = -1;
  }
  if (X * rem / D > K) {
    cout << X * rem - D * K << endl;
    return 0;
  }
  for (int i = 1; i <= K; i++) {
    if (X >= 0) {
      X -= D;
    } else {
      X += D;
    }
    if (rem * X < 0) {
      if ((K - i) % 2 == 1) {
        X += rem * D;
      }
      break;
    }
  }
  if (X < 0) {
    X *= -1;
  }
  cout << X << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  /* Input */
  long long int X, K, D;
  cin >> X >> K >> D;
  X = abs(X);

  /* Main Logic */
  long long int straight = min(K, X / D);
  K -= straight;
  X -= straight * D;

  long long int ans = 0;
  if (K % 2 == 0) {
    ans = X;
  } else {
    if (abs(X - D) >= abs(X + D))
      X = X + D;
    else if (abs(X - D) <= abs(X + D))
      X = X - D;
    ans = X;
  }

  /* Output */
  cout << abs(ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  /* Input */
  long long int X, K, D;
  cin >> X >> K >> D;

  /* Main Logic */
  long long int nowX = X;
  while ((nowX > D || nowX < -D) && K > 0) {
    if (abs(nowX - D) >= abs(nowX + D))
      nowX += D;
    else if (abs(nowX - D) <= abs(nowX + D))
      nowX -= D;
    K--;
  }

  long long int ans = 0;
  if (K % 2 == 0) {
    ans = nowX;
  } else {
    if (abs(nowX - D) >= abs(nowX + D))
      nowX = nowX + D;
    else if (abs(nowX - D) <= abs(nowX + D))
      nowX = nowX - D;
    ans = nowX;
  }

  /* Output */
  cout << abs(ans) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;

  ll t = x / d;

  if (t >= k) {
    cout << x - k * d;

  } else {
    x %= d;
    k -= t;
    bool a = x > d / 2;
    bool b = k % 2;
    if (a == b)
      cout << min(x, d - x);
    else {
      if (a && !b)
        cout << x;
      if (!a && b)
        cout << d - x;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  while (x >= d && k) {
    x -= d;
    k--;
  }
  if (!k) {
    cout << x;

  } else {
    bool a = x > d / 2;
    bool b = k % 2;
    if (a == b)
      cout << min(x, d - x);
    else {
      if (a && !b)
        cout << x;
      if (!a && b)
        cout << d - x;
    }
  }

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long X, K, D, n;
  cin >> X >> K >> D;
  X = abs(X); // X >= 0で考える(絶対値なので)
  n = X / D;
  if (n > K) { // XがD以下になるために、K回より多く移動しないといけないとき
    X -= D *
         K; // マイナス方向にDだけ移動をK回だけ繰り返す(これが一番近づけられる)
  } else {  // K回以下でXがD以下になれるとき
    X -= n * D;       // XをD以下にする
    K -= n;           // XがD以下になるまでの回数を引く
    if (K % 2 == 1) { // 残り回数が奇数回のとき
      X = abs(X - D);
    }
    //--(X-2D)----------(X-D)----(0)------(X)----------(X+D)----------(X+2D)
    //                         4       6
    // 残り奇数回→(X-(奇数)D)に動く→最も絶対値が小さいのは(X-D)(←負なので絶対値に直すことに注意)
    // 残り偶数回→(X-(偶数)D)に動く→最も絶対値が小さいのは(X)
  }
  cout << X << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  for (int i = 0; i < K; i++) {
    if (abs(X - D) < abs(X + D)) {
      X = X - D;
      if (abs(X) <= D) {
        if ((K - i) % 2 == 0) {
          if (abs(X - D) < abs(X + D)) {
            X = X - D;
          } else {
            X = X + D;
          }
        }
        break;
      }
    } else {
      X = X + D;
      if (abs(X) <= D) {
        if ((K - i) % 2 == 0) {
          if (abs(X - D) < abs(X + D)) {
            X = X - D;
          } else {
            X = X + D;
          }
        }
        break;
      }
    }
  }
  cout << abs(X) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

// C - Waking Takahashi
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

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

Error: 0

Fixed submission:

// C - Waking Takahashi
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

ll dfs(ll x, ll k, ll d) {
  if (k == 0)
    return x;

  if (abs(x + d) < abs(x - d))
    dfs(x + d, k - 1, d);
  else
    dfs(x - d, k - 1, d);
}

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

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

int main() {
  long long x, K, D, ans, cnt;
  cin >> x >> K >> D;
  if (x < 0)
    x = -x;
  if (x / D >= K) {
    ans = x - K * D;
  } else {

    K -= x / D;
    cnt = x % D;
    if (K % 2 == 0) {
      ans = cnt;
    } else {
      ans = D - cnt;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  long long x, K, D, prevx, nextp, nextm;
  cin >> x >> K >> D;
  for (long long i = 0; i < K; i++) {
    prevx = x;
    nextp = x + D;
    nextm = x - D;
    if (nextp < abs(nextm)) {
      x = nextp;
    } else {
      x = nextm;
    }
    if (x == abs(prevx))
      break;
    if (x == 0) {
      if ((K - i + 1) % 2 == 1) {
        x = prevx;
      }
      break;
    }
  }
  cout << abs(x) << endl;
}