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

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
#define PI 3.14159265358979323846264338327950 L
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
#endif
  int k, n;
  cin >> k >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int maxi = -1;
  for (int i = 0; i < n - 1; i++)
    maxi = max(maxi, a[i + 1] - a[i]);
  maxi = max(maxi, k - a[n - 1] + a[0]);
  cout << k - maxi << endl;

  return 0;
}

Error: WA

Fixed submission:

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
#define PI 3.14159265358979323846264338327950 L
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int k, n;
  cin >> k >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int maxi = -1;
  for (int i = 0; i < n - 1; i++)
    maxi = max(maxi, a[i + 1] - a[i]);
  maxi = max(maxi, k - a[n - 1] + a[0]);
  cout << k - maxi << endl;

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

#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define endl '\\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START                                                             \\
  ios_base::sync_with_stdio();                                                 \\
  cin.tie();                                                                   \\
  cout.tie();
using namespace std;
ll k, n, a[200005];
int32_t main() {
  CODE_START;
  cin >> k >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    a[i]++;
  }
  ll ans = 1e18;
  for (ll i = 0; i < n; i++) {
    ans = min(ans, a[i] + k - a[i + 1]);
  }
  ll s1 = 0;
  for (ll i = 0; i < n; i++) {
    a[i]--;
  }
  for (ll i = 1; i < n; i++)
    s1 = s1 + a[i] - a[i - 1];
  ans = min(ans, s1);
  if (a[0] == 0) {
    s1 = 0;
    for (ll i = 2; i < n; i++)
      s1 = s1 + a[i] - a[i - 1];
    s1 = s1 + k - a[n - 1];
    ans = min(ans, s1);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define endl '\\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START                                                             \\
  ios_base::sync_with_stdio();                                                 \\
  cin.tie();                                                                   \\
  cout.tie();
using namespace std;
ll k, n, a[200005], dp[3][200005];
int32_t main() {
  CODE_START;
  cin >> k >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i < n; i++) {
    dp[0][i] = dp[0][i - 1] + a[i] - a[i - 1];
  }
  a[n] = k;
  for (ll i = n - 1; i >= 1; i--) {
    dp[1][i] = dp[i][i + 1] + a[i + 1] - a[i];
  }
  ll ans = 1e18;
  for (ll i = 0; i < n; i++) {
    ans = min(ans, dp[0][i] + dp[1][i + 1]);
  }
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> ai(n);
  vector<ll> di(n);
  for (int i = 0; i < n; i++) {
    cin >> ai.at(i);
  }
  for (int i = 0; i < n - 1; i++) {
    di.at(i) = ai.at(i + 1) - ai.at(i);
  }
  di.at(n - 1) = k - ai.at(n - 1) + ai.at(0);
  ll dmax = 0;

  for (ll i : di)
    dmax = max(i, dmax);
  cout << k - dmax << endl;

  return 0;
}

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


Fixed submission:

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

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

  vector<int> ai(n);
  vector<int> di(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> ai.at(i);
  }
  for (int i = 0; i < n - 2; i++) {
    di.at(i) = ai.at(i + 1) - ai.at(i);
  }
  di.at(n - 1) = k - ai.at(n - 1) + ai.at(0);
  int dmax = 0;

  for (int i : di)
    dmax = max(i, dmax);
  cout << k - dmax << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define viit(a) vector<int>::iterator a
#define IT iterater
#define REP(i, a, n) for (ll i = a; i < (n); i++)
#define FOR(i, a, n) for (ll i = a; i <= (n); i++)
#define V vector
#define RE return
#define B(s) s.back()
#define all(a) a.begin(), a.end()
#define ER1(a) a.erase(a.begin())
#define ER0(a) a.erase(a.end())
#define Fill(a, b) memset(a, b, sizeof(a))
#define SO(a) sort(all(a))
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  long long n, k, a[1000001];
  cin >> k >> n;
  REP(i, 0, n)
  cin >> a[i];
  long long minimum = 1000050;
  REP(i, 0, n - 1) {
    int s = abs(a[i] - a[(i + 1) % n]);
    minimum = min(k - s, minimum);
  }
  minimum = min(a[n - 1] - a[0], minimum);
  cout << minimum;
  RE 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define viit(a) vector<int>::iterator a
#define IT iterater
#define REP(i, a, n) for (ll i = a; i < (n); i++)
#define FOR(i, a, n) for (ll i = a; i <= (n); i++)
#define V vector
#define RE return
#define B(s) s.back()
#define all(a) a.begin(), a.end()
#define ER1(a) a.erase(a.begin())
#define ER0(a) a.erase(a.end())
#define Fill(a, b) memset(a, b, sizeof(a))
#define SO(a) sort(all(a))
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int n, k, a[100001];
  cin >> k >> n;
  REP(i, 0, n)
  cin >> a[i];
  int minimum = 1000050;
  REP(i, 0, n - 1) {
    int s = abs(a[i] - a[(i + 1) % n]);
    minimum = min(k - s, minimum);
  }
  minimum = min(a[n - 1] - a[0], minimum);
  cout << minimum;
  RE 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2 * 100 * 1000 + 20;
