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

#include <bits/stdc++.h>
// using namespace std;
int main(void) {
  int N;
  int ans = 0;
  std::cin >> N;
  std::vector<int> vec(N, 0);
  for (int i = 0; i < N; i++) {
    // L_iを代入
    std::cin >> vec[i];
  }
  // i<j<kとなるようにforループを回す
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        int a = vec[i];
        int b = vec[j];
        int c = vec[k];
        // 三角形の成立条件を満たし、かつどの二辺も同じ長さでない
        if (a + b > c && a + c > b && b + c > a && a != b && b != c && a != c) {
          ans++;
        }
        // std::cout << i << j << k << std::endl;//デバッグ
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int N = 0;
  int count = 0;
  int L[N];
  cin >> N;
  for (int l = 0; l < N; l++) {
    cin >> L[l];
  }
  for (int i = 0; i < N - 2; i++) {
    for (int j = 1; j >= i && j < N - 1; j++) {
      for (int k = 2; k >= j && k < N; k++) {
        if (L[i] == L[j] || L[j] == L[k] || L[k] == L[i] ||
            L[i] + L[j] <= L[k] || L[j] + L[k] <= L[i] || L[k] + L[i] <= L[j]) {
          count = count + 0;
          cout << i << j << k << endl;
          // cout << L[i]==L[j] << endl;
        } else {
          count = count + 1;
          cout << i << j << k << endl;
        }
      }
    }
  }
  cout << count << endl;
}
Problem p02583 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> L(n);
  for (int &Li : L)
    cin >> Li;

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

  int ans = 0;

  for (int i = 0; i < n - 2; ++i) {
    for (int j = i + 1; j < n - 1; ++j) {
      if (L.at(i) == L.at(j))
        continue;
      for (int k = j + 1; k < n; ++k) {
        if (L.at(j) == L.at(k))
          continue;
        if (L.at(i) + L.at(j) > L.at(k))
          ans++;
        else
          break;
      }
    }
  }

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using ulli = unsigned long long;

int main() {
  size_t n;
  cin >> n;
  vector<int> L(n);
  for (int &Li : L)
    cin >> Li;

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

  int ans = 0;

  for (size_t i = 0UL; i < n - 2UL; ++i) {
    for (size_t j = i + 1UL; j < n - 1UL; ++j) {
      if (L.at(i) == L.at(j))
        continue;
      for (size_t k = j + 1UL; k < n; ++k) {
        if (L.at(j) == L.at(k))
          continue;
        if (L.at(i) + L.at(j) > L.at(k))
          ans++;
      }
    }
  }

  cout << ans << '\\n';

  return 0;
}
Problem p02583 (C++) — buggy submission, verdict: 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 < 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;
}

Error: TLE

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 < 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;
}
Problem p02583 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  set<string> sol;
  ll res = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      for (ll k = j + 1; k < n; k++) {
        if (l[i] + l[j] > l[k] && l[i] != l[j] && l[i] != l[k] &&
            l[j] != l[k] && l[i] + l[k] > l[j] && l[j] + l[k] > l[i]) {
          res++;
        }
      }
    }
  }

  cout << res;

  return 0;
}

Error: WA

