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

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

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  long long n = x / d + 1;
  long long m = min(k, n);
  x -= d * m;
  if ((k - m) % 2 == 0)
    cout << abs(x);
  else
    cout << x + d;
}


Error: TLE

Fixed submission:

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

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  double x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  long long dm = 1000000000000000;
  int i = 0;
  while (k >= 2 * i) {
    if (dm > abs(x - (k - 2 * i) * d))
      dm = abs(x - (k - 2 * i) * d);
    i++;
  }
  cout << dm << endl;
  return 0;
}
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;
using ll = long long;

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

  if (x < 0) {
    x = -x;
  }

  ll m = x / d;

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

  if ((k - m) % 2 == 0) {
    cout << x % d << endl;
  } else {
    cout << -((x % d) - d) << endl;
  }
}


Error: TLE

Fixed submission:

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

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

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, j, k) for (int i = j; i < k; i++)
#define ll long long

using namespace std;

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  X = abs(X);
  ll ans = 0;

  if (K <= X / D) {
    ans = X - D * K;
  } else {
    K = K - X / D;
    X = X - ((X / D) * D);
    if (K % 2 != 0)
      ans = D - X;
    else
      ans = X;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, j, k) for (int i = j; i < k; i++)

using namespace std;

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

  int tw = 0, th = 0, fi = 0;

  while (X % 5 == 0 && K % 5 == 0 && D % 5 == 0 && X > 5 && K > 5 && D > 5) {
    X /= 5;
    K /= 5;
    D /= 5;
    fi++;
  }

  while (X % 3 == 0 && K % 3 == 0 && D % 3 == 0 && X > 3 && K > 3 && D > 3) {
    X /= 3;
    K /= 3;
    D /= 3;
    th++;
  }

  while (X % 2 == 0 && K % 2 == 0 && D % 2 == 0 && X > 2 && K > 2 && D > 2) {
    X /= 2;
    K /= 2;
    D /= 2;
    tw++;
  }

  // 計算時間→座標を圧縮する？
  rep(i, K) {
    if (abs(X + D) < abs(X - D))
      X += D;
    else
      X -= D;
  }

  rep(i, th) X *= 3;
  rep(i, fi) X *= 5;
  rep(i, tw) X *= 2;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <unordered_set>

using namespace std;
typedef long long ll;
const int N = 1010;

int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);

  ll t = abs(x) / d;

  if (k > t) {
    if (x > 0)
      x -= t * d;
    else
      x += t * d;
    if ((k - t) & 1) {
      if (abs(x + d) < abs(x - d))
        x += d;
      else
        x -= d;
    }
  } else {
    if (x > 0)
      x -= k * d;
    else
      x += k * d;
  }

  printf("%lld\\n", abs(x));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
typedef long long ll;
const int N = 1010;

int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);

  for (int i = 0; i < k; i++) {
    //		if(x > 0)
    //		{
    if (abs(x + d) < abs(x - d)) {
      x += d;
    } else {
      x -= d;
    }
    //		}
    //		else
    //		{
    //			if()
    //
    //		}
    //
  }

  printf("%lld\\n", abs(x));

  return 0;
}
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;
  x = abs(x);
  if (x > 0 && (x / d) >= k) {
    long long qq = x / d;
    x = x - (k)*d;
    cout << x;
    return 0;
  }
  long long y = x / d;
  x -= y * d;
  k -= y;
  if (k % 2 == 0)
    cout << abs(x);
  else
    cout << min(abs(x - d), abs(x + d));
  return 0;
}


Error: TLE

