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

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

//----------素因数分解----------
vector<ll> prime_factorization(ll n) {
  vector<ll> factors(n + 1, 0);
  rep3(i, n + 1) {
    if (i <= 1)
      continue;
    while (n % i == 0) {
      n /= i;
      factors[i]++;
    }
  }
  return factors;
}

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

Error: TLE

Fixed submission:

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

//----------素因数分解----------
vector<ll> prime_factorization(ll n) {
  vector<ll> factors(n + 1, 0);
  rep3(i, n + 1) {
    if (i <= 1)
      continue;
    while (n % i == 0) {
      n /= i;
      factors[i]++;
    }
  }
  return factors;
}

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long x, k, d;
  cin >> x >> k >> d;
  long kk = abs(x) + d; // 距離
  long nn = 0;          // 何回でアウトしたか

  nn = abs(x) / d;
  if (nn > k)
    nn = k;
  kk = abs(x) - nn * d;

  long nokori = k - nn;
  if (nokori % 2 == 0)
    cout << kk;
  else
    cout << abs(kk - d);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long x, k, d;
  cin >> x >> k >> d;
  long kk = abs(x) + d; // 距離
  long nn = 0;          // 何回でアウトしたか
  for (long i = 1; i <= k; i++) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (kk >= abs(x - 0)) {
      kk = abs(x - 0);
      nn = i;
    } else {
      break;
    }
  }
  long nokori = k - nn;
  if (nokori % 2 == 0)
    cout << kk;
  else
    cout << abs(kk - d);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll ans = 0;
  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;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;           // 10^19 < 9223372036854775807
typedef unsigned long long ull; // 10^20 < 18446744073709551615

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define MOD 1000000007