Fixed submission:

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

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

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

  set<string> sol;
  ll res = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      for (ll k = j + 1; k < n; k++) {
        if (l[i] + l[j] > l[k] && l[i] != l[j] && l[i] != l[k] &&
            l[j] != l[k] && l[i] + l[k] > l[j] && l[j] + l[k] > l[i]) {
          res++;
        }
      }
    }
  }

  cout << res;

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  ll ans = 0;
  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[i] + a[j] > a[k] &&
            a[j] + a[k] > a[i]) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

  ll ans = 0;
  for (int i = 1; i < 101; i++) {
    for (int j = i + 1; j < 101; j++) {
      for (int k = j + 1; k < 101; k++) {
        if (a[i] > 0 && a[j] > 0 && a[k] > 0 && i + j > k && j + k > i) {
          ans += a[i] * a[j] * a[k];
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02583 (C++) — buggy submission, verdict: 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 < 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;
}


Error: TLE

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 < 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;
}
Problem p02583 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int ans = 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[k] && a[i] != a[j] && a[i] != a[k] &&
            a[j] != a[k]) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int ans = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; j < n; k++) {
        if (a[i] + a[j] > a[k]) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02583 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  long long n, i, k, j, c = 0, s;
  cin >> n;
  vector<long long> v;
  for (i = 0; i < n; i++) {
    cin >> k;
    v.push_back(k);
  }
  // vector<long long>v;
  /* for(auto i:st)
   {
       v.push_back(i);
      // st.erase(i);
   }*/
  sort(v.begin(), v.end());
  s = v.size();
  for (i = 0; i < s - 2; i++) {
    for (j = i + 1; j < s - 1; j++) {
      for (k = j + 1; k < s; k++) {
        if ((v[i] + v[j] > v[k]) &&
            (v[i] != v[j] && v[j] != v[k] && v[k] != v[i])) {
          c++;
        }
      }
    }
  }
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  long long n, i, k, j, c = 0;
  cin >> n;
  vector<long long> v;
  for (i = 0; i < n; i++) {
    cin >> k;
    v.push_back(k);
  }
  // vector<long long>v;
  /* for(auto i:st)
   {
       v.push_back(i);
      // st.erase(i);
   }*/
  sort(v.begin(), v.end());
  for (i = 0; i < v.size() - 2; i++) {
    for (j = i + 1; j < v.size() - 1; j++) {
      for (k = j + 1; k < v.size(); k++) {
        if ((v[i] + v[j] > v[k]) &&
            (v[i] != v[j] && v[j] != v[k] && v[k] != v[i])) {
          c++;
        }
      }
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02583 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int a[110];
map<int, int> num;
map<pair<PII, int>, int> res;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    num[a[i]]++;
  }
  LL ans = 0;
  for (int i = 1; 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[i] == a[k])
          continue;
        int m[3] = {a[i], a[j], a[k]};
        sort(m, m + 3);
        int x = m[0], y = m[1], z = m[2];
        if (x + y > z && x + z > y && y + z > x) {
          if (res[{{x, y}, z}])
            continue;
          res[{{x, y}, z}]++;
          ans += num[x] * num[y] * num[z];
        }
      }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int a[110];
map<int, int> num;
vector<pair<pair<int, int>, int>> res;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    num[a[i]]++;
  }
  LL ans = 0;
  for (int i = 1; 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[i] == a[k])
          continue;
        int m[3] = {a[i], a[j], a[k]};
        sort(m, m + 3);
        int x = m[0], y = m[1], z = m[2];
        if (x + y > z && x + z > y && y + z > x) {
          int flag = 0;
          for (int s = 0; s < res.size(); s++) {
            pair<PII, int> p = res[s];
            if ((p.first).first == x && (p.first).second == y &&
                p.second == z) {
              flag = 1;
              break;
            }
          }
          if (flag)
            continue;
          res.push_back({{x, y}, z});
          ans += num[x] * num[y] * num[z];
        }
      }
  cout << ans << endl;
  return 0;
}
Problem p02583 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define PI 3.14159265358979323846264338327950L

