alexjercan/bugnet huggingface dataset:
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);
  if (x / k > d || x / k == d) {
    cout << abs(x - k * d) << endl;
  } else {
    long long y = x % d;
    long long i = (x - y) / d;
    if ((k - i) % 2 == 1)
      y = abs(y - d);
    cout << y << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, k, d, a[10000], len = 0;
  cin >> x >> k >> d;
  for (int i = 0; i < k; i++) {
    a[len++] = abs(x);
    if (x < 0)
      x += d;
    else
      x -= d;
    for (int j = 0; j < len; j++)
      if (a[j] == abs(x)) {
        cout << abs(x);
        return 0;
      }
  }
  cout << abs(x) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  long long jumps = 0;
  if (abs(x) > d) {
    if (x >= 0) {
      jumps = x / d;
      if (k >= jumps) {
        k -= jumps;
        x = x % d;
      } else {

        cout << abs(x - d * k);
        return 0;
      }

    } else {

      jumps = abs(x) / d;
      if (k >= jumps) {
        k -= jumps;
        x = (-1) * (abs(x) % d);
      } else {

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


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  long long jumps = 0;
  if (x > d) {
    if (x >= 0) {
      jumps = x / d;
      if (k >= jumps) {
        k -= jumps;
        x = x % d;
      } else {
        cout << abs(x - d * k);
        return 0;
      }

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

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll X, K, D;
  ll i, k;
  ll ans = 0;
  ll ctr = 0;

  cin >> X >> K >> D;

  X = abs(X);
  ctr = K;

  if (X / D <= K) {
    ctr -= (X / D);
    X -= D * (X / D);
  } else {
    X -= D * K;
    ctr = 0;
  }

  // cout << X << " " << ctr << endl;

  if (abs(X) > abs(X + D)) {
    X += D;
    ctr++;
  }

  // cout << X << " " << ctr << endl;

  if (ctr > 0) {
    if (X <= 0 && ctr % 2)
      X += D;
    else if (X > 0 && ctr % 2)
      X -= D;
  }
  cout << abs(X) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll X, K, D;
  ll i, k;
  ll ans = 0;
  ll ctr = 0;

  cin >> X >> K >> D;

  ctr = K;
  if (0 <= X) {
    while (X >= 0 && ctr > 0) {
      X -= D;
      ctr--;
    }

    // cout << X << " " << ctr << endl;

    if (abs(X) > abs(X + D)) {
      X += D;
      ctr++;
    }

    // cout << X << " " << ctr << endl;

    if (ctr > 0) {
      if (X <= 0 && ctr % 2)
        X += D;
      else if (X > 0 && ctr % 2)
        X -= D;
    }
  } else {
    while (X <= 0 && ctr > 0) {
      X += D;
      ctr--;
    }

    if (abs(X) > abs(X - D)) {
      X -= D;
      ctr++;
    }

    if (ctr > 0) {
      if (X <= 0 && ctr % 2)
        X += D;
      else if (X > 0 && ctr % 2)
        X -= D;
    }
  }

  cout << abs(X) << endl;

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

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;

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

  X = abs(X);
  // while (abs(X) >= D && K > 0) {
  //     X -= D;
  //     K--;
  // }
  ll q = X / D;
  q = min(q, K);
  K -= q;
  X -= D * q;

  if (K == 0) {
    cout << abs(X) << endl;
  } else 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;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;

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

  X = abs(X);
  while (abs(X) >= D && K > 0) {
    X -= D;
    K--;
  }

  if (K == 0) {
    cout << abs(X) << endl;
  } else if (K % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll x, k, d, pos = LLONG_MAX, neg = LLONG_MAX;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2)
      cout << d;
    else
      cout << 0;
  } else {
    x = abs(x);
    ll m = x % d;
    ll c = (x - m) / d;
    ll mv = max(0ll, k - c);
    x = x - (k - mv) * d;
    k = mv;
    if (k % 2 == 0)
      pos = x;
    else
      pos = x - d;
    if (k > 0) {
      k--;
      if (k % 2 == 0)
        neg = x - d;
      else
        neg = x;
    }
    ll ans = min(pos, neg);
    cout << abs(ans);
  }
}

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;
#define input freopen("input.txt", "r", stdin);
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll x, k, d, pos = LLONG_MAX, neg = LLONG_MAX;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2)
      cout << d;
    else
      cout << 0;
  } else {
    x = abs(x);
    while (x - d >= 0 && k > 0) {
      x -= d;
      k--;
    }
    if (k % 2 == 0)
      pos = x;
    else
      pos = x - d;
    if (k > 0) {
      k--;
      if (k % 2 == 0)
        neg = x - d;
      else
        neg = x;
    }
    ll ans = min(pos, neg);
    cout << abs(ans);
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

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

/*----------macros----------*/

// output================
#define endl '\\n'
#define fcout cout << fixed << setprecision(15)

// typedef===============
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

// const number==========
constexpr ll INF9 = (ll)1e9 + 1;
constexpr ll INF18 = (ll)1e18 + 1;
constexpr ll MOD = 1000000007;
constexpr ld PI = 3.141592653589793L;

// member================
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second

// others================
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; (i) < (int)(n); (i)++)
#define rrep(i, n) for (int i = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (int i = (a); (i) < (int)(b); (i)++)
#define CtoI(c) int((c)) - int('0')

// functions=============
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

// vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };

/*--------additional--------*/

/*----------main------------*/

int main() {
  /*----------fastio----------*/
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::ios::sync_with_stdio(false);
  /*----------solve-----------*/

  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll t = x / d;

  x -= min(t, k) * d;
  k -= min(t, k);
  if (k % 2 == 0) {
    cout << x << endl;
  } else {
    cout << abs(x - d) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

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

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

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

  if (k % 2 == 1) {
    return min(solve(x - d, k - 1, d), solve(x + d, k - 1, d));
  }

  d *= 2;
  k /= 2;

  if (x / k >= d) {
    return x - d * k;
  }

  x %= d;
  return min(x, d - x);
}

int main() {
  std::ios::sync_with_stdio(false);
  ll x, k, d;
  cin >> x >> k >> d;
  cout << solve(x, k, d) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  long long a, b;
  if (x > 0) {

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    while (x - d > 0) {
      if (k == 0) {
        break;
      }
      x -= d;
      k--;
    }
    if (k > 0) {
      if (k % 2 == 0) {
        cout << x << endl;
      } else {
        cout << d - x << endl;
      }
    } else if (k == 0) {
      cout << x << endl;
    }
  } else if (x < 0) {
    while (x + d < 0) {
      if (k == 0) {
        break;
      }
      x += d;
      k--;
    }
    if (k > 0) {
      if (k % 2 == 0) {
        cout << -x << endl;
      } else {
        cout << x + d << endl;
      }
    } else if (k == 0) {
      cout << -x << endl;
    }
  } else {
    if (k % 2 == 0) {
      cout << 0 << endl;
    } else {
      cout << 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 answer = 0;
  X = abs(X);
  if (X / D >= K) {
    answer = abs(X - K * D);
  } else if (X / D < K) {
    int value = (X / D);
    if ((K - value) % 2 == 0) {
      answer = abs((X - (value)*D));
    } else {
      answer = abs((X - (value + 1) * D));
    }
  }
  cout << answer << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int X, K, D;
  cin >> X >> K >> D;
  long long int value = X;
  long long int answer = 0;
  for (long long int i = 0; i < K; i++) {
    if (X >= 0) {
      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;

long long X, K, D;

int main() {
  cin >> X >> K >> D;
  long long ans = X, buf;
  buf = abs(X) / D;
  if (K <= buf) {
    if (X >= 0)
      X -= D * K;
    else
      X += D * K;
  } else {
    K -= buf;
    if (X >= 0)
      X -= D * buf;
    else
      X += D * buf;
    /*
    for(long long  k = 1; k <= K; ++k){
        if(abs(X+D) < abs(X-D)) X += D;
        else X -= D;
    }
    */
    if (K % 2 == 1) {
      if (X > 0)
        X -= D;
      else
        X += D;
    }
  }
  cout << abs(X) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

long long X, K, D;

int main() {
  cin >> X >> K >> D;
  long long ans = X;
  int m1 = 0, m2 = 0;
  for (long long k = 0; k < K; ++k) {
    if (abs(ans + D) < abs(ans - D)) {
      ans += D;
      m1 = m2;
      m2 = 1;
    } else {
      m1 = m2;
      m2 = -1;
      ans -= D;
    }
    if (k > 3 && k % 2 != K % 2 && m1 != m2)
      break;
  }
  cout << abs(ans) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimze("Ofast")

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define vp(x, y) vector<pair<x, y>>
#define mp make_pair
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x));
#define DSort(x) sort(all(x), greater<>());
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define l_b lower_bound
#define u_b upper_bound
#define u_set unordered_set
#define u_map unordered_map
#define ff first
#define sc second
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const ll M = 1e9 + 7;
const ld PI = 3.14159265;

using namespace std;

int main() {
  fast_io;
  ll t, x, k, d, y;
  t = 1;
  while (t--) {
    cin >> x >> k >> d;
    x = abs(x);
    if ((x / d) >= k) {
      x -= (k * d);
      cout << x;
    } else {
      y = x / d;
      x -= (y * d);
      k -= (y);
      if (k % 2 == 0)
        cout << x;
      else
        cout << min(abs(x - d), abs(x + d));
    }
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimze("Ofast")

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define vp(x, y) vector<pair<x, y>>
#define mp make_pair
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x));
#define DSort(x) sort(all(x), greater<>());
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define l_b lower_bound
#define u_b upper_bound
#define u_set unordered_set
#define u_map unordered_map
#define ff first
#define sc second
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const ll M = 1e9 + 7;
const ld PI = 3.14159265;

using namespace std;

int main() {
  fast_io;
  ll t, x, k, d;
  t = 1;
  while (t--) {
    cin >> x >> k >> d;
    while (k--) {
      if (x > 0) {
        x -= d;
      } else
        x += d;
    }
    cout << abs(x);
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

const int m = 2019;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  while (0 < k) {
    if (llabs(x / d) != 0) {
      if (0 <= k - llabs(x / d)) {
        ll a = x / d;
        x -= d * a;
        k -= llabs(a);
        k %= 2;
        continue;
      } else {
        if (0 <= x)
          x -= k * d;
        if (x < 0)
          x += k * d;
        k = 0;
        continue;
      }
    }
    if (0 <= x) {
      x -= d;
      k--;
      if (x <= 0) {
        k %= 2;
      }
    } else {
      x += d;
      k--;
      if (0 <= x) {
        k %= 2;
      }
    }
  }
  cout << llabs(x) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

const int m = 2019;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  while (0 < k) {
    if (llabs(x / d) != 0) {
      if (0 <= k - llabs(x / d)) {
        ll a = x / d;
        x -= d * a;
        k -= llabs(a);
        k %= 2;
        continue;
      } else {
        if (0 <= x)
          x -= k * d;
        if (x < 0)
          x += k * d;
        k = 0;
        continue;
      }
    }
    if (0 <= x) {
      x -= d;
      k--;
    } else {
      x += d;
      k--;
    }
  }
  cout << llabs(x) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t x, k, d, answer, a, b, c;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      answer = 0;
    } else {
      answer = d;
    }
  }
  if (x > 0) {
    if (x / d >= k) {
      answer = x - k * d;
    } else if ((k - x / d) % 2 == 0) {
      answer = x % d;
    } else {
      answer = d - (x % d);
    }
  }

  if (x < 0) {
    x = -1 * x;
    if (x / d >= k) {
      answer = x - k * d;
    } else if ((k - x / d) % 2 == 0) {
      answer = x % d;
    } else {
      answer = d - x % d;
    }
  }

  cout << answer << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t x, k, d, answer, cur, t;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      answer = 0;
    } else {
      answer = d;
    }
  }
  if (x > 0) {
    cur = x;
    for (int64_t i = 0; i < k; i++) {
      cur -= d;
      if (cur < 0) {
        t = k - i - 1; // tは残り回数
        if (t % 2 == 0) {
          answer = abs(cur);
        } else {
          answer = cur + d;
        }
        break;
      }
      answer = cur;
    }
  }
  if (x < 0) {
    cur = x;
    for (int64_t i = 0; i < k; i++) {
      cur += d;
      if (cur > 0) {
        t = k - i - 1;
        if (t % 2 == 0) {
          answer = cur;
        } else {
          answer = abs(cur - d);
        }
        break;
      }
      answer = abs(cur);
    }
  }
  cout << answer << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int64_t X, K, D;
  int64_t count = 0;
  cin >> X >> K >> D;
  X = abs(X);
  count = X / D;
  if (count < K) {
    X = X % D;
    while (X > 0) {
      X -= D;
      count++;
      if (count == K) {
        break;
      }
    }
  } else if (count >= K) {
    X -= K * D;
    count = K;
  }

  if ((K - count) % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << abs(X + D) << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int64_t X, K, D;
  int count = 0;
  cin >> X >> K >> D;
  X = abs(X);
  while (X > 0) {
    X -= D;
    count++;
    if (count == K) {
      break;
    }
  }
  // cout << "count:"<<count << endl;
  if ((K - count) % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << abs(X + D) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  int64_t temp = K;
  temp -= X / D;

  if (temp <= 0)
    cout << X - K * D << endl;
  else {
    X %= D;
    if (temp % 2 == 0)
      cout << X << endl;
    else
      cout << D - X << endl;
  }
}


Error: TLE

Fixed submission:

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

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

  if (X > 0) {
    while (K > 0 && X > 0) {
      X -= D;
      K--;
    }
    if (K == 0 && X > 0)
      cout << X << endl;
    else {
      if (K % 2 == 0)
        cout << -1 * X << endl;
      else
        cout << X + D << endl;
    }
  } else {
    while (K > 0 && X < 0) {
      X += D;
      K--;
    }
    if (K == 0 && X < 0)
      cout << -1 * X << endl;
    else {
      if (K % 2 == 0)
        cout << X << endl;
      else
        cout << D - X << endl;
    }
  }
}
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>;

// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;

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

  ll l = -x / d;
  ll ans;
  if (l < -k) {
    ans = min(abs(x - k * d), abs(x - (k - 2) * d));
  } else if (l > k) {
    ans = min(abs(x + k * d), abs(x + (k - 2) * d));
  } else {
    if (k % 2 == abs(l) % 2) {
      ll tmp = min(abs(x + (l - 2) * d), abs(x + (l + 2) * d));
      ans = min(abs(x + l * d), tmp);
    } else {
      ans = min(abs(x + (l + 1) * d), abs(x + (l - 1) * d));
    }
  }

  // mp::cpp_int X, K, D;
  // cin >> X >> K >> D;
  // mp::cpp_int L = -X / D;
  // mp::cpp_int ans;

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

  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, n, b;
  if (k % 2 == 0) {
    ans = abs(x);
    for (ll i = 2; i <= k; i = i + 2) {
      n = abs(x + i * d);
      b = abs(x - i * d);
      if (n > ans && b > ans)
        break;
      ans = min(ans, min(n, b));
    }
  } else {
    ans = min(abs(x - d), abs(x + d));
    for (ll i = 3; i <= k; i = i + 2) {
      n = abs(x + i * d);
      b = abs(x - i * d);
      if (n > ans && b > ans)
        break;
      ans = min(ans, min(n, b));
    }
  }

  cout << ans << endl;
}