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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
// using std::cin, std::cout, std::endl, std::string;
using ll = long long;

#define rep(countName, left, right, up)                                        \\
  for (auto countName = left; countName < right; countName += up)
#define REP(countName, end)                                                    \\
  for (auto countName = 0; countName < end; countName++)
#define rev(countName, right, left, down)                                      \\
  for (auto countName = right; countName >= left; countName -= down)
#define REV(countName, end)                                                    \\
  for (auto countName = end; countName >= 0; countName--)

auto next(int now, int len) { return (now + 1) % len; }

auto solve1(int k, int n, std::vector<ll> &&v) {
  auto dist_max = 0, dist = 0;

  for (int i = 0; i < n; i++) {
    auto x = next(i, n);
    if (i < x) {
      dist = v[x] - v[i];
    } else {
      dist = k - v[i] + v[x];
    }
    dist_max = std::max(dist_max, dist);
    // cout << i << " " << x << " : " << dist << endl;
    // cout << "dist max : " << dist_max << endl;
  }

  return k - dist_max;
}

int main(void) {
  int k, n;
  cin >> k >> n;
  std::vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }
  cout << solve1(k, n, std::move(a)) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
// using std::cin, std::cout, std::endl, std::string;
using ll = long long;

#define rep(countName, left, right, up)                                        \\
  for (auto countName = left; countName < right; countName += up)
#define REP(countName, end)                                                    \\
  for (auto countName = 0; countName < end; countName++)
#define rev(countName, right, left, down)                                      \\
  for (auto countName = right; countName >= left; countName -= down)
#define REV(countName, end)                                                    \\
  for (auto countName = end; countName >= 0; countName--)

auto next(int now, int len) { return (now + 1) % len; }

auto solve1(int k, int n, std::vector<ll> &&v) {
  auto dist_min = k;
  for (auto start = 0; start < n; start++) {
    auto dist = 0;
    for (int i = next(start, n); i != start; i = next(i, n)) {
      auto x = next(i, n);
      if (i < x) {
        dist += v[x] - v[i];
      } else {
        dist += k - v[i] + v[x];
      }
      // cout << i << " " << x << " " << dist << endl;
    }
    dist_min = std::min(dist_min, dist);
    // cout << "dist min : " << dist_min << endl;
  }
  return dist_min;
}

int main(void) {
  int k, n;
  cin >> k >> n;
  std::vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }
  cout << solve1(k, n, std::move(a)) << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;

int main() {

  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  int maxa = 0;

  rep(i, n - 1) maxa = max(maxa, a[i + 1] - a[i]);
  maxa = max(maxa, k - a[n - 1] + a[0]);

  cout << k - maxa << endl;
}

Error: 0

Fixed submission:

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

