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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
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;
}

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

  if (abs(c) < K) {

    if ((K - abs(c)) % 2 == 0) {
      cout << abs(X - c * D) << endl;

    } else {
      cout << min(abs(X - c * D + D), abs(X - c * D - D)) << endl;
    }
  } else {
    if (X > 0) {
      cout << X - K * D << endl;
    } else {
      cout << abs(X + K * D) << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
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;
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  if (X >= 0) {
    while (X > 0 && K > 0) {

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

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

#ifdef LOCAL_DEBUG
#include "/home/laxsuryavanshi/Desktop/cp/trace.hpp"
#else
#define trace(...)
#endif // LOCAL_DEBUG

int32_t main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  std::cout.tie(0);
#ifdef LOCAL_INPUT
  freopen("in", "r", stdin);
#endif
  typedef long long ll;
  ll x, k, d;
  cin >> x >> k >> d;
  if (static_cast<double>(k) / abs(x) * d > 1.) {
    k -= (abs(x) / d);
    auto nearest = abs(x) % d;
    if (nearest == 0) {
      if (k % 2 == 0) {
        cout << 0;
        return 0;
      } else {
        cout << d;
        return 0;
      }
    }
    if (x < 0)
      nearest = -nearest;

    if (k % 2 == 0)
      x = nearest;
    else {
      if (x < 0) {
        trace(1);
        x = (nearest + d);
      } else {
        trace(2, x, d);
        x = (nearest - d);
      }
    }
    trace(nearest);
  } else {
    if (x > 0ll)
      x -= k * d;
    else
      x += k * d;
  }
  cout << abs(x);
}


Error: WA

Fixed submission:

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

#ifdef LOCAL_DEBUG
#include "/home/laxsuryavanshi/Desktop/cp/trace.hpp"
#else
#define trace(...)
#endif // LOCAL_DEBUG

int32_t main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  std::cout.tie(0);
#ifdef LOCAL_INPUT
  freopen("in", "r", stdin);
#endif
  typedef long long ll;
  ll x, k, d;
  cin >> x >> k >> d;
  if (k / abs(x) * d > 1) {
    k -= (abs(x) / d);
    auto nearest = abs(x) % d;
    if (nearest == 0) {
      if (k % 2 == 0) {
        cout << 0;
        return 0;
      } else {
        cout << d;
        return 0;
      }
    }
    if (x < 0)
      nearest = -nearest;

    if (k % 2 == 0)
      x = nearest;
    else {
      if (x < 0) {
        trace(1);
        x = (nearest + d);
      } else {
        trace(2, x, d);
        x = (nearest - d);
      }
    }
    trace(nearest);
  } else {
    if (x > 0ll)
      x -= k * d;
    else
      x += k * d;
  }
  cout << abs(x);
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e6 + 233;

int main() {

  ll x, k, d;
  cin >> x >> k >> d;
  ll ans;
  x = abs(x);
  ll s = min(k, x / d);
  k -= s;
  x -= s * d;
  if (k & 1)
    cout << d - x << endl;
  else
    cout << x << endl;

  return 0;
}

Error: -8

Fixed submission:

/*
        LINES :
                * walking takahashi
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define OJ
#define pb push_back
#define F first
#define S second

#define WLL(t) while (t--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define STF(i, a, b) for (size_t i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = a; i >= b; i--)
#define SRF(i, a, b) for (size_t i = a; i >= b; i--)

#define ALL(v) v.begin(), v.end()
#define PRECISION(x) cout << fixed << setprecision(x)
#define FASTIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define MOD 1000000007

#define TO(v, type) static_cast<type>(v)

void solve(ll x, ll k, ll d);
int main() {
#ifdef OJ
  READ;
  WRITE;
#endif

  FASTIO;
  ll x{}, k{}, d{};
  cin >> x >> k >> d;
  solve(x, k, d);
  return 0;
}
void solve(ll x, ll k, ll d) {
  // getting near of 0
  ll near = x / d;
  if (near >= k) {
    cout << x - (k * d) << "\\n";
  } else {
    ll nearval = x - (near * d);
    k -= near;
    if (k & 2) {
      cout << abs(nearval - d) << "\\n";
    } else {
      cout << nearval << "\\n";
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int64_t X, K, D;
  cin >> X >> K >> D;
  if (X < 0) {
    X = -X;
  }

  // D未満になるまでにかかる回数
  int64_t num = X / D;

  if (num >= K) {
    cout << X - D * K << endl;
    return 0;
  }

  // D未満になったときの値
  int64_t x = X - D * num;

  // 残り操作回数
  int64_t rem = K - num;

  if (rem % 2 == 0) {
    cout << x << endl;
  } else {
    cout << D - x << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int64_t X, K, D;
  cin >> X >> K >> D;
  if (X < 0) {
    X = -X;
  }

  // D未満になるまでにかかる回数
  int64_t num = X / D;
  assert(num <= K);

  if (num >= K) {
    cout << X - D * K << endl;
    return 0;
  }

  // D未満になったときの値
  int64_t x = X - D * num;

  // 残り操作回数
  int64_t rem = K - num;

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

#include <iostream>

using namespace std;

int main(void) {

  long long X, K, D;
  long long XD, KK;
  long long ans;

  cin >> X >> K >> D;

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

  if (X / D >= K)
    ans = X - D * K;
  else {
    XD = X / D;
    KK = K - XD;
    X -= XD * D;
    if (KK % 2 == 1)
      if ((X -= D) < 0)
        X *= -1;
    ans = X;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {

  long long X, K, D;
  long long a;
  long long ans = 1000000000000000;

  cin >> X >> K >> D;

  for (int i = 0; i <= K; i++) {
    if (ans == 0)
      break;
    a = X + D * i - D * (K - i);
    if (a < 0)
      a *= -1;
    if (a < ans)
      ans = a;
  }

  cout << ans << endl;

  return 0;
}
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++)
typedef long long ll;
////////////////////////////////////////////////
int main() {
  ll x, k, d, a;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d < k) {
    a = x - (x / d) * d;
    k -= x / d;
  } else {
    a = x - k * d;
    k = 0;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
////////////////////////////////////////////////
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  while (x > d) {
    x -= d;
    k--;
    if (k == 0)
      break;
  }

  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>
using namespace std;

typedef pair<int, int> P;

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

  long long ans = 1e15 + 1;

  if (abs(K * D) < abs(X)) {
    cout << abs(X) - abs(K * D) << endl;
    return 0;
  }

  for (long long i = 0; i < K; ++i) {
    if (abs(X - D) < abs(X + D)) {
      X = X - D;
    } else {
      X = X + D;
    }

    if (abs(ans) == abs(X)) {
      if ((K - i) % 2 == 0) {
        if (abs(ans - D) < abs(ans + D)) {
          ans = ans - D;
        } else {
          ans = ans + D;
        }
      }
      break;
    }

    ans = min(abs(ans), abs(X));
  }

  cout << abs(ans) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;

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

  long long ans = 1e15 + 1;

  for (long long i = 0; i < K; ++i) {
    if (abs(X - D) < abs(X + D)) {
      X = X - D;
    } else {
      X = X + D;
    }
    if (abs(ans) == abs(X)) {
      if ((K - i) % 2 == 0) {
        if (abs(ans - D) < abs(ans + D)) {
          ans = ans - D;
        } else {
          ans = ans + D;
        }
      }
      break;
    }

    ans = min(abs(ans), abs(X));
  }

  cout << abs(ans) << endl;

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

#include <iostream>
using namespace std;

int main(void) {
  long X, K, D;

  cin >> X >> K >> D;

  long now_place = X;
  long times = now_place / D;
  if (times < 0) {
    times *= -1;
  }

  if (times > K) {
    times = K;
  }

  if (now_place > 0) {
    now_place -= D * times;
    K -= times;
  } else {
    now_place += D * times;
    K -= times;
  }

  if (K % 2 == 0) {
    if (now_place > 0) {
      cout << now_place << endl;
    } else {
      cout << now_place * -1 << endl;
    }
  } else {
    if (now_place < 0) {
      now_place += D;
      if (now_place > 0) {
        cout << now_place << endl;
      } else {
        cout << now_place * -1 << endl;
      }
    } else {

      now_place -= D;
      if (now_place > 0) {
        cout << now_place << endl;
      } else {
        cout << now_place * -1 << endl;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  long X, K, D;

  cin >> X >> K >> D;

  long now_place = X;

  while (1) {
    if (now_place > 0) {
      now_place -= D;
      K--;
      if (now_place < 0) {
        K++;
        now_place += D;
        break;
      }
      if (K == 0)
        break;
    } else {
      now_place += D;
      K--;
      if (now_place > 0) {
        K++;
        now_place -= D;
        break;
      }
      if (K == 0)
        break;
    }
  }

  if (K % 2 == 0) {
    if (now_place > 0) {
      cout << now_place << endl;
    } else {
      cout << now_place * -1 << endl;
    }
  } else {
    if (now_place < 0) {
      now_place += D;
      if (now_place > 0) {
        cout << now_place << endl;
      } else {
        cout << now_place * -1 << endl;
      }
    } else {

      now_place -= D;
      if (now_place > 0) {
        cout << now_place << endl;
      } else {
        cout << now_place * -1 << endl;
      }
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll ans;
  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;

  return 0;
}

Error: TLE

Fixed submission:

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

ll absll(ll v) {
  if (v < 0)
    return -v;
  else
    return v;
}

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

  X = absll(X);
  ll prev = X;
  while ((X - D) >= 0) {
    prev = X;
    X -= D;
    K--;
    if (K == 0) {
      cout << X << endl;
      return 0;
    }
  }
  prev = X;
  X -= D;
  K--;

  if (K % 2 == 0) {
    cout << absll(X) << endl;
    return 0;
  } else {
    cout << prev << endl;
    return 0;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll X, K, D, N;
  cin >> X >> K >> D;
  if (X > 0) {
    N = X / D;
    if (N > K) {
      X = X - K * D;
    } else {
      X = X - N * D;
      N = (K - N) % 2;
      if (N != 0) {
        X = X - D;
      }
    }
  } else {
    N = (-1) * X / D;
    if (N > K) {
      X = X + K * D;
    } else {
      X = X + N * D;
      N = (K - N) % 2;
      if (N != 0) {
        X = X + D;
      }
    }
  }
  cout << llabs(X);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll move(ll X, ll D) {
  ll a, b;
  a = llabs(X + D);
  b = llabs(X - D);
  return a > b ? b : a;
}
int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  for (ll i = 0; i < K; i++) {
    X = move(X, D);
  }
  cout << X;
}
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 m = X;
  if (K < 1000000000 || (X / D) < 1000000000) {
    for (int i = 0; i < K; i++) {
      if (m > abs(m - D)) {
        if (m - D < 0) {
          m = abs(m - D);
          if ((K - i) % 2 == 0) {
            m = min(abs(m + D), abs(m - D));
          }
          break;
        }
        m = abs(m - D);
      } else {
        if ((K - i) % 2 == 1) {
          m = min(abs(m + D), abs(m - D));
        }
        break;
      }
    }
    cout << m << endl;
  } else {
    long long a = X % D;
    if ((X / D + 1) > K && X > K * D) {
      cout << X - K * D << endl;
    } else {
      long long b = (X - a) / D;
      if ((K - b) % 2 == 0)
        cout << a << endl;
      else
        cout << D - a << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#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, xTemp, kTemp;
  cin >> x >> k >> d;
  ll ans = 0;
  if (x < 0) {
    // xは正の数だけ考えればよい
    x = abs(x);
  } else
    ;

  if (k <= x / d) {

    ans = abs(x - k * d);
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#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 = 0;
  ll ansPl = 0;
  ll ansMi = 0;
  rep(i, k) {
    ansPl = x + d;
    ansMi = x - d;
    if (abs(ansPl) >= abs(ansMi)) {
      x = ansMi;
    } else {
      x = ansPl;
    }
  }
  ans = abs(x);
  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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d, a = 0;
  cin >> x >> k >> d;
  x = abs(x);
  double p = x / d;
  ll q = (p + 0.5) / 1;
  if (q == (p + 0.5)) {
    q = q - 1;
  } else if (k <= q) {
    q = k;
  }
  ll min = abs(x - q * d);
  if ((k - q) % 2 == 0) {
    cout << min << endl;
  } else {
    cout << abs(d - min) << 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, min = 1000000000000001, a = 0;
  cin >> x >> k >> d;
  x = abs(x);
  for (ll i = 0; i < k; i++) {
    if (abs(x - i * d) <= min) {
      min = abs(x - i * d);
      a = i;
    } else if (abs(x - i * d) <= abs(x - (i + 1) * d)) {
      break;
    }
  }
  if ((k - a) % 2 == 0) {
    cout << min << endl;
  } else {
    cout << abs(d - min) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x;
  long long k;
  long long d;
  cin >> x >> k >> d;
  long long mostnear = abs(x) % d;
  if (mostnear / d + k <= abs(x) / d) {
    cout << abs(x) - (k * d);
  } else if ((abs(k) % abs(x / d) == 1))
    cout << mostnear;
  else
    cout << abs(mostnear - d);
}
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 rep2(i, x, n) for (int i = x; i < (n); i++)
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}

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

  ll p = X;
  ll m = 1e16;
  ll r = abs(m) % D;
  vector<ll> q(3);
  ll j = 0;

  ll F = abs(p) / D;
  ll G = abs(p) % D;
  ll H = abs(G - D);
  if (K <= F) {
    cout << abs(X) - K * D << endl;
    return 0;
  }
  if ((K - F) % 2 == 0) {
    cout << G << endl;
  } else {
    cout << H << endl;
  }
  return 0;

  rep(i, K) {
    if (p > 0)
      p -= D;
    else
      p += D;

    chmin(m, abs(p));

    q[j % 3] = p;

    if (j > 3) {
      if (p == q[(j - 2) % 3]) {
        // cout << j << " "<< q[j%3] << " " << q[(j-1)%3] << " " << q[(j-2)%3]
        // << endl;
        if ((K - i) % 2 == 0) {
          m = abs(q[(j - 1) % 3]);
        } else {
          m = abs(p);
        }

        break;
      }
    }
    ++j;
  }
  cout << m << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}

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

  ll p = X;
  ll m = 1e16;
  ll r = abs(m) % D;
  vector<ll> q(3);
  ll j = 0;
  rep(i, K) {
    if (p > 0)
      p -= D;
    else
      p += D;

    chmin(m, abs(p));

    q[j % 3] = p;

    if (j > 3) {
      if (p == q[(j - 2) % 3]) {
        // cout << j << " "<< q[j%3] << " " << q[(j-1)%3] << " " << q[(j-2)%3]
        // << endl;
        if ((K - i) % 2 == 0) {
          m = abs(q[(j - 1) % 3]);
        } else {
          m = abs(p);
        }

        break;
      }
    }
    ++j;
  }
  cout << m << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

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

  vector<ll> cand;
  cand.push_back(0LL);
  cand.push_back(k);
  ll y = (-x) / d;
  vector<ll> sub;
  FOR(i, -100, 100) {
    ll z = k + y + i;
    FOR(j, -100, 100) { sub.push_back(z / 2 + j); }
  }

  FOR(i, -10, 10) REP(j, 2) for (ll t : sub) {
    ll u = j == 0 ? (t + i) : (-t + i);
    if (0LL <= u && u <= k) {
      cand.push_back(u);
    }
  }

  ll ans = 1e18;
  for (ll c : cand) {
    // DUMP(c);
    ll cons = 2 * c - k;
    if (abs(cons) > (d - 1 + 1e18) / d) {
      continue;
    }
    ll e = abs(x + cons * d);
    // DUMP(d);
    ans = min(ans, e);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  vector<ll> cand;
  cand.push_back(0LL);
  cand.push_back(k);
  ll t = (k - d / x) / 2;
  FOR(i, -10, 11) REP(j, 2) {
    ll u = j == 0 ? (t + i) : (-t + i);
    if (0LL <= u && u <= k) {
      cand.push_back(u);
    }
  }

  ll ans = 1e18;
  for (ll c : cand) {
    // DUMP(c);
    ll e = abs(x + (2LL * c - k) * d);
    // DUMP(d);
    ans = min(ans, e);
  }
  cout << ans << endl;
}