int n, k, a[N], ans;

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


Error: 0

Fixed submission:

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

const int N = 2 * 100 * 1000 + 20;
int n, k, a[N], ans;

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


#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdint.h>
#include <string>
#include <vector>

#include <cstdio>
#include <queue>

#include <list>

#define PI (3.14159265358979323846)
#define INF (1e+9)
#define DIV_NUM (1000000007)

using namespace std;

bool seen[501][501];

int N, K;
char field[501][501];
int dist[501][501];

typedef pair<int, int> P;
queue<P> que;

// typedef vector<vector<int>> v;
vector<double> A;
vector<double> A2;

int main() {

  // input data

  cin >> K >> N;

  int ai = 0;
  int tmp = 1;
  int c01 = 0;
  int c02 = 0;
  int c03 = 0;

  for (int i = 0; i < N; i++) {
    cin >> ai;
    A.push_back(ai);

    //        std::cout << A.front() << "  " << A.back() << "  " << ai[i] <<
    //        "\\n";
  }

  std::sort(A.begin(), A.end()); // 昇順ソート

  for (int i = 0; i < (N - 1); i++) {
    c01 = A[i + 1] - A[i];
    A2.push_back(c01);
  }
  c01 = K - A.back();
  c02 = A.front() - 0;
  c03 = c01 + c02;

  A2.push_back(c03);

  std::sort(A2.begin(), A2.end(), std::greater<int>()); // 降順ソート

  int G = 0;

  for (int i = 0; i < (A2.size() - 1); i++) {
    G = G + A2[i + 1];
  }

  std::cout << G;

  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdint.h>
#include <string>
#include <vector>

#include <cstdio>
#include <queue>

#include <list>

#define PI (3.14159265358979323846)
#define INF (1e+9)
#define DIV_NUM (1000000007)

using namespace std;

bool seen[501][501];

int N, K;
char field[501][501];
int dist[501][501];

typedef pair<int, int> P;
queue<P> que;

// typedef vector<vector<int>> v;
vector<double> A;
vector<double> A2;

int main() {

  // input data

  cin >> K >> N;

  int ai[100] = {100000000};
  int tmp = 1;
  int a0 = 0;
  int c01 = 0;

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

    //        std::cout << A.front() << "  " << A.back() << "  " << ai[i] <<
    //        "\\n";
  }

  std::sort(A.begin(), A.end()); // 昇順ソート

  for (int i = 0; i < (N - 1); i++) {
    c01 = A[i + 1] - A[i];
    A2.push_back(c01);
  }

  std::sort(A2.begin(), A2.end(), std::greater<int>()); // 降順ソート

  int G = 0;

  for (int i = 0; i < A2.size(); i++) {
    G = G + A2[i];
  }

  if ((K - G) < (G)) {
    G = K - G;
  }

  std::cout << G << "\\n";

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <istream>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  long long K, N;

  cin >> K >> N;

  vector<long long> A(N);
  vector<long long> subA(N - 1);

  long long maxd = 0;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    subA[i] = A[i + 1] - A[i];
    maxd = max(maxd, subA[i]);
  }

  long long ans = accumulate(subA.begin(), subA.end(), 0);

  if (maxd > (K - A[N - 1] + A[0])) {
    cout << ans - maxd + (K - A[N - 1] + A[0]) << endl;
  } else {
    cout << ans << endl;
  }

  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 <iomanip>
#include <iostream>
#include <istream>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int K, N;

  vector<int> A(N);
  vector<int> subA(N);
  subA[N - 1] = 0;

  cin >> K >> N;

  int maxd = 0;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    subA[i] = A[i + 1] - A[i];
    maxd = max(maxd, subA[i]);
  }

  int ans = accumulate(subA.begin(), subA.end(), 0);
  if (maxd > (K - A[N - 1])) {
    ans = ans - maxd + (K - A[N - 1]);
  }

  cout << ans << endl;

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

