prithivMLmods/Coder-Stat huggingface dataset:
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(a) cerr << #a << ": " << a << endl
#define sz(a) int(a.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define forr(i, st, en) for (int i = st; i <= en; i++)
#define all(v) (v).begin(), (v).end()
#define X first
#define Y second
#define pb push_back
#define fast                                                                   \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define vi vector<int>
#define vii vector<vector<int>>
#define int long long
int32_t main() {
  fast;
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a));
  int answer = 0;
  rep(i, n) {
    rep(j, n) {
      rep(k, n) {
        if (i < j && j < k && a[i] != a[j] && a[j] != a[k] &&
            a[i] + a[j] > a[k] && a[i] + a[k] > a[j] && a[j] + a[k] > a[i]) {
          debug(i);
          debug(j);
          debug(k);
          answer++;
        }
      }
    }
  }
  cout << answer << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(a) cerr << #a << ": " << a << endl
#define sz(a) int(a.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define forr(i, st, en) for (int i = st; i <= en; i++)
#define all(v) (v).begin(), (v).end()
#define X first
#define Y second
#define pb push_back
#define fast                                                                   \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define vi vector<int>
#define vii vector<vector<int>>
#define int long long
int32_t main() {
  fast;
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a));
  int answer = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (a[i] != a[j] && a[j] != a[k] && a[i] + a[j] > a[k] &&
            a[i] + a[k] > a[j] && a[j] + a[k] > a[i]) {
          answer++;
        }
      }
    }
  }
  cout << answer << endl;
}
Problem p02583 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int cnt = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  if (N < 3) {
    cout << 0;
    return -1;
  } else {
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        for (int k = j + 1; k < N; k++) {
          if ((A[i] != A[j]) && (A[j] != A[k]) && (A[k] != A[i])) {
            if (A[i] + A[j] > A[k]) {
              cnt++;
            }
          }
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int cnt = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < j; k++) {
        if ((A[i] != A[j]) && (A[j] != A[k]) && (A[k] != A[i])) {
          if (A[k] + A[j] > A[i]) {
            cnt++;
          }
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  vector<int> v;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    v.push_back(temp);
  }
  sort(v.begin(), v.end());

  int ans = 0;
  for (int i = 0; i < v.size() - 2; i++) {
    int j = i + 1;
    int k = i + 2;
    while (j < k && k < N) {
      j = k - 1;
      while (v[i] + v[j] > v[k] && j > i) {
        if (v[i] != v[j] && v[j] != v[k])
          ans++;
        j--;
      }
      k++;
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  vector<int> v;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    v.push_back(temp);
  }
  sort(v.begin(), v.end());

  int ans = 0;
  for (int i = 0; i < N - 2; i++) {
    int j = i + 1;
    int k = i + 2;
    while (j < k && k < N) {
      j = k - 1;
      while (v[i] + v[j] > v[k] && j > i) {
        if (v[i] != v[j] && v[j] != v[k])
          ans++;
        j--;
      }
      k++;
    }
  }
  cout << ans;
}
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;

#define rep(idx, N) for (u64 idx = 0; idx < N; idx++)

int main() {
  u32 n;
  cin >> n;

  vector<u64> l(n);

  rep(i, n) { cin >> l[i]; }

  sort(l.begin(), l.end());

  u64 ans = 0;

  for (u32 i = 0; i < n - 2; i++) {
    for (u32 j = i + 1; j < n - 1; j++) {
      for (u32 k = j + 1; k < n; k++) {
        if ((l[i] != l[j] && l[k] != l[j])) {
          if (l[k] < l[i] + l[j]) {
            // cout << l[i] << " " << l[j] << " " << l[k] << endl;
            ans++;
          } else {
            break;
          }
        }
      }
    }
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;

#define rep(idx, N) for (u64 idx = 0; idx < N; idx++)

int main() {
  u32 n;
  cin >> n;

  vector<u64> l(n);

  rep(i, n) { cin >> l[i]; }

  sort(l.begin(), l.end());

  u64 ans = 0;

  if (n < 3) {
    cout << 0;
    return 0;
  }

  for (u32 i = 0; i < n - 2; i++) {
    for (u32 j = i + 1; j < n - 1; j++) {
      for (u32 k = j + 1; k < n; k++) {
        if ((l[i] != l[j] && l[k] != l[j])) {
          if (l[k] < l[i] + l[j]) {
            // cout << l[i] << " " << l[j] << " " << l[k] << endl;
            ans++;
          } else {
            break;
          }
        }
      }
    }
  }

  cout << ans;

  return 0;
}
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

typedef long long ll;
// forループ
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
//
//
#define int long long

using namespace std;

int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}

ll a, b, c, s, t, x, y, z;
ll A, B, C, S, T, X, Y, Z;

ll cnt;

long double pi = 3.14159265358979323846;

string N, M;

unsigned long long D;

signed main() {

  cin >> a;

  vector<int> f(a);

  REP(i, a) cin >> f[i];

  for (x = 0; x < f.size() - 2; x++) {
    for (y = x + 1; y < f.size() - 1; y++) {
      for (z = y + 1; z < f.size(); z++) {
        if (f[x] > f[y] && f[x] > f[z] && f[x] - f[y] - f[z] < 0 &&
            f[y] != f[z])
          cnt++;
        else if (f[y] > f[x] && f[y] > f[z] && f[y] - f[x] - f[z] < 0 &&
                 f[x] != f[z])
          cnt++;
        else if (f[z] > f[y] && f[z] > f[x] && f[z] - f[y] - f[x] < 0 &&
                 f[x] != f[y])
          cnt++;
      }
    }
  }

  cout << cnt;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;
// forループ
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
//
//
#define int long long

using namespace std;

int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}

ll a, b, c, s, t, x, y, z;
ll A, B, C, S, T, X, Y, Z;

ll cnt;

long double pi = 3.14159265358979323846;

string N, M;

unsigned long long D;

signed main() {

  cin >> a;

  vector<int> f(a);

  REP(i, a) cin >> f[i];

  for (x = 0; x < a - 2; x++) {
    for (y = x + 1; y < a - 1; y++) {
      for (z = y + 1; z < a; z++) {
        if (f[x] > f[y] && f[x] > f[z] && f[x] - f[y] - f[z] < 0 &&
            f[y] != f[z])
          cnt++;
        else if (f[y] > f[x] && f[y] > f[z] && f[y] - f[x] - f[z] < 0 &&
                 f[x] != f[z])
          cnt++;
        else if (f[z] > f[y] && f[z] > f[x] && f[z] - f[y] - f[x] < 0 &&
                 f[x] != f[y])
          cnt++;
      }
    }
  }

  cout << cnt;

  return 0;
}
Problem p02583 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
L = map(int, input().split())

if N < 3:
    print(0)
    exit()

ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            arr = [L[i], L[j], L[k]]
            arr.sort()
            if arr[0] != arr[1] & arr[1] != arr[2]:
                if arr[2] < arr[0] + arr[1]:
                    ans = ans + 1

print(ans)


Fixed submission:
N = int(input())
L = list(map(int, input().split()))

if N < 3:
    print(0)
    exit()

ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            arr = [L[i], L[j], L[k]]
            arr.sort()
            if arr[0] != arr[1] & arr[1] != arr[2]:
                if arr[2] < arr[0] + arr[1]:
                    ans = ans + 1

print(ans)
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#include <vector>
#define re(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  re(i, n) cin >> l[i];

  int cnt = 0;

  re(i, n - 2) {
    for (int j = i + 1; i < n - 1; j++) {
      for (int k = j + 1; i < n; k++) {
        if (abs(l[i] - l[j]) < l[k] && l[k] < l[i] + l[j])
          cnt++;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#include <vector>
#define re(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  re(i, n) cin >> l[i];

  int cnt = 0;

  re(i, n - 2) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (abs(l[i] - l[j]) < l[k] && l[k] < l[i] + l[j] && l[i] != l[j] &&
            l[i] != l[k] && l[j] != l[k])
          cnt++;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>

int main() {
  unsigned N, answer = 0;
  scanf("%u", &N);
  unsigned L[N];
  for (int i = 0; i < N; ++i) {
    scanf("%u", L + i);
  }
  for (unsigned i = 0; i < N - 2; i++) {
    for (unsigned j = i + 1; j < N - 1; j++) {
      for (unsigned k = j + 1; k < N; k++) {
        if (L[i] != L[j] && L[k] != L[j] && L[i] != L[k] &&
            L[i] + L[j] > L[k] && L[i] + L[k] > L[j] && L[k] + L[j] > L[i])
          answer++;
      }
    }
  }
  printf("%u\n", answer);
  return 0;
}

Fixed submission:
#include <cstdio>

int main() {
  unsigned N, answer = 0;
  scanf("%u", &N);
  unsigned L[N];
  for (int i = 0; i < N; ++i) {
    scanf("%u", L + i);
  }
  for (unsigned i = 0; i + 2 < N; i++) {
    for (unsigned j = i + 1; j + 1 < N; j++) {
      for (unsigned k = j + 1; k < N; k++) {
        if (L[i] != L[j] && L[k] != L[j] && L[i] != L[k] &&
            L[i] + L[j] > L[k] && L[i] + L[k] > L[j] && L[k] + L[j] > L[i])
          answer++;
      }
    }
  }
  printf("%u\n", answer);
  return 0;
}
Problem p02583 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }

  int result = 0;

  // for(int i=0; i<l.size(); i++){
  //   cout << l[i] << endl;
  // }

  for (int i = 0; i < l.size() - 2; i++) {
    // cout << i << "-" << l[i] << " " << endl;
    for (int j = i + 1; j < l.size() - 1; j++) {
      // cout << "  " << j << "-" <<  l[j] << endl;
      for (int k = j + 1; k < l.size(); k++) {
        // cout << "    " << k << "-" << l[k];
        if (l[i] != l[j] && l[i] != l[k] && l[j] != l[k] &&
            (l[i] + l[j] > l[k] && l[i] + l[k] > l[j] && l[j] + l[k] > l[i])) {
          result++;
          // cout << "[x]";
        }
        // cout << endl;
      }
    }
  }

  cout << result << endl;

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }

  int result = 0;

  // for(int i=0; i<l.size(); i++){
  //   cout << l[i] << endl;
  // }

  if (n == 1) {
    cout << result << endl;
    return 0;
  }

  for (int i = 0; i < l.size() - 2; i++) {
    // cout << i << "-" << l[i] << " " << endl;
    for (int j = i + 1; j < l.size() - 1; j++) {
      // cout << "  " << j << "-" <<  l[j] << endl;
      for (int k = j + 1; k < l.size(); k++) {
        // cout << "    " << k << "-" << l[k];
        if (l[i] != l[j] && l[i] != l[k] && l[j] != l[k] &&
            (l[i] + l[j] > l[k] && l[i] + l[k] > l[j] && l[j] + l[k] > l[i])) {
          result++;
          // cout << "[x]";
        }
        // cout << endl;
      }
    }
  }

  cout << result << endl;

  return 0;
}
Problem p02583 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<int> v;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int nr;
    cin >> nr;
    v.push_back(nr);
  }

  sort(v.begin(), v.end());
  v.push_back(0);
  int total = 0, lastj = 0, lasti = 0;
  for (int i = 0; i < v.size() - 3; i++) {
    if (i > 0) {
      if (v[i] == v[i - 1]) {
        total += lasti;
        continue;
      } else {
        lasti = 0;
      }
    }
    for (int j = i + 1; j < v.size() - 2; j++)
      if (v[i] != v[j]) {
        if (v[j] == v[j - 1]) {
          total += lastj;
          lasti += lastj;
          continue;
        }
        int st = j + 1, dr = v.size() - 2;
        while (v[st] == v[st - 1] && st <= dr)
          st++;
        if (st > dr) {
          lastj = 0;
          continue;
        }
        int ini_st = st;

        while (st < dr) {
          int mij = (st + dr) / 2;
          if (v[i] + v[j] > v[mij])
            st = mij + 1;
          else
            dr = mij;
        }
        while (v[i] + v[j] <= v[st])
          st--;

        total += max(0, st - ini_st + 1);
        lastj = max(0, st - ini_st + 1);
        lasti += lastj;
      }
  }

  cout << total;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<int> v;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int nr;
    cin >> nr;
    v.push_back(nr);
  }
  if (n < 3) {
    cout << 0;
    return 0;
  }

  sort(v.begin(), v.end());
  v.push_back(0);
  int total = 0, lastj = 0, lasti = 0;
  for (int i = 0; i < v.size() - 3; i++) {
    if (i > 0) {
      if (v[i] == v[i - 1]) {
        total += lasti;
        continue;
      } else {
        lasti = 0;
      }
    }
    for (int j = i + 1; j < v.size() - 2; j++)
      if (v[i] != v[j]) {
        if (v[j] == v[j - 1]) {
          total += lastj;
          lasti += lastj;
          continue;
        }
        int st = j + 1, dr = v.size() - 2;
        while (v[st] == v[st - 1] && st <= dr)
          st++;
        if (st > dr) {
          lastj = 0;
          continue;
        }
        int ini_st = st;

        while (st < dr) {
          int mij = (st + dr) / 2;
          if (v[i] + v[j] > v[mij])
            st = mij + 1;
          else
            dr = mij;
        }
        while (v[i] + v[j] <= v[st])
          st--;

        total += max(0, st - ini_st + 1);
        lastj = max(0, st - ini_st + 1);
        lasti += lastj;
      }
  }

  cout << total;
  return 0;
}
Problem p02584 (Python)

