Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/

signed main() {
  int x, k, d;
  cin >> x >> k >> d;
  // int now=x;
  // rep(i,k){
  // 	if(now>0) now-=d;
  // 	else now+=d;
  // }
  // cout<<"answer="<<abs(now)<<endl;

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

  int dist = abs(x);
  int g = (x / dist) * (dist % d);

  int cnt = abs(x - g) / d;

  if (cnt > k) {
    if (x > 0)
      g += (cnt - k) * d;
    else
      g -= (cnt - k) * d;
    cout << abs(g) << endl;
    return 0;
  } else if (cnt == k) {
    cout << abs(g) << endl;
    return 0;
  } else {
    // assert(0);
    // printf("k=%lld\\n",k);
    // printf("cnt=%lld\\n",cnt);
    // printf("g=%lld\\n",g);
    k -= cnt;
    k %= 2;
    if (k == 0) {
      cout << abs(g) << endl;
    } else {
      if (x > 0)
        g -= d;
      else
        g += d;
      cout << abs(g) << endl;
    }
  }
  return 0;
}


Error: -6
61803eb9-126e-49d6-8a50-7865b9588e2b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02584/C++/s255008280.cpp:49: int main(): Assertion `0' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/

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

  int dist = abs(x);
  int g = (x / dist) * (dist % d);

  int cnt = abs(x - g) / d;

  if (cnt > k) {
    if (x > 0)
      g += (cnt - k) * d;
    else
      g -= (cnt - k) * d;
    cout << g << endl;
    return 0;
  } else if (cnt == k) {
    cout << g << endl;
  } else {
    assert(0);
    // printf("k=%lld\\n",k);
    // printf("cnt=%lld\\n",cnt);
    // printf("g=%lld\\n",g);
    k -= cnt;
    if (k % 2 == 0) {
      cout << g << endl;
      return 0;
    }
    if (x > 0) {
      g -= d;
      k--;

    } else {
      g += d;
      k--;
    }
    cout << abs(g) << endl;
  }
  return 0;
}