alexjercan/bugnet huggingface dataset:
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> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  int b = a[N - 1] - a[0];
  for (int i = 1; i < N; i++) {
    b = min(b, K - a[i] + a[i - 1]);
  }
  cout << b << endl;
}


Error: WA

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];
  }

  cout << a.at(N - 1) - a.at(0) << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 100;
ll a[maxn];
int main() {
  int n, k;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  n++;
  ll sum = 0, mx = 0;
  for (int i = 2; i <= n; i++) {
    sum += a[i] - a[i - 1];
    mx = max(mx, a[i] - a[i - 1]);
  }
  sum -= mx;
  cout << sum << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
ll a[maxn];
int main() {
  int n, k;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  n++;
  ll sum = 0, mx = 0;
  for (int i = 2; i <= n; i++) {
    sum += a[i] - a[i - 1];
    mx = max(mx, a[i] - a[i - 1]);
  }
  sum -= mx;
  cout << sum << endl;

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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1e18;

#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (ll i = ((int)(n)); i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define pb push_back

ll K, N;

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

  cin >> K >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  ll sumMigi = 0;
  ll sumHidari = 0;
  ll maxDiff = 0;
  ll diff = 0;
  // 右回り
  rep(i, N) {
    if (i == N - 1) {
      diff = K - A[i] + A[0];
    } else {
      diff = A[i + 1] - A[i];
    }
    sumMigi += diff;
    if (maxDiff < diff) {
      maxDiff = diff;
    }
  }
  sumMigi -= maxDiff;

  reverse(all(A));

  rep(i, N) {
    if (i == N - 1) {
      diff = A[i] + K - A[0];
    } else {
      diff = A[i] - A[i + 1];
    }
    sumHidari += diff;
    if (maxDiff < diff) {
      maxDiff = diff;
    }
  }
  sumHidari -= maxDiff;

  cout << min(sumMigi, sumHidari) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1e18;

#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (ll i = ((int)(n)); i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define pb push_back

ll K, N;

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

  cin >> K >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  ll sum = 0;
  ll min = INF;
  // 右回り
  rep(i, N - 1) {
    sum = 0;
    rep(j, N - 1) {
      if (i + j == N - 1) {
        sum += K - A[(i + j) % N] + A[(i + j + 1) % N];
      } else {
        sum += A[(i + j + 1) % N] - A[(i + j) % N];
      }
    }
    if (sum < min) {
      min = sum;
    }
  }

  reverse(all(A));

  rep(i, N - 1) {
    sum = 0;
    rep(j, N - 1) {
      if (i + j == N - 1) {
        sum += A[(i + j) % N] + K - A[(i + j + 1) % N];
      } else {
        sum += A[(i + j) % N] - A[(i + j + 1) % N];
      }
    }
    if (sum < min) {
      min = sum;
    }
  }

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

#include <math.h>
#include <stdio.h>
#include <string.h>
#define rep(i, n) for (i = 0; i < n; i++)

int main(void) {
  int i, n, k, d, min;
  scanf("%d%d", &k, &n);
  int a[n];
  rep(i, n) scanf("%d", &a[i]);

  min = a[n - 1] - a[0];
  for (i = 1; i < n; i++) {
    d = k - (a[i] - a[i - 1]);
    if (d < min) {
      min = d;
    }
  }
  printf("%d", min);
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (i = 0; i < N; i++)
int main() {
  int i, N, d, x, K, A[N];
  cin >> K >> N;
  rep(i, N) { cin >> A[i]; }
  d = A[N - 1] - A[0];
  for (i = 1; i < N; i++) {
    x = K - (A[i] - A[i - 1]);
    if (x < d)
      d = x;
  }
  cout << x;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int N;

int main() {
  int K, i, L, a1, an, am;
  cin >> K >> N;
  L = 0;
  cin >> a1;
  am = a1;
  for (i = 2; i <= N; i++) {
    cin >> an;
    if (L < an - am)
      L = an - am;
    am = an;
  }
  if (L < K - an + a1)
    L = K - an + a1;
  cout << K - L << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int N;
int A[20010];

int main() {
  int K, i, L;
  cin >> K >> N;
  for (i = 1; i <= N; i++) {
    cin >> A[i];
  }

  L = K - A[N] + A[1];
  for (i = 1; i < N; i++) {
    if (L < A[i + 1] - A[i])
      L = A[i + 1] - A[i];
  }
  cout << L << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll a[N], b[N];
#define mst(a) memset(a, 0, sizeof a)
int main() {
  ll k, n;
  while (~scanf("%lld%lld", &k, &n)) {
    ll mx = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
      scanf("%lld", &a[i]);
    int f = 0;
    ll ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++) {
      if (i == 1)
        ans = min(ans, min(k - (a[2] - a[1]), a[n] - a[1]));
      else if (i == n)
        ans = min(ans, min(k - (a[n] - a[n - 1]), a[n] - a[1]));
      else
        ans = min(ans, k - max(a[i] - a[i - 1], a[i + 1] - a[i]));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll a[N], b[N];
#define mst(a) memset(a, 0, sizeof a)
int main() {
  ll k, n;
  while (~scanf("%lld%lld", &k, &n)) {
    for (int i = 1; i <= n; i++)
      scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        b[i] += min(abs(a[i] - a[j]), k - abs(a[i] - a[j]));
    sort(b + 1, b + n + 1);
    printf("%lld\\n", b[1]);
  }
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

const int maxn = 200005;

int a[2 * maxn];
ll t[2 * maxn];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm

  int n, k;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i + n] = a[i];

  for (int i = 2; i <= 2 * n; i++) {
    t[i] = t[i - 1] + (a[i] - a[i - 1] + k) % k;
  }

  ll sol = 1LL << 60;
  for (int i = n; i <= 2 * n; i++)
    sol = min(sol, t[i] - t[i - n + 1]);

  cout << sol << "\\n";

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;

const int maxn = 200005;

int a[maxn];
ll t[maxn];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm

  int n, k;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i + n] = a[i];

  for (int i = 2; i <= 2 * n; i++) {
    t[i] = t[i - 1] + (a[i] - a[i - 1] + k) % k;
  }

  ll sol = 1LL << 60;
  for (int i = n; i <= 2 * n; i++)
    sol = min(sol, t[i] - t[i - n + 1]);

  cout << sol << "\\n";

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define make_pair mkp;

const int maxn = 2e5 + 10;
ll dist[maxn];
int a[maxn];

int main() {
  memset(dist, 0, sizeof(dist));
  int k, n;
  cin >> k >> n;
  // ll ans=0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  // ll res=0;
  dist[0] = min(a[n - 1] - a[0], a[0] + k - a[1]);
  dist[n - 1] = min(a[n - 1] - a[0], a[n - 2] + k - a[n - 1]);
  for (int i = 1; i < n - 1; i++) {
    dist[i] = min(a[i - 1] + k - a[i], a[i] + k - a[i + 1]);
  }
  ll ans = 1e15;
  for (int i = 0; i < n; i++) {
    ans = min(ans, dist[i]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define make_pair mkp;

const int maxn = 1e5 + 10;
int dist[maxn];
int a[maxn];

int main() {
  memset(dist, 0, sizeof(dist));
  int k, n;
  cin >> k >> n;
  // ll ans=0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dist[0] = min(a[n - 1] - a[0], a[0] + k - a[1]);
  dist[n - 1] = min(a[n - 1] - a[0], a[n - 2] + k - a[n - 1]);
  for (int i = 1; i < n - 1; i++) {
    dist[i] = max(a[i - 1] + k - a[i], a[i] + k - a[i + 1]);
  }
  int ans = 1e9;
  for (int i = 0; i < n; i++) {
    ans = min(ans, dist[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
#define PB push_back

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll k, n;
  cin >> k >> n;
  vector<ll> houses;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    houses.PB(a);
  }

  ll gap = k - houses[n - 1] + houses[0];
  ll cw = 0, m = 0, idx;
  for (ll i = 1; i < n; i++) {
    ll d = houses[i] - houses[i - 1];
    cw += d;
    if (d > m) {
      m = d;
      idx = i - 1;
    }
  }
  ll ccw = 0;
  for (ll i = idx; i > 0; i--) {
    ccw += houses[i] - houses[i - 1];
  }
  for (ll i = n - 1; i > idx + 1; i--) {
    ccw += houses[i] - houses[i - 1];
  }
  ccw += gap;

  ll res = min(cw, ccw);
  cout << res;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define PB push_back

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll k, n;
  cin >> k >> n;
  vector<ll> houses;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    houses.PB(a);
  }

  ll cw = 0;
  for (ll i = 1; i < n; i++) {
    cw += houses[i] - houses[i - 1];
  }

  ll gap = k - houses[n - 1] + houses[0];
  ll ccw = numeric_limits<ll>::max();
  if (gap < cw) {
    for (ll idx = 0; idx < n; idx++) {
      ll c = 0;
      for (ll i = idx; i > 0; i--) {
        c += houses[i] - houses[i - 1];
      }
      for (ll i = n - 1; i > idx + 1; i--) {
        c += houses[i] - houses[i - 1];
      }
      c += gap;
      ccw = min(c, ccw);
    }
  }

  ll res = min(cw, ccw);
  cout << res;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (ll i = (a); i <= (signed)(b); i++)
#define F0R(i, a) for (ll i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 200005
int n, k;
int a[MN];

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

  cin >> k >> n;
  F0R(i, n) { cin >> a[i]; }
  int mxd = 0;
  F0R(i, n - 1) { mxd = max(mxd, a[i + 1] - a[i]); }
  mxd = max(mxd, a[0] + k - a[n - 1]);

  cout << k - mxd << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (ll i = (a); i <= (signed)(b); i++)
#define F0R(i, a) for (ll i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 100005
int n, k;
int a[MN];

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

  cin >> k >> n;
  F0R(i, n) { cin >> a[i]; }
  int mxd = 0;
  F0R(i, n - 1) { mxd = max(mxd, a[i + 1] - a[i]); }
  mxd = max(mxd, a[0] + k - a[n - 1]);

  cout << k - mxd << "\\n";

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

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

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

Error: 0

Fixed submission:

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

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<int> d(N);
  rep(i, N) {
    if (i == N - 1) {
      d[N - 1] = A[0] + (K - A[N - 1]);
      break;
    }
    d[i] = A[i + i] - A[i];
  }
  int max = 0;
  rep(i, N) {
    if (max < d[i])
      max = d[i];
  }
  cout << K - max << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, i, a[500005], mi, x;
int main() {
  scanf("%lld%lld", &k, &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[n + i] = a[i] + k;
  }
  sort(a + 1, a + n * 2 + 1);
  mi = k;
  for (i = n; i < 2 * n; i++) {
    x = a[i] - a[i - n + 1];
    if (x < mi)
      mi = x;
  }
  printf("%lld\\n", mi);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, i, a[200005], mi, x;
int main() {
  scanf("%lld%lld", &k, &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[n + i] = a[i] + k;
  }
  sort(a + 1, a + n * 2 + 1);
  mi = k;
  for (i = n; i < 2 * n; i++) {
    x = a[i] - a[i - n + 1];
    if (x < mi)
      mi = x;
  }
  printf("%lld\\n", mi);
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int k, n;
  int *a;
  int i, j, max = 0, count = 0;
  int *dis;

  scanf("%d %d", &k, &n);

  a = (int *)malloc(sizeof(int) * n);
  dis = (int *)malloc(sizeof(int) * n);

  for (i = 0; i < n; i++) {
    scanf("%d", (a + i));
  }

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

  for (i = 1; i < n; i++) {
    if (dis[i] > max) {
      max = dis[i];
    }
  }

  printf("%d\\n", k - max);

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int k, n;
  int *a;
  int i, j, min = 0, count = 0;
  int *sum;

  scanf("%d %d", &k, &n);

  a = (int *)malloc(sizeof(int) * n);
  sum = (int *)malloc(sizeof(int) * n);

  for (i = 0; i < n; i++) {
    scanf("%d", (a + i));
  }

  for (i = 0; i < n; i++) {
    sum[i] = 0;
    count = 0;

    for (j = i;;) {

      if (j == (n - 1)) {
        sum[i] += ((k - a[j]) + a[0]);
        j = 0;
      } else if (j < (n - 1)) {
        sum[i] += (a[j + 1] - a[j]);
        j++;
      } else {
        // 処理なし
      }

      count++;

      if (count == (n - 1)) {
        break;
      } else {
        // 処理なし
      }
    }
    // printf("%d\\n" ,sum[i]);
  }

  min = sum[0];
  for (i = 1; i < n; i++) {
    if (sum[i] < min) {
      min = sum[i];
    }
  }

  printf("%d\\n", min);

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

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

int main() {
  int i = 0, temp, k, n;
  int al, ac, ar, a0, a1, af;
  int min, minTemp;
  int r, l;

  cin >> k >> n;
  cin >> al >> ac >> ar;

  a0 = al;
  a1 = ac;

  do {
    l = k - (ar - ac);
    r = k - (ac - al);
    if (r < l) {
      minTemp = r;
    } else {
      minTemp = l;
    }
    if (minTemp < min) {
      min = minTemp;
    }
    al = ac;
    ac = ar;
  } while (cin >> ar);

  af = ar;

  l = af - a0;
  r = k - (af - ac);
  if (r < l) {
    minTemp = r;
  } else {
    minTemp = l;
  }
  if (minTemp < min) {
    min = minTemp;
  }

  l = k - (a1 - a0);
  r = af - a0;
  if (r < l) {
    minTemp = r;
  } else {
    minTemp = l;
  }
  if (minTemp < min) {
    min = minTemp;
  }

  cout << min << endl;
}


Error: -11

Fixed submission:

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

int main() {
  int i = 0, temp, k, n;
  int *a;
  int min, minTemp;
  int r, l;

  cin >> k >> n;

  while (cin >> temp) {
    a[i] = temp;
    i++;
  }

  l = k - (a[1] - a[0]);
  r = a[n - 1] - a[0];
  if (r < l) {
    minTemp = r;
  } else {
    minTemp = l;
  }
  min = minTemp;

  for (i = 1; i < n - 1; i++) {
    l = k - (a[i + 1] - a[i]);
    r = k - (a[i] - a[i - 1]);
    if (r < l) {
      minTemp = r;
    } else {
      minTemp = l;
    }
    if (minTemp < min) {
      min = minTemp;
    }
  }

  l = a[n - 1] - a[0];
  r = k - (a[n - 1] - a[n - 2]);
  if (r < l) {
    minTemp = r;
  } else {
    minTemp = l;
  }
  if (minTemp < min) {
    min = minTemp;
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
int a[200010];
int main() {
  int n, k;
  cin >> k >> n;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      mx = max(mx, a[i] - a[i - 1]);
    }
  }
  mx = max(mx, a[0] + k - a[n - 1]);
  cout << k - mx << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
int a[100010];
int main() {
  int n, k;
  cin >> k >> n;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      mx = max(mx, a[i] - a[i - 1]);
    }
  }
  mx = max(mx, a[0] + k - a[n - 1]);
  cout << k - mx << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define mp make_pair
#define sz size()
#define pb push_back
using ll = long long;
using pii = pair<int, int>;

const int N = 2e5 + 5;
ll A[N];

int main() {

  ll k, n;
  cin >> k >> n;
  ll x = 0LL;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (i)
      x = max(x, A[i] - A[i - 1]);
  }
  x = max(x, A[0] - A[n - 1] + k);
  cout << k - x << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define mp make_pair
#define sz size()
#define pb push_back
using ll = long long;
using pii = pair<int, int>;

const int N = 1e5 + 5;
int A[N];

int main() {

  ll k, n;
  cin >> k >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] > abs(A[i] - k))
      A[i] -= k;
  }

  sort(A, A + n);

  cout << A[n - 1] - A[0] << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long k, n;
long long v[200003];
long long maxx, aux;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> k >> n;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] - v[i - 1] > maxx)
      maxx = v[i] - v[i - 1];
  }
  maxx = max(maxx, v[1] + k - v[n]);
  cout << k - maxx;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int k, n;
int v[20003];
int maxx, aux;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> k >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] - v[i - 1] > maxx) {
      maxx = v[i] - v[i - 1];
      aux = i;
    }
  }
  v[n + 1] = k;
  long long ans = 0;
  for (int i = aux + 1; i <= n + 1; i++)
    ans += v[i] - v[i - 1];
  for (int i = 1; i < aux; i++)
    ans += v[i] - v[i - 1];
  if (aux == 1)
    ans -= (v[n] - v[n - 1]);
  cout << ans;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long int k, n, max = 0;
  cin >> k >> n;
  long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i + 1] - a[i] > max)
      max = a[i + 1] - a[i];
  }
  if (k - a[n - 1] + a[0] > max)
    max = k - a[n - 1] + a[0];
  cout << k - max;
  return 0;
}

Error: WA

Fixed submission:

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

#define fi first
#define mp make_pair
#define se second
#define pb push_back
#define be(x) (x).begin(), (x).end()
#define rbe(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define forl(i, n) for (int i = 1; i <= (int)(n); i++)
#define fore(i, k, n) for (int i = (int)(k); i <= (int)(n); i++)

typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> pii;

int A[100000];
int des(int i, int N) {
  vector<int> dis;
  forn(j, N) {
    if (A[i] == 0) {
      return 0;
    }
    if (i != j) {
      dis.pb(A[j] - A[i]);
    }
  }
  sort(be(dis), greater<>());
  return dis[0];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int K, N;
  cin >> K >> N;
  forn(i, N) { cin >> A[i]; }
  vector<int> res;
  forn(i, N) { res.pb(des(i, N)); }
  sort(be(res), greater<>());
  cout << res[0];
  return 0;
}