int main(void) {
  ll X, K, D, k;
  cin >> X >> K >> D;

  ll ans;

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;           // 10^19 < 9223372036854775807
typedef unsigned long long ull; // 10^20 < 18446744073709551615

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define MOD 1000000007

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

  ll ans = 0;

  ll before = 10000000000000000, bebefore = 10000000000000000;
  REP(i, K) {
    bebefore = before;
    before = X;
    if (X < 0) {
      //            if ((X + D) != before) {
      X += D;
      //           }
    } else {
      //           if ((X - D) != before) {
      X -= D;
      //            }
    }
    ans = X;
    if ((before == X) || (bebefore == X)) {
      if ((K - i) % 2 == 0) {
        ans = before;
      } else {
        ans = bebefore;
      }
      break;
    }
  }

  cout << abs(ans) << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  while (1) {
    if ((abs(X) <= D && K % 2 == 0) || K == 0) {
      break;
    }
    if (X < 0) {
      if (D <= abs(X)) {
        long long buf = min(abs(X) / D, K);
        K -= buf;
        X = X + buf * D;
      } else {
        X += D;
        --K;
      }
    } else {
      if (D <= X) {
        long long buf = min(X / D, K);
        K -= buf;
        X = X - buf * D;
      } else {
        X -= D;
        --K;
      }
    }
  }

  cout << abs(X) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  while (1) {
    if ((abs(X) <= D && K % 2 == 0) || K == 0) {
      break;
    }
    if (X < 0)
      X += D;
    else
      X -= D;
    --K;
  }

  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 unsigned long long ull;
const long long INF = 1LL << 60;

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  X = abs(X);
  ll cnt = X / D;
  if (cnt > K) {
    cout << X - K * D << endl;
    return 0;
  }
  ll r = X % D;
  if ((K - cnt) % 2 == 0)
    cout << r << endl;
  else
    cout << min(abs(r + D), abs(r - D)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long INF = 1LL << 60;

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

#include <cmath>
#include <cstdlib>
#include <iostream>

void sub(long long int *x, long int *k, unsigned long int *d) {
  long long int tmp = *x / *d;

  if (*x % *d == 0) {
    tmp--;
  }

  if (tmp + 1 <= *k) {
    *k -= (tmp + 1);

    *x -= *d * (tmp + 1);
  } else {
    *x -= *d * *k;

    *k = 0;
  }
}

void sum(long long int *x, long int *k, unsigned long int *d) {
  *x *= -1;
  long long int tmp = *x / *d;

  if (*x % *d == 0) {
    tmp--;
  }

  *x *= -1;

  if (tmp + 1 <= *k) {
    *k -= (tmp + 1);

    *x += *d * (tmp + 1);
  } else {
    *x += *d * *k;

    *k = 0;
  }
}

int main() {
  long long int X;
  long int K;
  unsigned long int D;

  std::cin >> X >> K >> D;

  if (X > 0) {
    sub(&X, &K, &D);

    if (K > 0) {
      if (K % 2 == 1) {
        X += D;
      }
    }
  } else {
    sum(&X, &K, &D);

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

  if (X < 0) {
    X *= -1;
  }

  std::cout << X << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>

void sub(long long int *x, long int *k, unsigned long int *d) {
  *x -= *d;
  *k -= 1;

  if (*k <= 0 || *x <= 0) {
    return;
  }

  sub(x, k, d);
}

void sum(long long int *x, long int *k, unsigned long int *d) {
  *x += *d;
  *k -= 1;

  if (*k <= 0 || *x >= 0) {
    return;
  }

  sum(x, k, d);
}

int main() {
  long long int X;
  long int K;
  unsigned long int D;

  std::cin >> X >> K >> D;

  if (X > 0) {
    sub(&X, &K, &D);

    if (K > 0) {
      if (K % 2 == 1) {
        X += D;
      }
    }
  } else {
    sum(&X, &K, &D);

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

  if (X < 0) {
    X *= -1;
  }

  std::cout << X << std::endl;

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

// WA
#include <bits/stdc++.h>
#define LL long long
#define PB push_back
using namespace std;

int main() {
  LL x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if (k > (x / d)) {
    LL z = k - x / d;
    if (z % 2)
      cout << d - x % d;
    else
      cout << x % d;
  } else
    cout << x - (d * k);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define PB push_back
using namespace std;
void koro(LL x, LL k, LL d) {
  while (true) {
    x -= d;
    k--;
    if (k == 0 || x <= 0)
      break;
  }
  if (k != 0) {
    if (k % 2)
      cout << abs(x + d);
    else
      cout << abs(x);
  } else
    cout << abs(x);
}
int main() {
  LL x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    koro(x, k, d);
  } else if (x < 0) {
    x = abs(x);
    koro(x, k, d);
  } else {
    if (k % 2)
      cout << d;
    else
      cout << "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;

  if (x >= 0) {
    long long int a;
    a = min(x / d + 1, k);
    x = x - a * d;
    k = k - a;
  } else {
    long long int a;
    a = min((-1 * x) / d + 1, k);
    x = x + a * d;
    k = k - a;
  }

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


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) {
    while (x >= 0 && k > 0) {
      x -= d;
      k--;
    }
  } else {
    while (x < 0 && k > 0) {
      x += d;
      k--;
    }
  }

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

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

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

  ll moves = abs(x) / d;
  if (moves < k) {
    k -= moves;
    x = min(abs(x - moves * d), abs(x + moves * d));
  } else {
    cout << min(abs(x - k * d), abs(x + k * d));
    return;
  }
  if (k % 2 == 1) {
    ll a = x - d, b = x + d;
    if (abs(a) < abs(b)) {
      x = abs(a);
    } else {
      x = abs(b);
    }
  }
  cout << abs(x);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll t;
  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

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

  for (ll i = 0; i < k; i++) {
    ll a = x - d, b = x + d;
    if (abs(x) <= abs(a) && abs(x) <= abs(b)) {
      left = k - i;
      break;
    }
    if (abs(a) < abs(b)) {
      x = a;
    } else {
      x = b;
    }
  }
  if (left % 2 == 1) {
    ll a = x - d, b = x + d;
    if (abs(a) < abs(b)) {
      x = abs(a);
    } else {
      x = abs(b);
    }
  }
  cout << abs(x);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll t;
  solve();
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

long long longabs(long long x) {
  if (x < 0)
    x *= -1;
  return x;
}

int main() {
  long long pos;
  long long n, d;

  cin >> pos >> n >> d;
  pos = longabs(pos);

  if (pos / d >= n) {
    cout << pos - n * d << endl;
    return 0;
  }

  else {
    // 10 8 2 のとき 0 3 2 と同じである  10 8 3  == 1 5 3
    n -= pos / d;
    pos -= (pos / d) * d;
  }

  if (n % 2 == 0)
    cout << pos << endl;

  else
    cout << d - pos << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

long long myabs(long long x) {
  if (x < 0)
    x *= -1;
  return x;
}

int main() {
  long long pos;
  unsigned long long n, d;

  cin >> pos >> n >> d;
  pos = myabs(pos);

  if (pos >= n * d) {
    cout << pos - n * d << endl;
    return 0;
  }

  for (long long i = 0; i < n; i++) {

    if (pos - d <= 0) {
      if ((n - i - 1) % 2 == 0) {
        cout << myabs(pos - d) << endl;
        return 0;
      } else
        cout << myabs(pos) << endl;
      return 0;
    }
    pos = myabs(pos - d);
  }

  cout << pos << 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;
  cin >> X >> K >> D;
  X = abs(X);
  long long ans;
  if ((X / D) > K) {
    ans = X - D * K;
  } else {
    long long E = X / D;
    K -= E;
    X -= E * D;
    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;

int main() {
  long int X, K, D;
  cin >> X >> K >> D;
  for (int i = 0; i < K; i++) {
    long int Xp = X + D;
    long int Xn = X - D;
    if (abs(Xp) > abs(Xn)) {
      X = Xn;
    } else {
      X = Xp;
    }
  }
  cout << abs(X) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  ll x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    ll cnt = x / d;
    if (cnt >= k)
      x -= k * d, k = 0;
    else {
      x -= cnt * d, k -= cnt;
      while (k > 0 && abs(x - d) < abs(x))
        k--, x -= d;
    }
  } else {
    ll cnt = abs(x) / d;
    if (cnt >= k)
      x += k * d, k = 0;
    else {
      x += cnt * d, k -= cnt;
      while (k > 0 && abs(x + d) < abs(x))
        k--, x += d;
    }
  }
  if (k % 2 == 0)
    cout << abs(x) << "\\n";
  else {
    if (abs(x - d) < abs(x + d))
      cout << abs(x - d) << "\\n";
    else
      cout << abs(x + d) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  ll x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    int cnt = x / d;
    if (cnt >= k)
      x -= k * d, k = 0;
    else {
      x -= cnt * d, k -= cnt;
      while (k > 0 && abs(x - d) < abs(x))
        k--, x -= d;
    }
  } else {
    int cnt = abs(x) / d;
    if (cnt >= k)
      x += k * d, k = 0;
    else {
      x += cnt * d, k -= cnt;
      while (k > 0 && abs(x + d) < abs(x))
        k--, x += d;
    }
  }
  if (k % 2 == 0)
    cout << abs(x) << "\\n";
  else {
    if (abs(x - d) < abs(x + d))
      cout << abs(x - d) << "\\n";
    else
      cout << abs(x + d) << "\\n";
  }
  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;
  cin >> X >> K >> D;
  bool a = false;
  if (X > 0) {
    long long A = X / D;
    if (K < A) {
      X -= K * D;
      cout << abs(X) << endl;
      a = true;
    }
    X -= A * D;
    K -= A;
    if (abs(X - D) < abs(X) && K > 0) {
      X -= D;
      K--;
    }
  } else if (X < 0) {
    long long A = abs(X) / D;
    if (K < A) {
      X += K * D;
      cout << abs(X) << endl;
      a = true;
    }
    X += A * D;
    K -= A;
    if (abs(X + D) < abs(X) && K > 0) {
      X += D;
      K--;
    }
  }
  if (!a) {
    if (K % 2 == 0) {
      cout << abs(X) << endl;
    } else if (abs(X - D) < abs(X + D)) {
      cout << abs(X - D) << endl;
    } else {
      cout << abs(X + D) << endl;
    }
  }
}


Error: TLE

Fixed submission:

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

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  for (long long 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;

int main() {
  long long X, K, D, ans, num;
  cin >> X >> K >> D;
  if (abs(X) == D) {
    if (K % 2 == 0)
      ans = abs(X);
    else
      ans = 0;
  } else {
    num = abs(X / D);
    if (num <= K) {
      X = abs(X) - num * D;
      K = K - num;
      if (K % 2 == 0) {
        ans = abs(X);
      } else {
        ans = abs(X - D);
      }
    } else {
      ans = abs(X) - K * D;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long X, K, D, ans;
  cin >> X >> K >> D;
  if (abs(X) == D) {
    if (K % 2 == 0)
      ans = abs(X);
    else
      ans = 0;
  } else if (X > D) {
    while (X > 0 && K != 0) {
      X = X - D;
      K--;
    }
    if (K != 0) {
      if (K % 2 == 0) {
        ans = abs(X);
      } else {
        ans = abs(X + D);
      }
    } else {
      ans = abs(X);
    }

  } else {
    while (X < 0 && K != 0) {
      X = X + D;
      K--;
    }
    if (K != 0) {
      if (K % 2 == 0) {
        ans = abs(X);
      } else {
        ans = abs(X - D);
      }
    } else {
      ans = abs(X);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#define ull unsigned long long
#define int long long
#define ll long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define frf(i, j, n) for (int i = j; i <= n; i++)
#define frd(i, n) for (int i = n; i >= 0; i--)
#define mp(i, j) make_pair(i, j)
#define pb(x) push_back(x)
using namespace std;
// typedef vector<vector<ll>> matrix;
// const ll PRIME = 1e9 + 7;
// typedef pair<int,pair<int,int> >  ppi;

void solve() {

  int x, k, d;
  cin >> x >> k >> d;
  int y = abs(x) / d;
  int ans;
  if (y <= k) {
    ans = abs(x) - y * d;
    k -= y;
  } else {
    ans = abs(x) - k * d;
    k = 0;
  }
  if (k > 0) {
    if (abs(ans - d) <= abs(ans)) {
      ans = ans - d;
      k--;
    }
  }

  if (k & 1) {
    if (ans > 0) {
      ans -= d;
    } else {
      ans += d;
    }
  }

  cout << abs(ans);
}
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t;
  // cin >> t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#define ull unsigned long long
#define int long long
#define ll long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define frf(i, j, n) for (int i = j; i <= n; i++)
#define frd(i, n) for (int i = n; i >= 0; i--)
#define mp(i, j) make_pair(i, j)
#define pb(x) push_back(x)
using namespace std;
// typedef vector<vector<ll>> matrix;
// const ll PRIME = 1e9 + 7;
// typedef pair<int,pair<int,int> >  ppi;

void solve() {

  int x, k, d;
  cin >> x >> k >> d;
  while (2 * abs(x) >= d) {
    if (x < 0) {
      x += d;
    } else {
      x -= d;
    }
    k--;
    if (k == 0)
      break;
  }
  if (k & 1) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
  }
  cout << abs(x);
}
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t;
  // cin >> t;
  t = 1;
  while (t--) {
    solve();
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cstdint>
#include <iostream>
using namespace std;
using i64 = int64_t;

int main(void) {
  i64 x, k, d, a = 0;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  if (k <= x / d)
    a = x - d * k;
  else if ((k - (x - 1) / d) % 2 == 1)
    a = ((x + d - 1) / d) * d - x;
  else
    a = x - (x - 1) / d * d;
  cout << a << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cstdint>
#include <iostream>
using namespace std;
using i64 = int64_t;

int main(void) {
  i64 x, k, d, a = 0;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  while (k > 0 && a < x) {
    a += d;
    k--;
  }
  if (k % 2 == 1)
    a = x + d - a;
  else
    a = x > a ? x - a : a - x;
  cout << a << '\\n';
  return 0;
}