int main() {

  int k, n, mdist = 0;

  vector<int> a(22000);

  cin >> k >> n;

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

  for (int i = 0; i < n - 1; i++) {
    int d;
    d = a.at(i + 1) - a.at(i);
    mdist = max(mdist, d);
  }

  mdist = max(mdist, k + a.at(0) - a.at(n - 1));

  cout << k - mdist << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll k, n;
  cin >> k >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  ll ans = LLONG_MIN;
  for (ll i = 1; i < n; i++)
    ans = max(ans, arr[i] - arr[i - 1]);
  ans = max(ans, (k - arr[n - 1]) + arr[0]);
  cout << k - ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll k, n;
  cin >> k >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  ll fans = LLONG_MAX, ans1, ans2;
  for (ll i = 0; i < n; i++) {
    ans1 = 0;
    ll j = (i + 1) % n;
    while (arr[j] != arr[i]) {
      ans1 += min(abs(arr[(j) % n] - arr[(j - 1 + n) % n]),
                  k - abs(arr[(j) % n] - arr[(j - 1 + n) % n]));
      j = (j + 1) % n;
    }

    ans2 = 0;
    j = (i - 1 + n) % n;
    while (arr[j] != arr[i]) {
      ans2 += min(abs(arr[(j) % n] - arr[(j + 1) % n]),
                  k - abs(arr[(j) % n] - arr[(j + 1) % n]));
      j = (j - 1 + n) % n;
    }
    fans = min(ans1, ans2);
  }
  cout << fans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  long long int k, n;
  long long int a[200000] = {0};
  long long int i, max = 0;

  cin >> k >> n;
  cin >> a[0];
  for (i = 1; i < n; i++) {
    cin >> a[i];
  }

  for (i = 1; i < n; i++) {
    if (max < (a[i] - a[i - 1])) {
      max = a[i] - a[i - 1];
    }
  }
  if (max < (k + a[0] - a[n - 1])) {
    max = k + a[0] - a[n - 1];
  }

  cout << k - max << "\\n";
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long int k, n;
  long long int a[100000] = {0};
  long long int i, max = 0;

  cin >> k >> n;
  cin >> a[0];
  for (i = 1; i < n; i++) {
    cin >> a[i];
    if (max < (a[i - 1] - a[i])) {
      max = a[i - 1] - a[i];
    }
  }
  if (max < (a[0] + a[n])) {
    max = a[0] + a[n];
  }

  cout << k - max << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
ll a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll k;
  int n;
  cin >> k >> n;
  rep(i, 1, n) { cin >> a[i]; }
  ll Max = 0;
  rep(i, 2, n) Max = max(Max, a[i] - a[i - 1]);
  Max = max(Max, a[1] + k - a[n]);
  cout << k - Max << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
ll a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll k;
  int n;
  cin >> k >> n;
  rep(i, 1, n) { cin >> a[i]; }
  ll Max = 0;
  rep(i, 2, n) Max = max(Max, a[i] - a[i - 1]);
  Max = max(Max, a[1] + k - a[n]);
  cout << k - Max << '\\n';
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  int K = 0, N = 0;
  int *A = NULL;
  int d = 0, D = 0;

  cin >> K;
  cin >> N;

  A = (int *)calloc(N + 1, sizeof(int));

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i == 1) {
      d = 0;
    } else if (i > 1) {
      d = A[i] - A[i - 1];
    }
    if (d > D) {
      D = d;
    }
  }
  if (D < K - A[N] + A[1]) {
    D = K - A[N] + A[1];
  }
  printf("%d\\n", K - D);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  int K = 0, N = 0;
  int *A = NULL;
  int d = 0, D = 0;

  cin >> K;
  cin >> N;

  A = (int *)calloc(N + 1, N);

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i == 1) {
      d = 0;
    } else if (i > 1) {
      d = A[i] - A[i - 1];
    }
    if (d > D) {
      D = d;
    }
  }
  if (D < K - A[N] + A[1]) {
    D = K - A[N] + A[1];
  }
  printf("%d\\n", K - D);
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int k;
  int n;
  cin >> k;
  cin >> n;
  int a[n];
  int b[n]; // 家と家との距離
  int l = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int h = 0; h < n; h++) {
    if (h != n - 1) {
      b[h] = a[h + 1] - a[h]; //(i+1)番目と(i+2)番目との距離
    } else {
      b[h] = k + a[0] - a[h]; // n番目と1番目との距離
    }
  }
  // 家と家との距離の最大値を求める
  for (int j = 0; j < n; j++) {
    if (b[j] >= l) {
      l = b[j];
    } else {
    }
  }
  cout << k - l << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int k;
  int n;
  int a[n];
  cin >> k;
  cin >> n;
  int b[n];
  int l = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != n - 1) {
      b[i] = a[i] - a[i + 1]; //(i+1)番目と(i+2)番目との距離
    } else {
      b[i] = k + a[0] - a[i]; // n番目と1番目との距離
    }
  }
  // 家と家との距離の最大値を求める
  for (int j = 0; j < n; j++) {
    if (b[j] - b[(j + 1) % n] > 0) {
      l = b[j];
    } else {
      l = l;
    }
  }
  cout << k - l << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  int ans = -1;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i > 0) {
      ans = max(ans, A[i] - A[i - 1]);
    }
  }
  cout << K - max(ans, K - A[N - 1] + A[0]) << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i] != 0 && A[j] != 0 && abs(A[i] - A[j]) >= ans) {
        ans = abs(A[i] - A[j]);
      }
    }
  }
  cout << K - ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long

