Problem p02584 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// constexpr int N=;
void solve() {
  int in, t, d;
  cin >> in >> t >> d;
  int ff = 0;
  if (in < 0) {
    in *= -1;
    ff = 1;
  }

  int mulp = in / d;
  mulp = min(mulp, t);
  int rem = (t - mulp) % 2;
  int prefinal = in - d * mulp;
  if (rem == 0) {
    cout << prefinal << endl;
  } else {
    cout << min(abs(prefinal + d), abs(prefinal - d)) << endl;
  }
  // cout << << "   " << (t - mulp) % 2  << endl;
  return;
}

signed main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // sieve();
  int t = 1;
  cin >> t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// constexpr int N=;
void solve() {
  int in, t, d;
  cin >> in >> t >> d;
  int ff = 0;
  if (in < 0) {
    in *= -1;
    ff = 1;
  }

  int mulp = in / d;
  mulp = min(mulp, t);
  int rem = (t - mulp) % 2;
  int prefinal = in - d * mulp;
  if (rem == 0) {
    cout << prefinal << endl;
  } else {
    cout << min(abs(prefinal + d), abs(prefinal - d)) << endl;
  }
  // cout << << "   " << (t - mulp) % 2  << endl;
  return;
}

signed main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // sieve();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}