Buggy submission (status: Time Limit Exceeded):
x, k, d = map(int, input().split())
x = abs(x)
while k > 0:
    x = min(abs(x + d), abs(x - d))
    k -= 1
print(x)


Fixed submission:
x, k, d = map(int, input().split())
x = abs(x)

k_ = min(k, x // d)
k = k - k_
x = x - k_ * d

if k % 2 != 0:
    x = abs(x - d)
print(x)
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;

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

  X = abs(X);
  while (abs(X) >= D && K > 0) {
    X -= D;
    K--;
  }

  if (K == 0) {
    cout << abs(X) << endl;
  } else if (K % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

  return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;

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

  X = abs(X);
  // while (abs(X) >= D && K > 0) {
  //     X -= D;
  //     K--;
  // }
  ll q = X / D;
  q = min(q, K);
  K -= q;
  X -= D * q;

  if (K == 0) {
    cout << abs(X) << endl;
  } else if (K % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;

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

  x = abs(x);

  bool ok = true;
  ll tmp = 0;
  rep(i, k) {
    tmp += d;
    if (tmp >= x) {
      ok = false;
      break;
    }
  }

  if (ok) {
    ll ans = x;
    rep(i, k) ans -= d;
    cout << ans << endl;
    return 0;
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;

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

  x = abs(x);

  bool ok = true;

  if (k * d > x)
    ok = false;
  if (log10(k) + log10(d) > 17)
    ok = false;

  if (ok) {
    ll ans = x;
    rep(i, k) ans -= d;
    cout << ans << endl;
    return 0;
  }

  if ((k - (x / d)) % 2 == 0) {
    cout << x - (x / d) * d << endl;
  } else {
    cout << abs(x - ((x / d) + 1) * d) << endl;
  }
}
Problem p02584 (C++)

Buggy submission (status: 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;
  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;
}

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;

  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;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);
  if (x == d || -x == d) {
    printf("%lld\n", (k & 1 ? 0 : d));
    return 0;
  }
  if (k == 1) {
    printf("%lld\n", (x >= 0 ? abs(x - d) : abs(x + d)));
    return 0;
  }
  if (x >= 0) {
    while (x - d >= 0) {
      x -= d;
      k--;
      if (!k) {
        printf("%lld\n", abs(x));
        return 0;
      }
    }
  }
  if (x < 0) {
    while (x + d < 0) {
      x += d;
      k--;
      if (!k) {
        printf("%lld\n", abs(x));
        return 0;
      }
    }
  }
  if (k & 1) {
    printf("%lld\n", (abs(x - d) >= abs(x + d) ? abs(x + d) : abs(x - d)));
    return 0;
  } else {
    printf("%lld\n", abs(x));
    return 0;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll a = abs(x) / d;
  ll ans;
  if (a == k)
    ans = abs(x) - d * a;
  else if (a < k) {
    k -= a;
    if (k & 1)
      ans = d * (a + 1) - abs(x);
    else
      ans = abs(x) - d * a;
  } else
    ans = abs(x) - d * k;
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <limits>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i, n) for (int i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define pb push_back
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll ans = 1e14;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll sol = (i - 1) + (n / i - 1);
      ans = min(ans, sol);
    }
  }
  return ans;
}
int visited[200000] = {0};
vector<vector<int>> v(200000);
int dfs(int x) {
  int ans = 0;
  if (visited[x] == 0) {
    visited[x] = 1;
    ans++;
    int a = v[x].size();
    for (int j = 0; j < a; j++) {
      int data = v[x][j];
      ans = ans + dfs(data);
    }
  }
  return ans;
}
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      cout << x;
    } else {
      ll y = abs(x - d);
      ll z = abs(x + d);
      cout << min(y, z) << endl;
    }
  }
  x = abs(x);
  if ((x / k) > d) {
    x = x - k * d;
    cout << x << endl;
  } else if (x > 0) {

    while (x >= 0 && k > 0) {
      if (x - d >= 0) {
        x = x - d;
        k--;
      } else {
        break;
      }
    }
    if (k == 0) {
      cout << x << endl;
    } else {
      if (k % 2 == 0)
        cout << x << endl;
      else {
        ll y = abs(x - d);
        ll z = abs(x + d);
        cout << min(y, z) << endl;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <limits>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i, n) for (int i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define pb push_back
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll ans = 1e14;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll sol = (i - 1) + (n / i - 1);
      ans = min(ans, sol);
    }
  }
  return ans;
}
int visited[200000] = {0};
vector<vector<int>> v(200000);
int dfs(int x) {
  int ans = 0;
  if (visited[x] == 0) {
    visited[x] = 1;
    ans++;
    int a = v[x].size();
    for (int j = 0; j < a; j++) {
      int data = v[x][j];
      ans = ans + dfs(data);
    }
  }
  return ans;
}
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      cout << x;
    } else {
      ll y = abs(x - d);
      ll z = abs(x + d);
      cout << min(y, z) << endl;
    }
  }
  x = abs(x);
  if ((x / k) >= d) {
    x = x - k * d;
    cout << x << endl;
  } else if (x > 0) {

    while (x >= 0 && k > 0) {
      if (x - d >= 0) {
        x = x - d;
        k--;
      } else {
        break;
      }
    }
    if (k == 0) {
      cout << x << endl;
    } else {
      if (k % 2 == 0)
        cout << x << endl;
      else {
        ll y = abs(x - d);
        ll z = abs(x + d);
        cout << min(y, z) << endl;
      }
    }
  }
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  while (k--) {
    if (x > 0)
      x -= d; // 현재위치(x)가 양수인 경우
    else
      x += d; // 현재위치(x)가 음수거나, 0인 경우
  }
  cout << abs(x);
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  long long n = (d ? x / d : 0), r = (d ? x % d : 0);
  if (n > k)
    cout << (x - k * d);
  else if ((k - n) % 2)
    cout << abs(d - r);
  else
    cout << r;
  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, pair<ll, ll>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