Fixed submission:

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

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

  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  while (k && x >= 0) {
    x -= d;
    k--;
  }
  // cout<<k;
  k %= 2;

  if (k)
    x += d;
  cout << abs(x);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

  ll x, k, d;

  cin >> x >> k >> d;

  x = abs(x);

  ll z = x / d;

  if (z > k) {
    cout << abs(x - (d * k));
  }

  else {
    x -= (z * d);

    k = k - z;

    if (k % 2 == 0) {
      cout << abs(x);
    }

    else {
      cout << abs(x - d);
    }
  }
}


Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

  ll x, k, d;

  cin >> x >> k >> d;

  if (x >= 0) {

    if (x == 0) {
      if (k % 2 == 0) {

        cout << abs(x);
        return 0;
      }

      else {
        x -= d;
        cout << abs(x);
        return 0;
      }
    }

    while (1) {

      x -= d;
      k--;

      if (x < 0) {
        break;
      }

      if (k == 0) {
        break;
      }
    }

    if (k == 0) {
      cout << abs(x);
    }

    else {
      ll z = x - d;

      if (abs(z) <= x) {
        if (k % 2 == 0) {
          cout << abs(x);
        }

        else {
          cout << abs(x - d);
        }
      }

      else {
        if (k % 2 == 0) {
          cout << abs(x);
        }

        else {
          cout << abs(x + d);
        }
      }
    }

  }

  else {
    while (1) {

      x += d;
      k--;

      if (x > 0) {
        break;
      }

      if (k == 0) {
        break;
      }
    }

    if (k == 0) {
      cout << abs(x);
    }

    else {
      ll z = x + d;

      if (abs(z) <= abs(x)) {
        if (k % 2 == 0) {
          cout << abs(x);
        }

        else {
          cout << abs(x + d);
        }
      }

      else {
        if (k % 2 == 0) {
          cout << abs(x);
        }

        else {
          cout << abs(x - d);
        }
      }
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define sq(x) (x) * (x)
#define PI acos(-1.0)
#define all(x) x.begin(), x.end()
#define nl '\\n'

typedef long long int ll;
typedef unsigned long long int llu;

int main() {
  faster ll a, b, c, ans, x, y;
  cin >> a >> b >> c;
  if (a >= 0) {
    x = a / c;
    if (x == b)
      cout << a - (c * x) << nl;
    else if (x > b)
      cout << a - (c * b) << nl;
    else {
      a = a - (c * x) - c;
      x++;
      y = b - x;
      if (y % 2 == 0)
        cout << abs(a) << nl;
      else
        cout << a + c << nl;
    }
  } else {
    ans = abs(a);
    x = ans / c;
    if (x == b)
      cout << abs(a + c * x) << nl;
    else if (x > b)
      cout << abs(a + (c * b)) << nl;
    else {
      a = a + (c * x) + c;
      x++;
      y = b - x;
      if (y % 2 == 0)
        cout << abs(a) << nl;
      else
        cout << abs(a - c) << nl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define sq(x) (x) * (x)
#define PI acos(-1.0)
#define all(x) x.begin(), x.end()
#define nl '\\n'

typedef long long int ll;
typedef unsigned long long int llu;

int main() {
  faster ll a, b, c, ans, x, y;
  cin >> a >> b >> c;
  if (a >= 0) {
    x = 0;
    while (1) {
      a = a - c;
      x++;
      if (x == b || a <= 0)
        break;
    }
    y = b - x;
    if (y % 2 == 0)
      cout << abs(a) << nl;
    else
      cout << a + c << nl;
  } else {
    x = 0;
    while (1) {
      a = a + c;
      x++;
      if (x == b || a >= 0)
        break;
    }
    y = b - x;
    if (y % 2 == 0)
      cout << abs(a) << nl;
    else
      cout << abs(a - c) << nl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long int ll;
using namespace std;
int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    ll x, k, d, r, x1, x2;
    cin >> x >> k >> d;
    if (llabs(x) / d >= k) {
      if (x < 0)
        x = x + k * d;
      else
        x = x - k * d;
    } else {
      r = llabs(x) / d;
      if (x < 0) {
        x1 = x + r * d;
        x2 = x1 + d;
        if ((k - r) % 2 == 0)
          x = x1;
        else
          x = x2;
      } else {
        x1 = x - r * d;
        x2 = x1 - d;
        if ((k - r) % 2 == 0)
          x = x1;
        else
          x = x2;
      }
    }
    cout << llabs(x) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long int ll;
using namespace std;
int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    ll x, k, d;
    cin >> x >> k >> d;
    do {
      if (llabs(x) >= d) {
        if (x < 0)
          x = x + d;
        else
          x = x - d;
      } else {
        if (x < 0) {
          if (x + d < llabs(x) || k == 1)
            x = x + d;
          else
            x = x - d;
        } else {
          if (llabs(x - d) < x || k == 1)
            x = x - d;
          else
            x = x + d;
        }
      }
      k--;
    } while (k > 0);
    cout << llabs(x) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MAXX 105

ll n;
ll l[MAXX];

int main() {
  _FastIO;

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


Error: TLE

Fixed submission:

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

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MAXX 105

ll n;
ll l[MAXX];

int main() {
  _FastIO;

  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  while ((x - d) >= 0 && k) {
    k--;
    x -= d;
  }
  if (!k) {
    cout << x << endl;
    return 0;
  }
  if (k % 2) {
    cout << abs(x - d) << endl;
    return 0;
  }
  cout << x << 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;
  long long ans;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  if (k < (double)x / d) {
    ans = x - k * d;
  } else {
    long long n = x / d + 1;
    /*if(k % x != 0){
        n++;
    }*/
    long long ans1 = x - n * d;
    // cout << ans1 << endl;
    long long ans2 = ans1 + d;
    long long amari = k - n;
    if (amari % 2 == 0)
      ans = ans1;
    else
      ans = ans2;
  }
  cout << abs(ans) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long long x, k, d;
  long long ans;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  if (k * d < x) {
    ans = x - k * d;
  } else {
    long long ans1 = x % d;
    long long ans2 = abs(ans1 - d);
    long long n = x / d;
    if (k % x != 0) {
      n++;
    } else {
      swap(ans1, ans2);
    }
    long long amari = k - n;
    if (amari % 2 == 0)
      ans = ans2;
    else
      ans = ans1;
  }
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  if (abs(x) / d < k) {
    int64_t a, b;
    a = abs(x) / d;
    if (x < 0) {
      x += d * a;
      b = a;
      if (abs(x) > abs(x - d)) {
        x -= d;
        b++;
      }
    } else {
      x -= d * a;
      b = a;
      if (abs(x) > abs(x + d)) {
        x += d;
        b++;
      }
    }

    if ((k - b) % 2 == 1) {
      if (abs(x - d) > abs(x + d))
        x += d;
      else
        x -= d;
    }

  } else {
    if (x > 0)
      x -= d * k;
    else
      x += d * k;
  }

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


Error: TLE

Fixed submission:

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

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

  if (abs(x) / d < k) {
    int64_t a, b;
    a = abs(x) / d;
    if (x < 0) {
      x += d * a;
      b = a;
    } else {
      x -= d * a;
      b = a;
    }
    if (abs(x) > abs(x + d)) {
      x += d;
      b++;
    } else if (abs(x) > abs(x - d)) {
      x -= d;
      b++;
    }
    if ((k - b) % 2 == 1) {
      if (abs(x - d) > abs(x + d))
        x += d;
      else
        x -= d;
    }

  } else {
    for (int64_t i = 0; i < k; i++) {

      if (abs(x - d) < abs(x + d))
        x -= d;
      else
        x += d;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double x, k, d;
  cin >> x >> k >> d;
  ll y = k;
  if (y % 2 != 0) {
    long double r = (k + 1) / 2;
    long double l = (-1) * (r - 1);
    long double temp = l;
    long double mid;
    while (l < r) {
      mid = floor((l + r) / 2);
      if ((2 * mid - 1) * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + (2 * l - 1) * d) << endl;
    } else {
      cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
           << endl;
    }
  } else {
    long double r = k / 2;
    long double l = (-1) * r;
    long double temp = l, mid;
    while (l < r) {
      mid = floor(((l + r) / 2));
      if (2 * mid * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + 2 * l * d) << endl;
    } else {
      cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double x, k, d;
  cin >> x >> k >> d;
  ll y = k;
  if (y % 2 != 0) {
    long double r = (k + 1) / 2;
    long double l = (-1) * (r - 1);
    long double temp = l;
    long double mid;
    while (l < r) {
      mid = (ll)((l + r) / 2);
      if ((2 * mid - 1) * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + (2 * l - 1) * d) << endl;
    } else {
      cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
           << endl;
    }
  } else {
    long double r = k / 2;
    long double l = (-1) * r;
    long double temp = l, mid;
    while (l < r) {
      mid = floor(((l + r) / 2));
      if (2 * mid * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + 2 * l * d) << endl;
    } else {
      cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << 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 RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()

void Main();

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

void Main() {
  int64_t x, k, d;
  cin >> x >> k >> d;
  x = abs(x);

  int64_t f1 = x / d;
  if (k <= f1) {
    cout << abs(x - k * d) << endl;
    return;
  }

  k -= f1;
  x %= d;

  if (k % 2 == 0) {
    cout << abs(x) << endl;
  } else {
    cout << abs(x - d) << 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 RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()

void Main();

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

void Main() {
  int64_t x, k, d;
  cin >> x >> k >> d;

  if (x > 0) {
    while (x > 0 && k > 0) {
      x -= d;
      k--;
    }
  } else if (x < 0) {
    while (x < 0 && k > 0) {
      x += d;
      k--;
    }
  }

  // cout << "x: " << x << endl;
  // cout << "k: " << k << endl;

  bool f1 = x >= 0;

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

#include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define maxn 100005
#define int long long
using namespace std;
int n;
int x, k, d;
int a[105];
int ans;
signed main() {
  ios;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d > k) {
    cout << abs(x - d * k) << "\\n";
  } else {
    k = k - x / d;
    if (k & 1) {
      cout << d - x % d << "\\n";
    } else {
      cout << x % d << "\\n";
    }
  }
}
/*if (K * D <= X)
        ans = X - K * D;
else
{
        K -= X / D;
        int A = X % D;

        if (K % 2 == 0)
                ans = A;
        else
                ans = D - A;
}
cout << ans<<endl;*/
//}
/*#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>

#define ll long long
using namespace std;

void solve(){
  ll x,k,d;
  cin >>x>> k>> d;
  x = abs(x);
  d = abs(d);


}

int main(){
  int t=1;
  // cin>>t;
  while(t--){
    solve();
  }
 return 0;
}*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define maxn 100005
#define int long long
using namespace std;
int n;
int X, K, D;
int a[105];
int ans;
signed main() {
  ios;
  cin >> X >> K >> D;
  while (K--) {
    if (abs(X + D) > abs(X - D)) {
      X = X - D;
    } else {
      X = X + D;
    }
  }
  cout << abs(X);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll jumps = x / d;
  if (jumps > k) {
    cout << x - (k * d);
  } else {
    x -= jumps * d;
    k -= jumps;
    if (k % 2 == 0) {
      cout << x;
    } else {
      cout << min(abs(x + d), abs(x - d));
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll x, k, d;
  cin >> x >> k >> d;
  while (k--) {
    if (abs(x + d) < abs(x - d)) {
      x = x + d;
    } else {
      x -= d;
    }
  }
  cout << abs(x);

  return 0;
}