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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\\n';
#define PRINTVEC(x)                                                            \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    std::cout << x[i] << ' ';                                                  \\
  std::cout << '\\n';
#define PRINTP(x)                                                              \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    std::cout << x[i].first << ' ' << x[i].second << '\\n';                     \\
  std::cout << '\\n';
#define io                                                                     \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(NULL);
const int INF = 1 << 29;
typedef long long ll;

int main() {
  io;
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif

  int k, n;
  std::cin >> k >> n;
  std::vector<int> v(n);
  for (int &it : v)
    std::cin >> it;
  int ans = v[n - 1] - v[0];
  for (int i = 1; i < n; i++) {
    ans = std::min(ans, k - v[i] + v[i - 1]);
  }
  std::cout << ans;

#ifndef ONLINE_JUDGE
// std::cout << '\\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << "
// sec \\n";
#endif
  return 0;
}


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


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\\n';
#define PRINTVEC(x)                                                            \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    std::cout << x[i] << ' ';                                                  \\
  std::cout << '\\n';
#define PRINTP(x)                                                              \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    std::cout << x[i].first << ' ' << x[i].second << '\\n';                     \\
  std::cout << '\\n';
#define io                                                                     \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(NULL);
const int INF = 1 << 29;
typedef long long ll;

int main() {
  io;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int k, n;
  std::cin >> k >> n;
  std::vector<int> v(n);
  for (int &it : v)
    std::cin >> it;
  int ans = v[n - 1] - v[0];
  for (int i = 1; i < n; i++) {
    ans = std::min(ans, k - v[i] + v[i - 1]);
  }
  std::cout << ans;

#ifndef ONLINE_JUDGE
  std::cout << '\\n'
            << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \\n";
#endif
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  int K, N;
  int A[2];
  int d, A_0, d_last;
  int d_max = 0;
  scanf("%d %d", &K, &N);
  scanf("%d", &A[0]);
  A_0 = A[0];
  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &A[1]);
    d = A[1] - A[0];
    if (d > d_max) {
      d_max = d;
    }
    A[0] = A[1];
  }
  d_last = K - A[0] + A_0;
  if (d_last > d_max) {
    d_max = d_last;
  }

  int y = K - d_max;

  printf("%d", y);
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  int K, N;
  int A[100000];
  scanf("%d %d", &K, &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }

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

  int max_distance = distance[0];
  for (int i = 0; i < N; i++) {
    if (distance[i] > max_distance) {
      max_distance = distance[i];
    }
  }

  int y = 0;
  for (int i = 0; i < N; i++) {
    y += distance[i];
  }
  y -= max_distance;

  printf("%d", y);
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int max(int a, int b) // 最大を求める
{
  if (a >= b)
    return a;
  else
    return b;
}

int main(void) {
  int K, N;
  int i;
  int a[200000];
  int dis[200000];
  int large = 0;

  do {
    scanf("%d %d", &K, &N);
  } while ((K < 2 || K > 1000000) || (N < 2 || N > 200000));

  for (i = 0; N > i; i++) {
    if (i == 0) {
      do {
        scanf("%d ", &a[i]);
      } while (a[i] >= K);
    } else {
      do {
        scanf("%d ", &a[i]);
      } while (a[i] >= K || a[i] <= a[i - 1]);
    }
  }
  for (i = 0; N > i; i++) {
    if (N != i + 1) // 最後の引き算で距離がーになるのを防ぐ
      dis[i] = a[i + 1] - a[i];
    else
      dis[i] = (K + a[0]) - a[i];
    large = max(large, dis[i]);
  }

  printf("%d", K - large);
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>

int max(int a, int b) // 最大を求める
{
  if (a >= b)
    return a;
  else
    return b;
}

int main(void) {
  int K, N;
  int i;
  int a[200000];
  int dis[200000];
  int large = 0;

  do {
    scanf("%d %d", &K, &N);
  } while ((K < 2 || K > 1000000) || (N < 2 || N > 200000));

  for (i = 0; N > i; i++) {
    if (i == 0) {
      do {
        scanf("%d ", a[i]);
      } while (a[i] >= K);
    } else {
      do {
        scanf("%d ", a[i]);
      } while (a[i] >= K || a[i] <= a[i - 1]);
    }
  }
  for (i = 0; N > i; i++) {
    if (N != i + 1) // 最後の引き算で距離がーになるのを防ぐ
      dis[i] = a[i + 1] - a[i];
    else
      dis[i] = (K + a[i + 1]) - a[i];
    large = max(large, dis[i]);
  }

  printf("%d", K - large);
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int k, n, a[100005], max, f;
  cin >> k >> n;
  max = 0;
  if (n == 0) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i == 1) {
      f = a[i];
      continue;
    }
    if (a[i] - a[i - 1] > max)
      max = a[i] - a[i - 1];
  }
  if (a[1] + k - a[n] > max)
    max = a[1] + k - a[n];
  cout << k - max;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