ll x, k, d, ans;

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

  x = abs(x);

  if ((ll)x / d < k) {
    if ((k - (ll)x / d) % 2) {
      ans = d - (x % d);
    } else {
      ans = x % d;
    }
  } else {
    if ((ll)x / d == k) {
      rep(i, d) {
        if (x % d == i) {
          ans = i;
        }
      }
    } else {
      ans = x - (k * d);
    }
  }

  ans = abs(ans);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, pair<ll, ll>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

ll x, k, d, ans;

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

  x = abs(x);

  if ((ll)x / d < k) {
    if ((k - (ll)x / d) % 2) {
      ans = d - (x % d);
    } else {
      ans = x % d;
    }
  } else {
    if ((ll)x / d == k) {
      ans = x % d;
    } else {
      ans = x - (k * d);
    }
  }

  ans = abs(ans);

  cout << ans << endl;

  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  for (long long i = 1; i < k + 1; i++) {
    if (x <= 0) {
      if ((k - i) % 2 == 1) {
        cout << abs(x);
        return 0;
      } else {
        cout << x + d;
        return 0;
      }
    }
    x -= d;
  }
  cout << abs(x);
}

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 n = x / d + 1;
  long long m = min(k, n);
  x -= d * m;
  if ((k - m) % 2 == 0)
    cout << abs(x);
  else
    cout << x + d;
}
Problem p02584 (Python)

