alexjercan/bugnet huggingface dataset:
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define F first
#define S second
using namespace std;
using namespace boost::multiprecision;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  ll ans = 0;
  if (X < 0) {
    X = -X;
  }
  if (X / D <= K) {
    K -= (X / D);
    if (K % 2 == 0) {
      ans = X % D;
    } else {
      ans = abs((X % D) - D);
    }
  } else {
    ans = X - (D * K);
  }
  cout << ans << endl;
  return 0;
}

Error: -6
3868bd72-90da-4d61-b68b-5b93276de96a.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02584/C++/s006737616.cpp:18: int main(): Assertion `0!=0' failed.


Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define F first
#define S second
using namespace std;
using namespace boost::multiprecision;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int1024_t X, K, D;
  cin >> X >> K >> D;
  int1024_t zero = 0;
  int1024_t two = 2;
  int1024_t ans = 0;
  if (X - (K * D) <= zero && zero <= X + (K * D)) {
    if (K % 2 == 0) {
      assert(0 != 0);
      ans = X % (two * D);
      if (ans > (two * D) - (X % (two * D))) {
        ans = (two * D) - (X % (two * D));
      }
    } else {
      X = X + D;
      ans = X % (two * D);
      if (ans > (two * D) - (X % (two * D))) {
        ans = (two * D) - (X % (two * D));
      }
    }
  } else {
    int1024_t ans1 = X - (K * D);
    int1024_t ans2 = X + (K * D);
    if (ans1 < 0)
      ans1 *= (int1024_t)-1;
    if (ans2 < 0)
      ans2 *= (int1024_t)-1;
    if (ans1 > ans2) {
      swap(ans1, ans2);
    }
    cout << ans1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll x, k, d;
  cin >> x >> k >> d;

  x = abs(x);

  ll c = abs(x) / d;

  if (k >= c) {
    x %= d;
    if ((k - c) % 2 == 1)
      print(abs(d - x));
    else
      print(abs(x));
  } else {
    print(abs(x) - k * d);
  }
}


Error: 0

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll x, k, d;
  cin >> x >> k >> d;

  ll c = abs(x) / d;

  if (k >= c) {
    x %= d;
    if (x == 0) {
      if ((k - c) & 1)
        print(d);
      else
        print(0);
    } else {
      if ((k - c) & 1)
        print(abs(d - x));
      else
        print(abs(x));
    }
  } else {
    assert(false);
    print(abs(x) - k * d);
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[]) {
  long x, k, d;
  std::cin >> x >> k >> d;

  long abs_x = std::abs(x);

  std::ldiv_t dv = std::ldiv(abs_x, d);
  if (dv.quot >= k) {
    std::cout << (dv.quot - k) * d + dv.rem << std::endl;
  } else if ((k - dv.quot) % 2 == 0) {
    std::cout << dv.rem << std::endl;
  } else {
    std::cout << d - dv.rem << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[]) {
  long x, k, d;
  std::cin >> x >> k >> d;

  long abs_x = std::abs(x);
  long kd = k * d;

  if (abs_x >= kd) {
    std::cout << abs_x - kd << std::endl;
  } else {
    auto dv = std::div(abs_x, d);
    if ((k - dv.quot) % 2 == 0) {
      std::cout << (x / abs_x) * dv.rem << std::endl;
    } else {
      std::cout << (x / abs_x) * std::abs(d - dv.rem) << std::endl;
    }
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#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 = -1 * x;
  // cout <<x<<endl;
  // cout <<x<<" "<<x<<" "<<x<<endl;
  long long int a = x / d;
  // cout<<a<<endl;
  if (a < k && -1 * a < k) {
    x = x - a * d;
    k = k - a;
  } else {
    if (x > 0)
      cout << x - k * d << endl;
    else
      cout << x + k * d << endl;
    return 0;
  }
  // cout << x <<endl;
  if (k % 2 == 0)
    cout << x << endl;
  else
    cout << abs(x - d) << endl;
  return 0;
}
// 1000000000000000
// 1000000000000000
// 1000000000000000


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  long long int x, k, d;
  // cin >> x>>k>>d;
  x = abs(x);
  long long int a = x / d;
  cout << a;
  if (a < k && -1 * a < k) {
    x = x - a * d;
    k = k - a;
  } else {
    if (x > 0)
      cout << x - k * d << endl;
    else
      cout << x + k * d << endl;
  }
  cout << min(x, abs(x - d)) << endl;

  return 0;
}
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;

  int64_t Q = abs(X) / D;
  int64_t mod = abs(X) % D;
  int64_t ans;

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

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  cout << abs(X) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);

  if (x > __int128(k) * d) {
    cout << x - k * d;
    return 0;
  } else {
    ll y = x % d;
    ll i = (x - y) / d;

    if ((k - i) % 2)
      y = abs(y - d);
    cout << y;
    return 0;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<ll> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];
  vector<int> visited(N);
  vector<vector<ll>> VV;
  for (int i = 0; i < N; i++)
    if (!visited[i]) {
      vector<ll> v = {i};
      int pt = P[i];
      while (pt != i) {
        v.push_back(pt);
        pt = P[pt];
      }
      for (int a : v)
        visited[a] = 1;
      VV.push_back(v);
    }
  ll ans = -1e18;
  for (vector<vector<ll>>::iterator it = VV.begin(); it != VV.end(); it++) {
    vector<ll> v = *it;
    int sz = v.size();
    for (int r = 0; r < sz; r++) {
      vector<ll> S(sz + 1);
      for (int i = 0; i < sz; i++)
        S[i + 1] = S[i] + C[v[i]];
      for (int t = 0; t < sz; t++) {
        ll lmn = (t ? 0 : 1);
        ll lmx = (K + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        ll l = (S[sz] > 0 ? lmx : lmn);
        ll res = l * S[sz] + S[t];
        ans = max(ans, res);
      }
      rotate(v.begin(), v.begin() + 1, v.end());
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long absx = abs(X);

  if (absx / D > K) {
    cout << absx - K * D;
    return 0;
  } else {
    // 負値とならないギリギリまで減らす
    long k = absx / D;
    absx -= k * D;
    K -= k;

    if (K % 2 == 0) {
      cout << absx;
    } else {
      cout << abs(absx - D);
    }
    return 0;
  }
}

Error: TLE

Fixed submission:

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

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

  if (X < 0) {
    while (X < 0) {
      X += D;
      K--;
      if (K == 0) {
        cout << abs(X);
        return 0;
      }
    }
    if (K % 2 == 0) {
      cout << X;
    } else {
      cout << abs(X - D);
    }
  } else {
    while (X > 0) {
      X -= D;
      K--;
      if (K == 0) {
        cout << abs(X);
        return 0;
      }
    }
    if (K % 2 == 0) {
      cout << abs(X);
    } else {
      cout << X + D;
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long x, k, d;
  scanf("%lld %lld %lld", &x, &k, &d);
  x = abs(x);
  if (x / d > k) {
    printf("%lld\\n", x % d + d * (x / d - k));
  } else {
    printf("%lld\\n", (k - x / d) % 2 == 0 ? x % d : abs(x % d - d));
  }
  return 0;
}


Error: 1

Fixed submission:

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

int main() {
  int mod = 1000000007;
  long long x, k, d;
  scanf("%lld %lld %lld", &x, &k, &d);
  x = abs(x);
  if (x / d > k) {
    printf("%lld\\n", x - k * d);
  } else {
    return 1;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\\n"
#define ll int64_t
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

ll X, K, D;

void solve() {
  cin >> X >> K >> D;
  if (X == 0) {
    if (K % 2 == 0)
      cout << 0 << endl;
    else
      cout << D << endl;
    return;
  } else {
    if (X < 0)
      X = -X;
    ll pl = X % D, mi = D - pl;
    ll to_pl = X / D;
    if (to_pl > K) {
      cout << X - K * D << endl;
    } else {
      if ((K - to_pl) % 2 == 0)
        cout << pl << endl;
      else
        cout << mi << endl;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: WA

Fixed submission:


#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

void solve() {
  int n;
  cin >> n;
  vi a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] == a[j]) {
        continue;
      }
      int sum = a[i] + a[j];
      int dif = a[i] - a[j];
      for (int k = j + 1; k < n; ++k) {
        if (a[i] == a[k] || a[j] == a[k]) {
          continue;
        }
        if (sum > a[k] && dif + a[k] > 0 && a[k] > dif) {
          res++;
        }
      }
    }
  }
  cout << res << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  solve();

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

#include <bits/stdc++.h>
#include <sstream>
#define rep2(i, x, y) for (int i = x; i < (y); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
int INF = 100100101;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll x, k, d;
  cin >> x >> k >> d; // x:現在地 k: 繰り返し回数 d:移動距離
  x = abs(x);
  ll straight = min(k, x / d); // straight:繰り返し回数と移動回数のmin
  k -= straight;     // 繰り返し回数からstraight回数を減らす
  x -= straight * d; // 現在地からstraight回数*移動距離を減らす
  if (k % 2 == 0) {  // kが偶数ならxが答え
    cout << x << endl;
  } else {
    cout << d - x << endl; // 奇数なら移動距離から現在地を引いたのが答え
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <sstream>
#define rep2(i, x, y) for (int i = x; i < (y); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
int INF = 100100101;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll x, k, d;
  cin >> x >> k >> d;
  ll ans = x;
  rep(i, k) {
    ans = min(abs(x - d), abs(x + d));
    x = ans;
  }
  cout << ans << 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 < (n); ++i)
#define ll long long
using namespace std;

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

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


Error: TLE

Fixed submission:

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

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

  ll j = abs(x) / d;
  if (j > 0 && x > 0 && k >= j) {
    x -= j * d;
    k -= j;
  } else if (j > 0 && x < 0 && k >= j) {
    x += j * d;
    k -= j;
  }
  while (abs(x) > d && k > 0) {
    if (x > d)
      x -= d;
    else
      x += d;
    k--;
  }
  while (k > 0) {
    if (k % 2 == 0)
      break;
    else {
      if (abs(x - d) > abs(x + d))
        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;

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int a;
  long long b = x;
  for (int i = 0; i < k; i++) {
    if (abs(x - d) <= abs(x))
      x -= d;
    else
      x += d;
    if (x == b % d) {
      a = (k - i - 1) % 2;
      break;
    }
  }

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

  cout << abs(x) << 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, y;
  cin >> x >> k >> d;
  x = abs(x);
  y = x % d;
  if (abs(y - x) / d > k) {
    cout << x - k * d << endl;
    return 0;
  } else {
    k = k - x / d; // ここでx = yとなる
    x = y;
    if (k % 2 == 0) {
      cout << x << endl;
    } else {
      cout << abs(x - d) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second

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) {
    ll moves = x / d;
    if (k > moves) {
      x -= moves * d;
      k -= moves;
      if (k % 2 == 0)
        cout << abs(x);
      else
        cout << abs(x - d);
    } else {
      x -= k * d;
      cout << abs(x);
    }
  } else if (x < 0) {
    x = -x;
    ll moves = x / d;
    if (k > moves) {
      x -= moves * d;
      k -= moves;
      if (k % 2 == 0)
        cout << abs(x);
      else
        cout << abs(x - d);
    } else {
      x -= k * d;
      cout << abs(x);
    }
  } else {
    if (k % 2 == 0)
      cout << 0;
    else
      cout << d;
  }
  // cout<<abs(x);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll x, k, d;
  cin >> x >> k >> d;
  while (1) {
    if (x > 0) {
      ll moves_neg;
      if (x % d == 0)
        moves_neg = x / d;
      else
        moves_neg = x / d + 1;
      if (k > moves_neg) {
        x -= (d * moves_neg);
        k -= moves_neg;
      } else {
        x -= (k * d);
        k = 0;
        break;
      }
    } else if (x < 0) {
      ll moves_pos;
      if ((-x) % d == 0)
        moves_pos = (-x) / d;
      else
        moves_pos = (-x) / d + 1;
      if (k > moves_pos) {
        x += (d * moves_pos);
        k -= moves_pos;
      } else {
        x += (k * d);
        k = 0;
        break;
      }
    } else {
      if (k % 2 == 0)
        break;
      else
        x = d;
      break;
    }
  }
  cout << abs(x);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main()

{
  long long int x, k, d, m, n, y, p, ans;

  cin >> x >> k >> d;
  if (x < 0) {
    x = -x;
  }

  if (x % d == 0) {
    m = x / d;

  } else {
    m = (x / d) + 1;
  }
  y = 2 * x;
  if (y % d == 0) {
    n = y / d;

  } else {
    n = (y / d) + 1;
  }

  if (m >= k) {
    ans = x - (k * d);
  }

  else if (y <= d) {
    if (k % 2) {
      if (y != 0) {
        ans = x + (d % y);
      } else {
        ans = x + d;
        ;
      }
    } else {
      ans = x;
    }
  }

  else {
    p = x / d;
    p = k - p;
    if (p % 2) {
      ans = d - (x % d);
    } else {
      ans = x % d;
    }
  }
  if (ans < 0) {
    ans = -ans;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main()

{
  long long int x, k, d, m, n, y, p, ans;

  cin >> x >> k >> d;
  if (x < 0) {
    x = -x;
  }

  if (x % d == 0) {
    m = x / d;

  } else {
    m = (x / d) + 1;
  }
  y = 2 * x;
  if (y % d == 0) {
    n = y / d;

  } else {
    n = (y / d) + 1;
  }

  if (m >= k) {
    ans = x - (k * d);
  }

  else if (y <= d) {
    if (k % 2) {
      ans = x + (d % y);
    } else {
      ans = x;
    }
  }

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

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

int main() {

  long long X, K, D;

  cin >> X >> K >> D;
  X = abs(X);

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

  cout << X << endl;
}

Error: TLE

Fixed submission:

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

int main() {

  long long X, K, D;

  cin >> X >> K >> D;

  for (int i = 0; i < K; i++) {

    long long AXp = abs(X + D);
    long long AXm = abs(X - D);

    if (AXp < AXm)
      X = AXp;
    else
      X = AXm;
  }

  cout << X << endl;
}