int main() {
  ll k, n;
  std::cin >> k >> n;

  std::vector<ll> A(n);
  ll diff = 0;

  for (ll i = 0; i < n; i++) {
    std::cin >> A[i];
    if (i > 0) {
      if ((A[i] - A[i - 1]) > diff) {
        diff = A[i] - A[i - 1];
      }
    }
  }

  ll x = k + A[0] - A[n - 1];
  if (x > diff) {
    diff = x;
  }

  std::cout << k - diff << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long

int main() {
  ll k, n;
  std::cin >> k >> n;

  std::vector<ll> A(n);
  ll diff = 0;
  ll point = 0;
  for (ll i = 0; i < n; i++) {
    std::cin >> A[i];
    if (i > 0) {
      if ((A[i] - A[i - 1]) > diff) {
        point = i;
        diff = A[i] - A[i - 1];
      }
    }
  }

  ll ans1 = 0;
  for (ll i = 1; i < n; i++) {
    int tmp1 = (point + i - 1) % n;
    int tmp2 = (point + i) % n;
    if (tmp2 == 0) {
      ans1 += k - A[n - 1];
    } else {
      ans1 += A[tmp1] > A[tmp2] ? (A[tmp1] - A[tmp2]) : (A[tmp2] - A[tmp1]);
    }
  }

  ll ans2 = 0;
  for (ll i = 0; i < (n - 1); i++) {
    int tmp1 = (point - i) % n;
    int tmp2 = (point - i - 1) % n;
    if (tmp1 == 0) {
      ans2 += k - A[n - 1];
    } else {
      ans2 += A[tmp1] > A[tmp2] ? (A[tmp1] - A[tmp2]) : (A[tmp2] - A[tmp1]);
    }
  }

  if (ans1 > ans2) {
    std::cout << ans2 << std::endl;
  } else {
    std::cout << ans1 << std::endl;
  }
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#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() {
  int K, N;
  cin >> K >> N;
  vector<int> temp(N);
  rep(i, N) cin >> temp.at(i);
  vector<int> vec(N);
  for (int i = 1; i < N; i++)
    vec.at(i) = temp.at(i) - temp.at(i - 1);
  vec.at(0) = K - temp.at(N - 1) + temp.at(0);
  cout << K - *max_element(vec.begin(), vec.end()) << endl;
}


Error: 0

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() {
  int K, N;
  cin >> K >> N;
  vector<int> temp(6);
  rep(i, N) cin >> temp.at(i);
  vector<int> vec(6);
  for (int i = 1; i < N; i++)
    vec.at(i) = temp.at(i) - temp.at(i - 1);
  vec.at(0) = K - temp.at(N - 1) + temp.at(0);
  cout << K - *max_element(vec.begin(), vec.end()) << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int K, N;
  cin >> K >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  int maximumDistance = 0, previous = 0;
  for (int i = 0; i < N; i++) {
    maximumDistance = max(maximumDistance, abs(v[i] - previous));
    previous = v[i];
  }
  maximumDistance = max(maximumDistance, K - v.back() + v[0]);
  cout << K - maximumDistance << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int K, N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  int maximumDistance = 0, previous = 0;
  for (int i = 0; i < N; i++) {
    maximumDistance = max(maximumDistance, abs(v[i] - previous));
    previous = v[i];
  }
  maximumDistance = max(maximumDistance, K - v.back() + v[0]);
  cout << K - maximumDistance << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REP2(i, x, n) for (int i = x; i <= n; i++)
#define OP(x) cout << x << endl;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long long int lli;

int main() {
  ll k, n;
  cin >> k >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  lli ans = k;
  rep(i, n) {
    lli tmp = 0;
    if (i == 0) {
      tmp = abs(a[n - 1] - a[i]);
    } else {
      tmp = abs((a[i - 1] + k) - a[i]);
    }
    ans = min(ans, tmp);
  }
  OP(ans);
}


Error: TLE

Fixed submission:

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REP2(i, x, n) for (int i = x; i <= n; i++)
#define OP(x) cout << x << endl;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long long int lli;

ll K, N;
vector<ll> A;
lli ANS;

lli calc(const vector<int> &nums) {
  lli sum = 0;
  for (int i = 0; i < N - 1; i++) {
    if (A[nums[i]] == 0) {
      sum += abs(A[nums[i + 1]] - K);
    } else if (A[nums[i] + 1] == 0) {
      sum += abs(K - A[nums[i]]);
    } else {
      sum += abs(A[nums[i + 1]] - A[nums[i]]);
    }
  }
  ANS = min(ANS, sum);
  return sum;
}

int main() {
  cin >> K >> N;
  A.resize(N);
  rep(i, N) cin >> A[i];
  vector<int> nums(N);
  iota(nums.begin(), nums.end(), 0);
  ANS = K;
  do {
    calc(nums);
  } while (next_permutation(nums.begin(), nums.end()));
  OP(ANS);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
#define ld long double
#define ll long long

const int MOD = 1000000007;
const int IINF = INT_MAX;
const ll INF = 1LL << 60;

int main() {
  int k, n;
  cin >> k >> n;
  V<int> a(n * 2);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    a[i + n] = a[i] + k;
  }

  int ans = IINF;
  for (int i = 0; i < n; i++) {
    ans = min(ans, abs(a[i + n - 1] - a[i]));
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
#define ld long double
#define ll long long

const int MOD = 1000000007;
const int IINF = INT_MAX;
const ll INF = 1LL << 60;

int main() {
  int k, n;
  cin >> k >> n;
  V<int> a(n + 2);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  a[n] = k;
  if (a[0] == 0)
    a[n + 1] = k + a[1];
  else
    a[n + 1] = k + a[0];

  int ans = IINF;
  for (int i = 0; i < n; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, d;
  cin >> n >> d;
  vector<int> arr;
  int maxi = 0;
  while (d--) {
    int x;
    cin >> x;
    if (x > maxi)
      maxi = x;
    arr.push_back(x);
  }
  sort(arr.begin(), arr.end());
  arr.push_back(n + arr[0]);
  int ash = 0, sum = 0;
  for (int i = 0; i < arr.size() - 1; i++) {
    if ((arr[i + 1] - arr[i]) > ash) {
      sum += ash;
      ash = arr[i + 1] - arr[i];
    } else {
      sum += (arr[i + 1] - arr[i]);
    }
  }
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, d;
  cin >> n >> d;
  vector<int> arr;
  while (d--) {
    int x;
    cin >> x;
    arr.push_back(x);
  }
  vector<int> ans;
  for (int i = 0; i < arr.size() - 1; i++) {
    for (int j = i + 1; j < arr.size(); j++) {
      ans.push_back(abs(arr[i] - arr[j]));
    }
  }
  sort(ans.begin(), ans.end());
  ans.push_back(n - ans[2]);
  sort(ans.begin(), ans.end());
  cout << ans[0] + ans[1];
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
int read() {
  char c;
  int w = 1;
  while ((c = getchar()) > '9' || c < '0')
    if (c == '-')
      w = -1;
  int ans = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    ans = (ans << 1) + (ans << 3) + c - '0';
  return ans * w;
}
int a, b;
int c[1000005];
signed main() {
  a = read();
  b = read();
  for (int i = 1; i <= b; i++) {
    c[i] = read();
  }
  sort(c + 1, c + b + 1);
  int maxx = 0;
  for (int i = 2; i <= b; i++) {
    maxx = max(maxx, c[i] - c[i - 1]);
  }
  maxx = max(maxx, c[1] + a - c[b]);
  cout << a - maxx << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int read() {
  char c;
  int w = 1;
  while ((c = getchar()) > '9' || c < '0')
    if (c == '-')
      w = -1;
  int ans = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    ans = (ans << 1) + (ans << 3) + c - '0';
  return ans * w;
}
int a, b;
int c[100005];
int abss(int x) {
  if (x < 0)
    x = -x;
  return x;
}
signed main() {
  a = read();
  b = read();
  for (int i = 1; i <= b; i++) {
    c[i] = read();
  }
  sort(c + 1, c + b + 1);
  int w = b + 1;
  w /= 2;
  int ans = 0;
  for (int i = 1; i <= b; i++) {
    ans = max(ans, abss(c[w] - c[i]));
  }
  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int k, n;
  cin >> k >> n;

  vector<int> a(n);
  int max = 0;
  for (auto i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 1) {
      int tmp = a[i] - a[i - 1];
      if (tmp > max) {
        max = tmp;
      }
    }
  }
  if (k - a[n - 1] + a[0] > max) {
    max = k - a[n - 1] + a[0];
  }
  if (a[1] - a[0] > max) {
    max = a[1] - a[0];
  }

  cout << (k - max) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int k, n;
  cin >> k >> n;

  vector<int> a(n);
  for (auto i = 0; i < n; i++)
    cin >> a[i];

  int ans = INT_MAX, tmp, prev;
  for (auto i = 0; i < n; i++) {
    tmp = 0;
    prev = i;
    for (auto j = 1; j < n; j++) {
      if ((i + j) % n < prev)
        tmp += (k - a[prev] + a[(i + j) % n]);
      else
        tmp += a[(i + j) % n] - a[prev];
      prev = (i + j) % n;
    }
    if (tmp < ans)
      ans = tmp;
  }

  //    for (auto i = 0; i < n; i++) {
  //        tmp = 0;
  //        prev = i;
  //        for (auto j = n; j > 0; j--) {
  //            if((i + j) % n < prev) tmp += (k - a[prev] + a[(i + j) % n]);
  //            else tmp += a[(i + j) % n] - a[prev];
  //            prev = (i + j) % n;
  //        }
  //        if(tmp < ans) ans = tmp;
  //    }

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

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int n, m;
int a[200001];
int main() {
  cin >> n >> m;
  int mx = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
  }
  a[m] = n + a[0];
  for (int i = 1; i <= m; i++)
    mx = max(a[i] - a[i - 1], mx);

  cout << n - mx;
}


Error: WA

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int n, m;
int a[200001];
int main() {
  cin >> n >> m;
  int mx = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
  }
  a[m] = n + a[0];
  for (int i = 1; i <= n; i++)
    mx = max(a[i] - a[i - 1], mx);

  cout << n - mx;
}