Buggy submission (status: Time Limit Exceeded):
X, K, D = map(int, input().split())

x = abs(X)
k = K
while x > 0 and k > 0:
    x -= D
    k -= 1

if k % 2 == 1:
    x += D

print(abs(x))


Fixed submission:
X, K, D = map(int, input().split())

x = abs(X)
k = K

xd = min(x // D + 1, K)
x -= D * xd
k -= xd

if k % 2 == 1:
    x += D

print(abs(x))
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MAXX 105

ll n;
ll l[MAXX];

int main() {
  _FastIO;

  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  while ((x - d) >= 0 && k) {
    k--;
    x -= d;
  }
  if (!k) {
    cout << x << endl;
    return 0;
  }
  if (k % 2) {
    cout << abs(x - d) << endl;
    return 0;
  }
  cout << x << endl;
  return 0;
}


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

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MAXX 105

ll n;
ll l[MAXX];

int main() {
  _FastIO;

  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll p = x / d;
  if (p >= k) {
    x -= (k * d);
    k = 0;
  } else {
    x %= d;
    k -= p;
  }
  if (!k) {
    cout << x << endl;
    return 0;
  }
  if (k % 2) {
    cout << abs(x - d) << endl;
    return 0;
  }
  cout << x << endl;
  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  while (1) {
    if ((abs(X) <= D && K % 2 == 0) || K == 0) {
      break;
    }
    if (X < 0)
      X += D;
    else
      X -= D;
    --K;
  }

  cout << abs(X) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  while (1) {
    if ((abs(X) <= D && K % 2 == 0) || K == 0) {
      break;
    }
    if (X < 0) {
      if (D <= abs(X)) {
        long long buf = min(abs(X) / D, K);
        K -= buf;
        X = X + buf * D;
      } else {
        X += D;
        --K;
      }
    } else {
      if (D <= X) {
        long long buf = min(X / D, K);
        K -= buf;
        X = X - buf * D;
      } else {
        X -= D;
        --K;
      }
    }
  }

  cout << abs(X) << endl;

  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long x, k, d;
  cin >> x >> k >> d;
  long kk = abs(x) + d; // 距離
  long nn = 0;          // 何回でアウトしたか
  for (long i = 1; i <= k; i++) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (kk >= abs(x - 0)) {
      kk = abs(x - 0);
      nn = i;
    } else {
      break;
    }
  }
  long nokori = k - nn;
  if (nokori % 2 == 0)
    cout << kk;
  else
    cout << abs(kk - d);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long x, k, d;
  cin >> x >> k >> d;
  long kk = abs(x) + d; // 距離
  long nn = 0;          // 何回でアウトしたか

  nn = abs(x) / d;
  if (nn > k)
    nn = k;
  kk = abs(x) - nn * d;

  long nokori = k - nn;
  if (nokori % 2 == 0)
    cout << kk;
  else
    cout << abs(kk - d);
  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double x, k, d;
  cin >> x >> k >> d;
  ll y = k;
  if (y % 2 != 0) {
    long double r = (k + 1) / 2;
    long double l = (-1) * (r - 1);
    long double temp = l;
    long double mid;
    while (l < r) {
      mid = (ll)((l + r) / 2);
      if ((2 * mid - 1) * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + (2 * l - 1) * d) << endl;
    } else {
      cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
           << endl;
    }
  } else {
    long double r = k / 2;
    long double l = (-1) * r;
    long double temp = l, mid;
    while (l < r) {
      mid = floor(((l + r) / 2));
      if (2 * mid * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + 2 * l * d) << endl;
    } else {
      cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double x, k, d;
  cin >> x >> k >> d;
  ll y = k;
  if (y % 2 != 0) {
    long double r = (k + 1) / 2;
    long double l = (-1) * (r - 1);
    long double temp = l;
    long double mid;
    while (l < r) {
      mid = floor((l + r) / 2);
      if ((2 * mid - 1) * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + (2 * l - 1) * d) << endl;
    } else {
      cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
           << endl;
    }
  } else {
    long double r = k / 2;
    long double l = (-1) * r;
    long double temp = l, mid;
    while (l < r) {
      mid = floor(((l + r) / 2));
      if (2 * mid * d + x >= 0) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (l == temp) {
      cout << (ll)abs(x + 2 * l * d) << endl;
    } else {
      cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << endl;
    }
  }
}
Problem p02584 (Python)

Buggy submission (status: Runtime Error):
X, K, D = map(int, input().split())


def f(C):
    NX = float("inf")
    if C > K:
        return NX

    if K % 2 == 0:
        if C % 2 == 0:
            NX = abs(abs(X) - C * D)
        else:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
    else:
        if C % 2 == 0:
            NX = abs(NX, abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
        else:
            NX = abs(abs(X) - C * D)

    return NX


C = min(abs(X) // D, K)

print(min(f(C), f(C + 1)))


Fixed submission:
X, K, D = map(int, input().split())


def f(C):
    NX = float("inf")
    if C > K:
        return NX

    if K % 2 == 0:
        if C % 2 == 0:
            NX = abs(abs(X) - C * D)
        else:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
    else:
        if C % 2 == 0:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
        else:
            NX = abs(abs(X) - C * D)

    return NX


C = min(abs(X) // D, K)

print(min(f(C), f(C + 1)))
Problem p02584 (C++)

Buggy submission (status: 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 (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);
}


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 (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);
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

constexpr long long INFLL = 1e18;

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  map<ll, ll> check;
  bool c = false;
  check[x] = k;
  if (abs(x) / d > 0) {
    ll count = abs(x) / d;
    if (k >= count) {
      if (x > 0) {
        x = x - d * count;
      } else {
        x = x + d * count;
      }
      k -= count;
    }
  }
  while (k > 0) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (check[x] == 0) {
      check[x] = k;
    } else {
      c = true;
      k--;
      break;
    }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

constexpr long long INFLL = 1e18;

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  map<ll, ll> check;
  bool c = false;
  check[x] = k;
  if (abs(x) / d > 0) {
    ll count = abs(x) / d;
    if (k >= count) {
      if (x > 0) {
        x = x - d * count;
      } else {
        x = x + d * count;
      }
      k -= count;
    } else {
      if (x > 0) {
        x = x - d * k;
      } else {
        x = x + d * k;
      }
      k = 0;
    }
  }
  while (k > 0) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
    if (check[x] == 0) {
      check[x] = k;
    } else {
      c = true;
      k--;
      break;
    }

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

  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long a, d, c;
  cin >> a >> d >> c;
  for (long long i = 0; i < d; i++) {
    if (abs(a) <= c / 2) {
      i += ((d - i - 1) / 2) * 2;
    }
    if (a > 0)
      a -= c;
    else
      a += c;
  }
  cout << abs(a) << endl;
}

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

int main() {
  long long a, d, c;
  cin >> a >> d >> c;
  a = abs(a);
  long long step = min(d, a / c);
  d -= step;
  a -= step * c;
  if (d % 2 == 0)
    cout << a << endl;
  else
    cout << c - a << endl;
}
Problem p02584 (C++)

Buggy submission (status: 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;
  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;
  }
}

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;

  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 (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ABS(ll a) {
  if (a < 0)
    return -a;
  else
    return a;
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  for (ll i = 0; i < K;) {
    if (ABS(X + D) > ABS(X - D)) {
      X = X - D;
    } else
      X = X + D;

    i++;
    if (ABS(X) <= D) {
      if ((K - i) % 2 == 0)
        break;
      else {
        if (ABS(X + D) > ABS(X - D)) {
          X = X - D;
        } else
          X = X + D;
        break;
      }
    }
  }
  cout << ABS(X) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ABS(ll a) {
  if (a < 0)
    return -a;
  else
    return a;
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  ll i = min(ABS(X) / D, K);
  X = ABS(X) - D * i;
  for (; i < K;) {
    if (ABS(X + D) > ABS(X - D)) {
      X = X - D;
    } else
      X = X + D;

    i++;
    if (ABS(X) <= D) {
      if ((K - i) % 2 == 0)
        break;
      else {
        if (ABS(X + D) > ABS(X - D)) {
          X = X - D;
        } else
          X = X + D;
        break;
      }
    }
  }
  cout << ABS(X) << endl;
  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
  __int128 x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void print(__int128 x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  LL x, k, d;
  while (x = read(), k = read(), d = read()) {
    if (x < 0)
      x = -x;
    if (x >= 0) {
      if ((x - k * d) >= 0) {
        print(x - k * d);
      } else {
        int t = (x / d);
        if ((k - t) % 2)
          print(-(x - d * t - d));
        else
          print(x - d * t);
      }
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
  __int128 x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void print(__int128 x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  LL x, k, d;
  x = read(), k = read(), d = read();

  if (x < 0)
    x = -x;
  if (x >= 0) {
    if ((x - k * d) >= 0) {
      print(x - k * d);
    } else {
      int t = (x / d);
      if ((k - t) % 2)
        print(-(x - d * t - d));
      else
        print(x - d * t);
    }
  }

  return 0;
}
Problem p02584 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

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

  long long X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  long long num = abs(X) / D;

  if (K >= num) {
    K -= num;
    X -= num * D;
  }

  for (K; K > 0; --K) {
    if (X <= 0) {
      break;
    }
    X -= D;
  }

  if (K % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

  return 0;
}


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

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

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

  long long X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  long long num = abs(X) / D;

  if (K >= num) {
    K -= num;
    X -= num * D;
  } else {
    X -= K * D;
    K = 0;
  }

  for (K; K > 0; --K) {
    if (X <= 0) {
      break;
    }
    X -= D;
  }

  if (K % 2 == 0) {
    cout << abs(X) << endl;
  } else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

  return 0;
}