ll res;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  res = 0;
  vector<ll> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }
  sort(L.begin(), L.end());
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < j; k++) {
        if (L[k] != L[j] && L[i] != L[j] && L[k] + L[j] > L[i]) {
          res++;
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define PI 3.14159265358979323846264338327950L

ll res;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  res = 0;
  vector<ll> L(N);
  vector<ll> M(101, 0);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }
  for (int i = 0; i < N; i++) {
    M[L[i] - 1]++;
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
      for (int k = 0; k < 100; k++) {
        if ((i + 1 + j + 1 > k + 1) && (i < j) && (j < k)) {
          res += M[i] * M[j] * M[k];
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}
Problem p02583 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;

bool notequal(int x, int y, int z) {
  if (x != y && y != z && z != x) {
    return true;
  }
  return false;
}
bool isTriangle(int minX, int midX, int maxX) {
  if (maxX < midX + minX) {
    return true;
  }
  return false;
}

struct Li {
  int count;
  int x;
};

int main() {
  int N;
  vector<Li> L;
  cin >> N;
  int t = 0;
  for (int i = 0; i < N; ++i) {
    cin >> t;
    bool addf = true;
    for (int j = 0; j < L.size(); ++j) {
      if (L[j].x == t) {
        L[j].count++;
        addf = false;
        break;
      }
      if (L[j].x < t) {
        Li l;
        l.x = t;
        l.count = 1;
        L.insert(L.begin() + j, l);
        addf = false;
        break;
      }
    }
    if (addf) {
      Li l;
      l.x = t;
      l.count = 1;
      L.push_back(l);
    }
  }

  ul count = 0;

  for (int i = 0; i < L.size(); ++i) {
    for (int j = i + 1; j < L.size(); ++j) {
      for (int k = j + 1; k < L.size(); ++k) {
        if (L[i].x >= L[j].x + L[k].x) {
          break;
        }
        // cout << L[i].x << " " << L[j].x << " " << L[k].x << endl;
        count += L[i].count * L[j].count * L[k].count;
      }
    }
  }

  cout << count << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;

bool notequal(int x, int y, int z) {
  if (x != y && y != z && z != x) {
    return true;
  }
  return false;
}
bool isTriangle(int minX, int midX, int maxX) {
  if (maxX < midX + minX) {
    return true;
  }
  return false;
}

int main() {
  int N;
  int L[101] = {0};
  cin >> N;
  int t = 0;
  int maxX = 0;
  for (int i = 0; i < N; ++i) {
    cin >> t;
    L[t]++;
    if (maxX < t) {
      maxX = t;
    }
  }
  ul count = 0;
  for (int i = 1; i <= maxX; ++i) {
    for (int j = i + 1; j <= maxX; ++j) {
      for (int k = j + 1; k < i + j; ++k) {
        if (isTriangle(i, j, k)) {
          count += L[i] * L[j] * L[k];
        }
      }
    }
  }
  cout << count << endl;
}
Problem p02583 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> lens(n);

  for (int i = 0; i < n; i++) {
    cin >> lens.at(i);
  }

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

  vector<int> origin_lens(n);
  origin_lens = lens;

  lens.erase(unique(lens.begin(), lens.end()), lens.end());

  vector<int> num(lens.size());

  for (int i = 0; i < lens.size(); i++) {
    num.at(i) = count(origin_lens.begin(), origin_lens.end(), lens.at(i));
  }

  // for(int i = 0; i < lens.size(); i++){
  //     cout << num.at(i) << ", ";
  //     cout << lens.at(i) << endl;
  // }

  int count = 0;
  if (lens.size() < 3) {
    cout << count << endl;
    return 0;
  }
  for (int i = 0; i < lens.size() - 2; i++) {
    for (int j = i + 1; j < lens.size() - 1; j++) {
      for (int k = j + 1; k < lens.size(); k++) {
        if (abs(lens.at(i) - lens.at(j)) < lens.at(k) &&
            lens.at(k) < lens.at(i) + lens.at(j)) {
          count += num.at(i) * num.at(j) * num.at(k);
        }
      }
    }
  }

  cout << count << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> lens(n);

  for (int i = 0; i < n; i++) {
    cin >> lens.at(i);
  }

  int count = 0;
  for (int i = 0; i < lens.size() - 2; i++) {
    for (int j = i + 1; j < lens.size() - 1; j++) {
      for (int k = j + 1; k < lens.size(); k++) {
        if (lens.at(i) != lens.at(j) && lens.at(j) != lens.at(k) &&
            lens.at(k) != lens.at(i) &&
            abs(lens.at(i) - lens.at(j)) < lens.at(k) &&
            lens.at(k) < lens.at(i) + lens.at(j)) {
          count++;
        }
      }
    }
  }

  cout << count << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int64_t f(int64_t X, int64_t K, int64_t D, int64_t a) {
  return abs((2 * a - K) * D + X);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t X, K, D;

  cin >> X >> K >> D;

  int64_t a = K / 2 - X / (2 * D);
  // int64_t a = (K * D - X) / (2 * D);
  int64_t ap = a + 1;
  int64_t am = a - 1;
  vector<int64_t> tmp;
  if (0 <= a and a <= K)
    tmp.push_back(f(X, K, D, a));
  if (0 <= ap and ap <= K)
    tmp.push_back(f(X, K, D, ap));
  if (0 <= am and am <= K)
    tmp.push_back(f(X, K, D, am));
  if (tmp.size() <= 0) {
    int64_t tmp1 = f(X, K, D, 0);
    int64_t tmp2 = f(X, K, D, K);
    if (tmp1 > tmp2) {
      cout << tmp2;
    } else {
      cout << tmp1;
    }
  } else {
    cout << *min_element(tmp.begin(), tmp.end());
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int64_t f(int64_t X, int64_t K, int64_t D, int64_t a) {
  return abs(2 * a * D - K * D + X);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t X, K, D;

  cin >> X >> K >> D;

  int64_t a = (K * D - X) / (2 * D);
  int64_t ap = a + 1;
  int64_t am = a - 1;
  vector<int64_t> tmp;
  if (0 <= a and a <= K)
    tmp.push_back(f(X, K, D, a));
  if (0 <= ap and ap <= K)
    tmp.push_back(f(X, K, D, ap));
  if (0 <= am and am <= K)
    tmp.push_back(f(X, K, D, am));
  if (tmp.size() < 0) {
    int64_t tmp1 = f(X, K, D, 0);
    int64_t tmp2 = f(X, K, D, K);
    if (tmp1 > tmp2) {
      cout << tmp2;
    } else {
      cout << tmp1;
    }
  } else {
    cout << *min_element(tmp.begin(), tmp.end());
  }

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

ll X, K, D;
void input() { cin >> X >> K >> D; }

void solve() {
  if (X == 0) {
    if (K % 2 == 0)
      cout << 0 << endl;
    else
      cout << D << endl;
    return;
  } else {
    if (X < 0)
      X = -X;
    ll pl = X % D, mi = D - pl;
    ll to_pl = X / D;
    if (to_pl > K) {
      cout << X - K * D << endl;
    } else {
      if ((K - to_pl) % 2 == 0)
        cout << pl << endl;
      else
        cout << mi << endl;
    }
  }
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
const ll INF = 100000000000000000;

ll X, K, D;
void input() { cin >> X >> K >> D; }

void solve() {
  if (X == 0) {
    if (K % 2 == 0)
      cout << 0 << endl;
    else
      cout << D << endl;
    return;
  } else {
    if (X < 0)
      X = -X;
    ll pl_min = X, mi_min = INF;
    while (K > 0) {
      X -= D;
      --K;
      if (X >= 0)
        pl_min = X;
      else {
        mi_min = abs(X);
        break;
      }
    }
    if (mi_min == INF)
      cout << pl_min << endl;
    else {
      if (K % 2 == 0)
        cout << mi_min << endl;
      else
        cout << pl_min << endl;
    }
  }
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
#define max_num 1000000000000000000
#define max_itr 10000000

int main() {

  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x; // xを正にする

  // k回移動しても0を超えない場合
  if (x / d > k) {
    x -= d * k;
    cout << x << endl;
    return 0;
  } else {
    ll e = x / d;
    k -= e;
    x -= e * d;
    if (k % 2 == 1)
      x = abs(x - d);
    cout << x << endl;
    return 0;
  }
}


Error: TLE

Fixed submission:

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

using ll = long long;

int main() {

  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  ll ans = x;

  if (x / k / d > 1) {
    for (int i = 0; i < k; i++)
      x -= d;
    cout << x << endl;
    return 0;
  }

  while (1) {
    k--;
    x -= d;
    if (x <= 0) {
      if (k % 2 == 0) {
        ans = abs(x);
        cout << ans << endl;
        return 0;
      } else {
        cout << ans << endl;
        return 0;
      }
    }
    ans = min(ans, abs(x));
  }

  cout << -1 << endl;
}