int k, n, a[234536];
signed main() {
  cin >> k >> n;
  for1(i, n) cin >> a[i];
  int ans = a[n] - a[1];
  for1(i, n - 1) ans = min(ans, a[i] + k - a[i + 1]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
int k, n, a[123456];
signed main() {
  cin >> k >> n;
  for1(i, n) cin >> a[i];
  int ans = a[n] - a[1];
  for1(i, n - 1) ans = min(ans, a[i] + k - a[i + 1]);
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int K, N;
  cin >> K >> N;
  int house[N + 9], distance[N + 9];
  for (int i = 0; i < N; i++) {
    cin >> house[i];
  }

  for (int i = 0; i < N; i++) {
    distance[i] = house[i + 1] - house[i];
    if (i == N - 1) {
      distance[i] = K - house[i] + house[0];
    }
  }

  int max = distance[0];
  for (int i = 1; i < N; i++) {
    if (distance[i] > max) {
      max = distance[i];
    }
  }

  cout << K - max << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int K, N;
  int house[N + 9], distance[N + 9];
  cin >> K >> N;
  for (int i = 0; i < N; i++) {
    cin >> house[i];
  }

  for (int i = 0; i < N; i++) {
    distance[i] = house[i + 1] - house[i];
    if (i == N - 1) {
      distance[i] = K - house[i];
    }
  }

  int max = distance[0];
  for (int i = 1; i < N; i++) {
    if (distance[i] > max) {
      max = distance[i];
    }
  }

  int sum;
  for (int i = 0; i < N; i++) {
    sum += distance[i];
  }

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

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

int main() {
  long long k, n, m;
  cin >> k >> n;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  m = k - a[n - 1] + a[0];
  for (int i = 0; i < n - 1; i++)
    m = max(m, a[i + 1] - a[i]);
  cout << k - m << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  long long k, n, m;
  long long a[n];
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  m = k - a[n - 1] + a[0];
  for (int i = 0; i < n - 1; i++)
    m = max(m, a[i + 1] - a[i]);
  cout << k - m << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long k, maX, ans, n, i;
long long a[10000000];
int main() {
  cin >> i >> n;
  for (k = 1; k <= n; k++) {
    cin >> a[k];
  }
  sort(a + 1, a + 1 + n);
  for (k = 2; k <= n; k++) {
    maX = max(a[k] - a[k - 1], maX);
  }
  maX = max(maX, a[1] - a[n] + i);
  cout << i - maX;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long k, maX, ans, n, i;
long long a[100000];
int main() {
  cin >> i >> n;
  for (k = 1; k <= n; k++) {
    cin >> a[k];
  }
  sort(a + 1, a + 1 + n);
  for (k = 2; k <= n; k++) {
    maX = max(a[k] - a[k - 1], maX);
  }
  maX = max(maX, a[1] - a[n] + i);
  cout << i - maX;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, n, d, p;
  cin >> p >> n;
  long long int a[n + 1], b;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  b = (p - a[n]) + a[1];
  d = b;
  j = 1;
  for (i = 2; i <= n; i++) {
    if (a[i] - a[i - 1] > b) {
      b = a[i] - a[i - 1];
      j = i;
    }
  }
  for (i = 1; i < n; i++) {
    d += (a[i + 1] - a[i]);
  }
  cout << d - b;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, n, d, p;
  cin >> p >> n;
  long long int a[n + 1], b;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  b = (p - a[n]) + a[1];
  d = b;
  j = 1;
  for (i = 2; i <= n; i++) {
    if (a[i] - a[i - 1] > b) {
      b = a[i + 1] - a[i];
      j = i;
    }
  }
  cout << j << endl;
  b = 0;
  for (i = 2; i <= n; i++) {
    b += (a[j + 1] - a[j]);
    j++;
    if (j == n && i + 1 <= n) {
      b += d;
      j = 1;
      i++;
    }
  }
  cout << b;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <complex>
#include <iostream>
#include <vector>

using namespace std;

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

  vector<int> house(n);
  cin >> house.at(0);

  vector<int> distance(n);

  for (int i = 1; i < n; ++i) {
    cin >> house.at(i);
    distance.at(i) = house.at(i) - house.at(i - 1);
  }

  distance.at(0) = k - house.at(n - 1) + house.at(0);

  int ans = distance.at(0);
  int m = distance.at(0);

  for (int j = 1; j < n; ++j) {
    ans += distance.at(j);
    m = max(m, distance.at(j));
  }

  cout << ans - m;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <complex>
#include <iostream>
#include <vector>

using namespace std;

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

  vector<int> house(n);
  cin >> house.at(0);

  vector<int> distance(n);

  for (int i = 1; i < n; ++i) {
    cin >> house.at(i);
    distance.at(i) = house.at(i) - house.at(i - 1);
  }

  distance.at(0) = k - house.at(n - 1) + house.at(0);

  int ans;

  for (int j = 0; j < n; ++j) {
    int sum = 0;
    for (int i = 0; i < n - 1; ++i) {
      sum += distance.at((j + i) % n);
    }

    if (j == 0)
      ans = sum;
    else
      ans = min(sum, ans);
  }

  cout << ans;
}
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;
int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  a.push_back(a[0] + k);
  int l = 0;
  rep(i, n) { l = max(l, a[i + 1] - a[i]); }
  int ans = k - l;
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int K, N;
  cin >> K >> N;

  for (int i = 0; i < N; i++) {
    int A[i];
    cin >> A[i];

    if (i == 0 || i == N - 1) {
      cout << A[N - 1] - A[0] << endl;
    } else {
      cout << K << 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>;

ll x = 1e9 + 7;

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

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


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

ll x = 1e9 + 7;

int main() {
  int k, n;
  int x = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0)
      continue;
    x = max(x, a[i] - a[i - 1]);
  }
  x = max(x, k - a[n - 1] + a[0]);
  cout << k - x << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  int a, ab, a0, max = 0, N, K, c, cost = 0;
  cin >> K >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    if (i == 0) {
      a0 = a;
      ab = a;
      continue;
    }
    cost += (a - ab);
    if (max < (a - ab))
      max = (a - ab);
    ab = a;
  }
  cost += K - a + a0;
  if (max < (K - a + a0))
    max = (K - a + a0);
  cost -= max;
  cout << cost << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  unsigned int A[2 * 10 ^ 5], max = 0, N, K, C[2 * 10 ^ 5], cost = 0;
  cin >> K >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    if (i == 0)
      continue;
    C[i] = A[i] - A[i - 1];
    if (max < C[i])
      max = C[i];
    cost += C[i];
  }

  C[0] = K - A[N - 1] + A[0];
  cost += C[0];

  if (max < C[0])
    max = C[0];
  cost -= max;
  cout << cost << 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> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec.at(i);
  sort(vec.begin(), vec.end());
  // for(int i=0;i<n;i++)cout<<vec.at(i)<<" ";
  /*
   for(int i=0;i<n;i++){
     for(int j=0;j<i;j++){
       if(vec.at(i)<vec.at(j))swap(vec.at(i), vec.at(j));
     }
   }
   */
  int longer = max(vec.at(1) - vec.at(0), k + vec.at(0) - vec.at(n - 1));
  for (int i = 1; i < n - 1; i++)
    longer = max(longer, vec.at(i + 1) - vec.at(i));
  cout << k - longer;
}


Error: TLE

Fixed submission:

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

int main() {
  int k, n;
  cin >> k >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec.at(i);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (vec.at(i) < vec.at(j))
        swap(vec.at(i), vec.at(j));
    }
  }
  int longer = max(vec.at(1) - vec.at(0), k + vec.at(0) - vec.at(n - 1));
  for (int i = 1; i < n - 1; i++)
    longer = max(longer, vec.at(i + 1) - vec.at(i));
  cout << k - longer;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int maxN = 2e5 + 10;
int a[maxN] = {0};
int main() {
  int k, n, max = -1;
  cin >> k >> 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 (a[0] + k - a[n - 1] > max) {
    max = a[0] + k - a[n - 1];
  }
  cout << k - max << endl;
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;
  vector<int> V(A + B + C);
  for (auto &x : V)
    cin >> x;
  sort(V.begin(), V.begin() + A);
  sort(V.begin() + A, V.end() - C);
  sort(V.end() - C, V.end());
  for (int i = 0; i < A - X; i++)
    V[i] = 0;
  for (int j = A; j < A + B - Y; j++)
    V[j] = 0;
  sort(V.rbegin(), V.rend());
  cout << accumulate(V.begin(), V.begin() + X + Y, 0LL) << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
#define int long long // at least int64 > 9*10^18
#define ENDL '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\\n'
#define all(v) (v).begin(), (v).end()
using namespace std;
signed main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) { cin >> a[i]; }
  a.push_back(k + a[0]);

  rep(i, n) { b[i] = a[i + 1] - a[i]; }
  sort(all(b));
  reverse(all(b));
  int d = k - b[0];
  cout << d << endl;
}

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


Fixed submission:


#include <bits/stdc++.h>
#define int long long // at least int64 > 9*10^18
#define ENDL '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\\n'
#define all(v) (v).begin(), (v).end()
using namespace std;
signed main() {
  int k, n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) { cin >> a[i]; }
  a.push_back(k + a[0]);
  rep(i, n) { b[i] = a[i + 1] - a[i]; }
  sort(all(b));
  cout << k - b[n] << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int k, n, i, saidai = 0;
  cin >> k >> n;
  int a[n];
  cin >> a[0];
  for (i = 1; i < n; i++) {
    cin >> a[i];
    saidai = max(saidai, a[i] - a[i - 1]);
  }
  saidai = max(saidai, a[0] + k - a[n - 1]);
  k -= saidai;
  cout << k;
}

