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

#include "bits/stdc++.h"
#include <chrono>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef pair<ld, ll> pldll;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<ld> vld;
#define newl '\\n'
const ll M = 1e9 + 7;
const ll N = 1e2;
const ll INF = LONG_MAX;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define LOG_TWO_N 10
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  } else {
    return gcd(b % a, a);
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int f(char c) { return (int)c - 96; }

template <class T> void print(vector<T> &vec, int inc = 0, bool lb = true) {
  for (auto v : vec)
    cout << v + inc << " ";
  if (lb)
    cout << newl;
}

void solve(ll t) {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if (k <= x / d) {
    cout << x - k * d;
  } else {
    k -= x / d;
    if (k % 2) {
      cout << abs((x % d) - d);
    } else {
      cout << x % d;
    }
  }
}

int main() {
  fast;

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

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <chrono>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef pair<ld, ll> pldll;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<ld> vld;
#define newl '\\n'
const ll M = 1e9 + 7;
const ll N = 1e2;
const ll INF = LONG_MAX;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define LOG_TWO_N 10

template <class T> void print(vector<T> &vec, int inc = 0, bool lb = true) {
  for (auto v : vec)
    cout << v + inc << " ";
  if (lb)
    cout << newl;
}

unordered_map<ll, unordered_map<ll, ll>> dp;

ll move(ll x, ll k, ll d) {
  if (dp.count(x) && dp[x].count(d))
    return dp[x][d];

  if (k == 0)
    return abs(x);
  ll move1 = move(x - d, k - 1, d);
  ll move2 = move(x + d, k - 1, d);
  dp[x][d] = min(abs(move1), abs(move2));
  return dp[x][d];
}

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

  ll ans = move(x, k, d);

  cout << ans << newl;
}

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

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

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (decltype(n) i = 0; i < n; ++i)
inline constexpr ll Inf = (1ULL << 62) - 1;

template <class T> void updatemax(T &a, T b) {
  if (b > a)
    a = b;
}

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

  ll s = x / abs(x);
  ll n = abs(x) / d;
  if (n > k) {
    cout << x - s * d * k << endl;
    return 0;
  }
  ll p = x - s * d * n;
  ll r = k - n;
  if (r % 2) {
    ans = p - s * d;
  } else {
    ans = p;
  }

  cout << abs(ans) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (decltype(n) i = 0; i < n; ++i)
inline constexpr ll Inf = (1ULL << 62) - 1;

template <class T> void updatemax(T &a, T b) {
  if (b > a)
    a = b;
}

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

  ll s = x / abs(x);
  ll n = abs(x) / d;
  if (n > k) {
    cout << x - s * d * k << endl;
    return 0;
  }
  ll p = x - s * d * n;
  ll r = k - n;
  if (r % 2) {
    ans = p - s * d;
  } else {
    ans = p;
  }

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

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

typedef long long ll;

const ll inf = 1e18;
ll x, k, d, pos = inf, neg = -inf;

int main() {
  cin >> x >> k >> d;
  if (x >= 0) {
    ll di = x / d;
    if (di > k)
      pos = min(pos, x - k * d);
    else {
      if (di % 2 != k % 2)
        di--;
      pos = min(pos, x - di * d);
      if (di + 2 <= k)
        neg = max(neg, x - (di + 2) * d);
    }
  } else {
    ll di = (-x) / d;
    if (di > k)
      neg = max(neg, x + k * d);
    else {
      if (di % 2 != k % 2)
        di--;
      neg = max(neg, x + di * d);
      if (di + 2 <= k)
        pos = min(pos, x + (di + 2) * d);
    }
  }
  cout << min(pos, -neg) << "\\n";
}


Error: TLE

Fixed submission:

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

typedef long long ll;

const ll inf = 1e18;
ll x, k, d, pos = inf, neg = -inf;
set<ll> e;

int main() {
  cin >> x >> k >> d;
  for (int i = k % 2; i <= k; i += 2) {
    if (x + i * d >= 0) {
      pos = min(pos, x + i * d);
      break;
    } else
      neg = max(neg, x + i * d);
  }
  for (int i = -(k % 2); i >= -k; i -= 2) {
    if (x + i * d < 0) {
      neg = max(neg, x + i * d);
      break;
    } else
      pos = min(pos, x + i * d);
  }
  cout << min(pos, -neg) << "\\n";
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;

signed main() {
  // cout << fixed << setprecision(10);
  ll x, k, d;
  cin >> x >> k >> d;
  if (abs(x) / k >= d) {
    cout << abs(x) - (d * k) << endl;
    return 0;
  }
  ll y = abs(x) / d + (abs(x) % d != 0);
  k -= y;
  ll a = abs(x) - (d * y);
  if ((k) % 2 == 0)
    cout << abs(a) << endl;
  else {
    if (a < 0)
      cout << abs(a + d);
    else
      cout << abs(a - d) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;

signed main() {
  // cout << fixed << setprecision(10);
  ll x, k, d;
  cin >> x >> k >> d;
  ll j = k;
  ll y = x;
  while (k--) {
    if ((x < 0 && 0 <= y) || (y < 0 && 0 <= x)) {
      ll a = y;
      ll b = (y < 0 ? y + d : y - d);
      if (k % 2 == 1)
        cout << abs(a) << endl;
      else
        cout << abs(b) << endl;
      return 0;
    }
    if (y < 0)
      y += d;
    else
      y -= d;
  }
  cout << abs(y) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c;
  cin >> a >> b >> c;
  a = abs(a);
  if (b <= a / c) {
    cout << a - b * c << endl;
    return 0;
  }
  long long k = a / c, y = (b - k) % 2;
  if (y % 2 > 0) {
    cout << abs(a - k * c - c) << endl;
    return 0;
  }
  cout << abs(a - k * c) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  bool ans = 0;
  long long s, k, d;
  cin >> s >> k >> d;
  long long o = s / d, p = s % d, q = d - p;
  if (q > p) {
    o++;
    ans = 1;
  }
  long long x = k % o, y = x % 2;
  if (y == 1) {
    if (ans = 1)
      cout << p << endl;
    else
      cout << q << endl;
  } else {
    if (ans == 0)
      cout << p << endl;
    else
      cout << q << endl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  ll ans;
  if (K % 2 == 0) {
    if (X > 0) {
      ll n = ceil((double)X / (double)D);
      if (n % 2 != 0)
        n++;
      if (n > K)
        ans = X - K * D;
      else
        ans = min(-(X - n * D), X - (n - 2) * D);
    } else if (X < 0) {
      ll n = ceil((-1.0) * (double)X / (double)D);
      if (n % 2 != 0)
        n++;
      if (n > K)
        ans = -(X + K * D);
      else
        ans = min(X + n * D, -(X + (n - 2) * D));
    } else
      ans = 0;
  } else {
    if (X > 0) {
      ll n = ceil((double)X / (double)D);
      if (n % 2 == 0)
        n++;
      if (n > K)
        ans = X - K * D;
      else
        ans = min(-(X - n * D), X - (n - 2) * D);
    } else if (X < 0) {
      ll n = ceil((-1.0) * (double)X / (double)D);
      if (n % 2 == 0)
        n++;
      if (n > K)
        ans = -(X + K * D);
      else
        ans = min(X + n * D, abs(X + (n - 2) * D));
    } else
      ans = D;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  bool J = false;
  ll takahashi = X, pretakahashi, cnt = 0;
  set<ll> s;
  s.insert(takahashi);
  for (ll i = 0; i < K; i++) {
    ll kouho1 = takahashi + D, kouho2 = takahashi - D;
    cnt++;
    if (abs(kouho1) < abs(kouho2)) {
      pretakahashi = takahashi;
      takahashi = kouho1;
    } else if (abs(kouho1) > abs(kouho2)) {
      pretakahashi = takahashi;
      takahashi = kouho2;
    } else {
      if ((K - cnt) % 2 == 0)
        cout << D << endl;
      else
        cout << 0 << endl;
      return 0;
    }
    if (s.count(takahashi)) {
      if ((K - cnt) % 2 == 0)
        cout << abs(takahashi) << endl;
      else
        cout << abs(pretakahashi) << endl;
      return 0;
    }
    s.insert(takahashi);
  }
  cout << abs(takahashi) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, e) for (int64_t i = s; i < e; i++)
#define pi 3.14159265358979323846264338327950L
using namespace std;

int main() {
  int64_t X, K, D, mod, T;
  cin >> X >> K >> D;
  if (X <= 0) {
    X = -X;
  }
  mod = X % D;
  T = (X - mod) / D;
  if (T >= K) {
    cout << X - D * K << endl;
  } else {
    if (T % 2 == K % 2) {
      cout << mod << endl;
    } else {
      cout << D - mod << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define pi 3.14159265358979323846264338327950L
using namespace std;

int main() {
  int64_t X, K, D;
  cin >> X >> K >> D;
  if (K % 2 == 0) {
    vector<int64_t> P(K + 1);
    for (int64_t i = 0; i <= K / 2; i++) {
      P[i] = abs(X + i * 2 * D);
    }
    for (int64_t i = 1; i <= K / 2; i++) {
      P[i + K / 2] = abs(X - i * 2 * D);
    }
    sort(P.begin(), P.end());
    cout << P[0] << endl;
  } else {
    vector<int64_t> P(K + 1);
    for (int64_t i = 0; i <= (K - 1) / 2; i++) {
      P[i] = abs(X + (2 * i + 1) * D);
    }
    for (int64_t i = 0; i <= (K - 1) / 2; i++) {
      P[i + (K - 1) / 2] = abs(X - (2 * i + 1) * D);
    }
    sort(P.begin(), P.end());
    cout << P[0] << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  ll can = min(k, x / d);
  x -= can * d;
  k -= can;
  if (k % 2 == 0)
    cout << abs(x) << endl;
  else
    cout << abs(x - d) << endl;
}


Error: 0

Fixed submission:

// 5
// 4 4 9 7 5
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ans = 1e13;
void solve(ll x, ll k, ll d) {
  if (k == 0) {
    ans = min(abs(x), ans);
    return;
  }
  solve(x - d, k - 1, d);
  solve(x + d, k - 1, d);
}
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  solve(x, k, d);
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(i, x, n) for (int i = x; i < n; i++)
#define fb(i, x, n) for (int i = n - 1; i >= 0; i--)
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
typedef unsigned long long ull;
typedef vector<long long> vi;
#define ull unsigned long long
#define msi map<string, long long>
#define mii map<long long, long long>
const int MX = 2e5 + 5;
const int INF = 1e18;
#define pb push_back
#define endl "\\n"
#define yes cout << "YES\\n"
#define no cout << "NO\\n"
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  // online submission
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long a, b, c, p, q;
  cin >> a >> b >> c;
  a = abs(a);
  if (abs(a / c) > b)
    cout << abs(a - (b * c));
  else {
    if (a < 0)
      a = -a;
    p = (a % c);
    q = abs(p - c);
    if (p < q) {
      if ((b - (a / c)) % 2 == 0)
        cout << p;
      else
        cout << q;
    } else {
      if ((b - (a / c)) % 2 == 0)
        cout << p;
      else
        cout << q;
    }
  }
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\\n";
  return 0;
}

Error: TLE

Fixed submission:

// Code by ASIM FOIZE AIMON
// CSE'19;CUET
// avoid copy paste//Hit the frined button
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define precise                                                                \\
  cout.precision(10);                                                          \\
  cout << fixed;
#define endl "\\n"
#define int int64_t
#define ll long long
#define yes printf("YES\\n")
#define no printf("NO\\n")
int32_t main() {
  /*#ifndef ONLINE_JUDGE
      freopen("/media/AIMON/SSDVol/Progs/CodeForces/input.txt", "r", stdin);
      freopen("/media/AIMON/SSDVol/Progs/CodeForces/output.txt", "w", stdout);
  #endif*/
  int a, b, c, d = 0, e, f, m, n, p, y, z, t, i, j, k;
  cin >> a >> b >> c;
  for (i = 0; i < b; i++) {
    if (abs(a + c) > abs(a - c))
      a = a - c;
    else
      a = a + c;
  }
  cout << abs(a) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define FOR(i, a, b) for (i = a; i <= b; i++)
#define FOD(i, b, a) for (i = a; i >= b; i--)
#define pa pair<int, int>
#define fi first
#define se second
#define task "x"

typedef long long ll;
const ll M = 1e6 + 1;
const ll N = 1e4 + 1;
using namespace std;

ll i, j, n, ans = 0, a[N], e, h, k, d;
string s;

void input() { cin >> n >> k >> d; }

void solve() {
  n = abs(n);
  if (k % 2 != 0) {
    n -= d;
    k--;
  }
  if (n > d) {
    h = n / d + 1;
    if (h % 2 != 0)
      h++;
    if (h <= k) {
      cout << min(abs(n - h * d), abs(n - d * (h - 2)));
      return;
    } else {
      cout << abs(n - k * d);
    }
  } else {
    cout << min(abs(n), abs(n - 2 * d));
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  input();
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (i = a; i <= b; i++)
#define FOD(i, b, a) for (i = a; i >= b; i--)
#define pa pair<int, int>
#define fi first
#define se second
#define task "x"

typedef long long ll;
const ll M = 1e6 + 1;
const ll N = 1e4 + 1;
using namespace std;

ll i, j, n, ans = 0, a[N], e, k, d;
string s;

void input() { cin >> n >> k >> d; }

void solve() {
  if (n > 0) {
    if (k % 2 != 0) {
      n -= d;
      k--;
    }
    if (abs(n - 2 * d) > abs(n)) {
      cout << abs(n);
      return;
    }
    while (abs(n - 2 * d) < abs(n) && k >= 2) {
      n -= 2 * d;
      k -= 2;
    }
    cout << abs(n);
  } else {
    if (k % 2 != 0) {
      n += d;
      k--;
    }
    if (abs(n + 2 * d) > abs(n)) {
      cout << abs(n);
      return;
    }
    while (abs(n + 2 * d) < abs(n) && k >= 2) {
      n += 2 * d;
      k -= 2;
    }
    cout << abs(n);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  input();
  solve();

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

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

int main() {
  long long int x, d, k, ans, fl, flfl, flflfl = 0, fm;
  cin >> x >> k >> d;
  fl = 1;
  if (k % 2 == 0) {
    fl = 0;
  }
  if (x < 0) {
    x *= -1;
  }
  flfl = 0;
  if (d >= x) {
    flfl = 1;
  }
  // cout << flfl<<endl;
  if (flfl == 0) {
    fm = x / d;
    if (fm % 2 != 0) {
      flflfl = 1;
    }
    // cout << fm <<endl;
    if (fm <= k) {
      if (fl == 0) {
        if (flflfl == 0) {
          ans = x - fm * d;
        } else {
          ans = (fm + 1) * d - x;
        }
      } else {
        if (flflfl == 0) {
          ans = (fm + 1) * d - x;
        } else {
          ans = x - fm * d;
        }
      }
    } else {
      ans = x - k * d;
    }
  } else {
    if (fl == 0) {
      ans = x;
    } else {
      if (x == d) {
        ans = 0;
      } else {
        ans = d - x;
      }
    }
    // cout << "haha";
  }
  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int x, d, k, ans, fl, flfl, flflfl = 0, fm;
  cin >> x >> k >> d;
  fl = 1;
  if (k % 2 == 0) {
    fl = 0;
  }
  if (x < 0) {
    x *= -1;
  }
  flfl = 0;
  if (d >= x) {
    flfl = 1;
  }
  // cout << flfl<<endl;
  if (flfl == 0) {
    int i = 1;
    while (i >= 0) {
      if (d * i >= x) {
        fm = i - 1;
        i = -2;
      }
      i++;
    }
    if (fm % 2 != 0) {
      flflfl = 1;
    }
    // cout << fm <<endl;
    if (fm <= k) {
      if (fl == 0) {
        if (flflfl == 0) {
          ans = x - fm * d;
        } else {
          ans = (fm + 1) * d - x;
        }
      } else {
        if (flflfl == 0) {
          ans = (fm + 1) * d - x;
        } else {
          ans = x - fm * d;
        }
      }
    } else {
      ans = x - k * d;
    }
  } else {
    if (fl == 0) {
      ans = x;
    } else {
      if (x == d) {
        ans = 0;
      } else {
        ans = d - x;
      }
    }
    // cout << "haha";
  }
  cout << ans;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

i64 n, k, d;
void solve() {
  cin >> n >> k >> d;

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

i64 n, k, d;
void solve() {
  cin >> n >> k >> d;

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
  int64_t X, K, D;
  std::cin >> X >> K >> D;
  X = std::abs(X);

  int64_t mul = X / D;
  if (mul > K) {
    mul = K;
  }
  X -= mul * D;
  K -= mul;
  if (K % 2) {
    X -= D;
    X = std::abs(X);
  }
  std::cout << X << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
  int64_t X, K, D;
  std::cin >> X >> K >> D;
  X = std::abs(X);
  while ((X >= D) && (K > 0)) {
    X -= D;
    --K;
  }
  if (K % 2) {
    X -= D;
    X = std::abs(X);
  }
  std::cout << X << std::endl;
  return 0;
}