#include <cstdio>
#include <iostream>
using namespace std;
#define N 200010
int k;
int n;
int a[N];
int fun(int k, int x) { return x > k - x ? k - x : x; }
int main() {
  while (scanf("%d %d", &k, &n) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);
    int ans = 0, dis = 0;
    for (int i = 0; i < n; i++) {
      if (i == 0)
        dis = k - a[n - 1] + a[0];
      else
        dis = a[i] - a[i - 1];
      if (ans < dis)
        ans = dis;
    }
    printf("%d\\n", k - ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define N 100010
int k;
int n;
int a[N];
int main() {
  while (scanf("%d %d", &k, &n) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);
    int ans = N, dis;
    for (int i = 0; i < n; i++) {
      if (i == 0)
        dis = a[n - 1] - a[i];
      else
        dis = k - (a[i] - a[i - 1]);
      if (ans > dis)
        ans = dis;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int k, n, a[200010];
int ma;

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


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int k, n, a[100010];
int ma;

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll K, N, A[int(2e5 + 5)];

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

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll K, N, A[int(1e5 + 5)];

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

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

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

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

int K;
int N;

int main() {
  cin >> K >> N;

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

  int tmp = 0;
  int diff = 0;
  for (int i = 0; i < N; i++) {
    if (i == N - 1) {
      tmp = A[0] - A[N - 1] + K;
      diff = max(diff, tmp);
    } else {
      tmp = A[i + 1] - A[i];
      diff = max(diff, tmp);
    }
  }

  cout << K - diff << endl;
}

Error: TLE

Fixed submission:

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

int K;
int N;

int main() {
  cin >> K >> N;

  deque<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int res = 10000000;
  for (int i = 0; i < N; i++) {
    if (i > 0) {
      int tmp;
      tmp = A.front();
      A.pop_front();
      A.push_back(tmp);
    }

    int sum = 0;
    for (int j = 0; j < N - 1; j++) {
      if (A[j + 1] - A[j] < 0)
        sum += A[j + 1] - A[j] + K;
      else
        sum += A[j + 1] - A[j];
    }
    res = min(res, sum);
  }

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

#include <bits/stdc++.h>
using namespace std;
int arr[200005];

int main() {
  int n, k;
  scanf("%d", &k);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  int x = arr[n - 1] - arr[0];
  int y = arr[0] + (k - arr[1]);
  x = min(x, y);
  int d = (k - arr[n - 1]) + arr[n - 2];
  int mini = min(x, d);
  for (int i = 1; i < n - 1; i++) {
    int p = (k - arr[i]) + arr[i - 1];
    int q = arr[i] + (k - arr[i + 1]);
    int mini2 = min(p, q);
    mini = min(mini, mini2);
  }
  cout << mini;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int arr[100005];

int main() {
  int n, k;
  scanf("%d", &k);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  int x = arr[n - 1] - arr[0];
  int y = arr[0] + (k - arr[1]);
  x = min(x, y);
  int d = (k - arr[n - 1]) + arr[n - 2];
  int mini = min(x, d);
  for (int i = 1; i < n - 1; i++) {
    int p = (k - arr[i]) + arr[i - 1];
    int q = arr[i] + (k - arr[i + 1]);
    int mini2 = min(p, q);
    mini = min(mini, mini2);
  }
  cout << mini;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

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

  int n, k;
  cin >> k >> n;

  vector<int> a(n);

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

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

  int ans = a[0] + k - a[n - 1];

  for (int i = 1; i < n; i++)
    ans = max(ans, a[i] - a[i - 1]);

  ans = k - ans;

  cout << ans << "\\n";

  return 0;
}


Error: WA

Fixed submission:

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

int a[200010];

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

  int n, k;
  cin >> n >> k;

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

  int ans = a[0] + k - a[n - 1];

  for (int i = 1; i < n; i++)
    ans = max(ans, a[i] - a[i - 1]);

  ans = k - ans;

  cout << ans << "\\n";

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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);

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

  int maxDistance = 0;
  for (int i = 0; i < N; i++) {
    if (i == N - 1) {
      int distance = A[N - 1] - A[0];
      if (distance > K / 2)
        distance = K - distance;
      if (maxDistance < distance)
        maxDistance = distance;
    } else {
      int distance = A[i + 1] - A[i];
      if (distance > K / 2)
        distance = K - distance;
      if (maxDistance < distance)
        maxDistance = distance;
    }
  }
  std::cout << K - maxDistance << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int K, N;
  cin >> K, N;
  vector<int> A(N);

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

  int preNum = 0;
  for (int i = 0; i < N; i++) {
    if ((K / 2) > (A[i] - preNum)) {
      return (N - preNum + A[i]);
    }
  }
  return (A[N - 1] - A[0]);

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, n, a, m, k, l, sum1 = 0, sum = 0, flag, cnt = 0, maxn,
                                     ans1, ans2, p = 0, q = 0, rans = INT_MAX,
                                     minn = INT_MAX, ans;
  string s;
  char ch;
  vector<long long int> ar;
  cin >> k >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    ar.push_back(a);
  }
  ans = ar[n - 1] - ar[0];
  for (i = 1; i < n; i++) {
    ans = min(ans, k - abs(ar[i] - ar[i - 1]));
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, n, m, k, l, sum1 = 0, sum = 0, flag, cnt = 0, maxn, ans1,
                                  ans2, p = 0, q = 0, r, ar[100000],
                                  ans = INT_MAX, minn = INT_MAX;
  string s;
  char ch;
  cin >> k >> n;
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }
  ar[-1] = 0;
  ar[n] = k;
  ans = ar[n - 1] - ar[0];
  for (i = 1; i < n; i++) {
    ans = min(ans, k - abs(ar[i] - ar[i - 1]));
  }
  cout << ans << endl;
  return 0;
}