Error: WA

Fixed submission:

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

int main() {
  int k, n, i, saidai = 0;
  cin >> k >> n;
  int a[i];
  cin >> a[0];
  for (i = 1; i < n; i++) {
    cin >> a[i];
    saidai = max(saidai, a[i] - a[i - 1]);
  }
  saidai = max(saidai, a[0] + k - a[n - 1]);
  k -= saidai;
  cout << k;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long long int K, N, max, ans;
  max = 0;
  ans = 0;
  cin >> K >> N;
  long long int A[200000], B[200001];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  A[N] = A[0] + K;
  for (int j = 0; j < N + 1; j++) {
    B[j] = A[j + 1] - A[j];
  }
  for (int k = 0; k < N; k++) {
    if (B[k] > max) {
      max = B[k];
    }
  }
  for (int l = 0; l < N; l++) {
    ans = ans + B[l];
  }
  ans = ans - max;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long int K, N, max, ans;
  max = 0;
  ans = 0;
  cin >> K >> N;
  long long int A[20000], B[20001];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  A[N] = A[0] + K;
  for (int j = 0; j < N + 1; j++) {
    B[j] = A[j + 1] - A[j];
  }
  for (int k = 0; k < N; k++) {
    if (B[k] > max) {
      max = B[k];
    }
  }
  for (int l = 0; l < N; l++) {
    ans = ans + B[l];
  }
  ans = ans - max;
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep_from_1(i, n) for (int i = 1; i < n; i++)
using namespace std;

int main() {
  // ---------------------------------------------------
  int K, N;
  cin >> K >> N; // K=20, N=3
  vector<int> A(N);
  rep(i, N) cin >> A.at(i); // A.at(0)=5, A.at(1)=10, A.at(2)=15
  // cout << "End of initialization" << endl;
  // ---------------------------------------------------
  vector<int> dis(N);
  dis.push_back(K + A.at(0) - A.at(N - 1));
  rep_from_1(i, N) dis.push_back(A.at(i) - A.at(i - 1));
  // ---------------------------------------------------
  sort(dis.begin(), dis.end());
  reverse(dis.begin(), dis.end());
  cout << K - dis.at(0) << endl;
  // ---------------------------------------------------
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);

  vector<int> dis(N);
  dis.push_back(K + A.at(0) - A.at(N - 1));
  rep(i, N) dis.push_back(A.at(i + 1) - A.at(i));

  sort(dis.begin(), dis.end());
  reverse(dis.begin(), dis.end());
  cout << K - dis.at(0) << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 0x3f3f3f3f;
const int N = 200000 + 5;
int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int k, n;
  cin >> k >> n;
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (maxn < a[i] - a[i - 1])
      maxn = a[i] - a[i - 1];
  }
  int z = k + a[1] - a[n];
  if (z > maxn)
    maxn = z;
  cout << k - maxn << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 0x3f3f3f3f;
const int N = 100000 + 5;
int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int k, n;
  cin >> k >> n;
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (maxn < a[i] - a[i - 1])
      maxn = a[i] - a[i - 1];
  }
  int z = k + a[1] - a[n];
  if (z > maxn)
    maxn = z;
  cout << k - maxn << endl;
  return 0;
}