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;
  int house[200010];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> house[i];
  }
  for (int i = 0; i < N - 1; i++) {
    ans = max(house[i + 1] - house[i], ans);
  }
  ans = max(K - house[N - 1] + house[0], ans);

  cout << K - ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int K, N;
  cin >> K >> N;
  int house[20010];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> house[i];
  }
  for (int i = 0; i < N - 1; i++) {
    ans = max(house[i + 1] - house[i], ans);
  }
  ans = max(K - house[N - 1] + house[0], ans);

  cout << K - ans << endl;
  return 0;
}
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];
  }
  a.push_back(a[0] + k);
  int max = 0, res = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    temp = a[i + 1] - a[i];
    res += temp;
    if (temp > max)
      max = temp;
  }
  cout << res - max << endl;
}


Error: WA

Fixed submission:

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

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

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

int k, n, res;

int main() {

  cin >> k >> n;
  int a[n], a2[n], r[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a2[i] = min(a[i], k - a[i]);
  }

  sort(a, a + n);

  for (int i = 0; i < n - 1; i++) {
    r[i] = min((a2[i] + a2[i + 1]), (a[i + 1] - a[i]));
  }

  r[n - 1] = min((a2[0] + a2[n - 1]), (a[n - 1] - a[0]));

  sort(r, r + n);

  for (int i = 0; i < n - 1; i++) {
    res += r[i];
  }

  cout << res;

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>

int max(std::vector<int> &a) {
  int m = a[0];
  for (int i = 1; i < a.size(); i++)
    if (a[i] > m)
      m = a[i];
  return m;
}

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

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

int main() {
  int64_t K, N;
  cin >> K >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int MAX = 0;
  int mi;
  for (int i = 0; i < N; i++) {
    int i2 = i + 1;
    int d;
    if (i2 >= N) {
      i2 = 0;
      d = K - A.at(N - 1) + A.at(0);
    } else {
      d = A.at(i2) - A.at(i);
    }
    if (MAX < d) {
      MAX = d;
      mi = i2;
    }
  }

  int64_t res = 0;
  for (int i = mi; i < mi + N - 1; i++) {
    int i1 = (i >= N) ? i - N : i;
    int i2 = (i1 + 1 >= N) ? i1 + 1 - N : i1 + 1;
    if (i1 < i2) {
      res += A.at(i2) - A.at(i1);
    } else {
      res += A.at(i2) + (K - A.at(i1));
    }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int64_t K, N;
  cin >> K >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  int64_t res = 0;
  int64_t MAX = (int64_t)1 << 60;
  for (int i = 0; i < N; i++) {
    vector<int> AV(N);
    for (int j = 0; j < N; j++) {
      int k = i + j;
      k = k < N ? k : k - N;
      AV.at(j) = k;
    }

    int c = 0;
    while (!AV.empty()) {
      int d1 = c - 1;
      if (d1 < 0)
        d1 = AV.size() - 1;
      int d2 = c + 1;
      if (d2 >= N)
        d2 = 0;
      int d;
      int r1, r2;
      if (d1 > c) {
        r1 = K - A.at(d1) + A.at(c);
      } else {
        r1 = A.at(c) - A.at(d1);
      }
      if (d2 < c) {
        r2 = A.at(d2) + K - A.at(c);
      } else {
        r2 = A.at(d2) - A.at(c);
      }
      AV.erase(AV.begin() + c);
      if (r1 < r2) {
        res += r1;
        if (c < d1)
          d1--;
        c = d1;
      } else {
        res += r2;
        if (c < d2)
          d2--;
        c = d2;
      }
    }
    if (MAX > res)
      MAX = res;
  }
  cout << MAX << 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> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int sum = 0;
  for (int i = 1; i < N; i++) {
    int sumsum = 0;
    sumsum = vec.at(i) - vec.at(i - 1);
    sum = max(sum, sumsum);
  }
  int MINMIN = 0;
  MINMIN = max(vec.at(0) + K - vec.at(N - 1), sum);
  cout << K - MINMIN << 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 <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);
  }
  int sum = 0;
  for (int i = 1; i <= N; ++i) {
    sum += vec.at(i) - vec.at(i - 1);
  }
  cout << sum << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= (ll)b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 3e5 + 100;

ll a[MAX];
int main() {
  fastio;
  ll n, k, ans = 0;
  cin >> k >> n;
  f(i, 0, n) cin >> a[i];
  sort(a, a + n);
  ll x = a[0];
  f(i, 0, n - 1) a[i] = a[i + 1] - a[i];

  a[n - 1] = k - a[n - 1] + x;
  sort(a, a + n);
  ll sum = 0;
  f(i, 0, n - 1) sum += a[i];
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= (ll)b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 3e3 + 100;

ll a[MAX];
int main() {
  fastio;
  ll n, k, ans = 0;
  cin >> k >> n;
  f(i, 0, n) cin >> a[i];
  sort(a, a + n);
  ll x = a[0];
  f(i, 0, n - 1) a[i] = a[i + 1] - a[i];

  a[n - 1] = k - a[n - 1] + x;
  sort(a, a + n);
  ll sum = 0;
  f(i, 0, n - 1) sum += a[i];
  cout << sum << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)

int main(void) {
  ll K, N;
  ll dist = 0;
  ll tmp_kankaku = 0;
  ll max_kankaku = 0;
  ll skip_posi = 0;

  // 標準入力
  cin >> K >> N;

  vector<int> house_posi(N);
  int house_kankaku[N];

  REP(i, N) { cin >> house_posi.at(i); }

  REP(i, N) {
    if (i == 0) {
      // 最初
      tmp_kankaku = house_posi.at(i) + (K - house_posi.at(N - 1));
    } else {
      tmp_kankaku = house_posi.at(i) - house_posi.at(i - 1);
    }

    if (tmp_kankaku > max_kankaku) {
      skip_posi = i;
      max_kankaku = tmp_kankaku;
    }
  }

  // cout << "skip_posi:" << skip_posi << endl;

  // 家の位置は昇順ソート済
  // end-startを計算
  // ll temp = *(house_posi.end()-1) - *house_posi.begin();
  // cout << temp << endl;

  // end-startがK/2以下なら、最小経路になる
  //  if( temp <= (K/2) )
  //  {
  //      cout << temp << endl;
  //      return 0;
  //  }

  // MAX-MINがK/2を超える場合、家の間隔が最も遠いところを通過しないように動く
  // 最も遠いところskip_posi-1～skip_posiの間

  if (skip_posi == 0) {
    // 最初と最後が一番長い場合
    // 最初の家～最後の家のルート
    dist = (*(house_posi.end() - 1) - *(house_posi.begin()));
  } else {
    // そのほかのルート
    // スキップ前までは通常経路
    dist += house_posi.at(skip_posi - 1);
    // スキップ後の部分
    dist += (K - house_posi.at(skip_posi));
  }

  cout << dist << endl;
  return 0;
}

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


Fixed submission:

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

typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)

int main(void) {
  ll K, N;
  ll dist = 0;
  ll tmp_kankaku = 0;
  ll max_kankaku = 0;
  ll skip_posi = 0;

  // 標準入力
  cin >> K >> N;

  vector<int> house_posi(N);
  int house_kankaku[N];

  REP(i, N) {
    cin >> house_posi.at(i);
    if (i == 0)
      tmp_kankaku = house_posi[i];
    if (i > 0)
      tmp_kankaku = house_posi[i] - house_posi[i - 1];
    if (tmp_kankaku > max_kankaku) {
      skip_posi = i;
      max_kankaku = tmp_kankaku;
    }
  }

  // 家の位置は昇順ソート済
  // end-startを計算
  ll temp = *(house_posi.end() - 1) - *house_posi.begin();
  // cout << temp << endl;

  // end-startがK/2以下なら、最小経路になる
  //  if( temp <= (K/2) )
  //  {
  //      cout << temp << endl;
  //      return 0;
  //  }

  // MAX-MINがK/2を超える場合、家の間隔が最も遠いところを通過しないように動く
  // 最も遠いところskip_posi-1～skip_posiの間

  // スキップ前までは通常経路
  dist += house_posi.at(skip_posi - 1);
  // スキップ後の部分
  dist += (K - house_posi.at(skip_posi));

  cout << dist << endl;
  return 0;
}
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), B(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N - 1; i++) {
    B.at(i) = A.at(i + 1) - A.at(i);
  }
  B.at(N - 1) = A.at(0) + K - A.at(N - 1);

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

  int max = 0;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += B.at(i);
    if (max < B.at(i))
      max = B.at(i);
  }
  cout << sum - max << endl;
}


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


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.at(i);

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

  int min = INT_MAX;

  for (int j = 0; j <= 10; j++) {
    int dis = 0;
    for (int i = 0; i < N - 1; i++) {
      int a = j % N;
      int b = a - 1;
      if (a == 0) {
        b = N - 1;
        dis += abs(A.at(a) + K - A.at(b));
      }
      dis += abs(A.at(a) - A.at(b));
    }
    if (min > dis)
      min = dis;
  }
  for (int j = N - 10; j <= N; j++) {
    int dis = 0;
    for (int i = 0; i < N - 1; i++) {
      int a = j % N;
      int b = a - 1;
      if (a == 0) {
        b = N - 1;
        dis += abs(A.at(a) + K - A.at(b));
      }
      dis += abs(A.at(a) - A.at(b));
    }
    if (min > dis)
      min = dis;
  }

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

#include <iostream>
#include <vector>

using namespace std;
void print(vector<int> V) {
  for (int i = 0; i < V.size(); i++) {
    cout << V[i] << " ";
  }
  cout << endl;
}

vector<int> V;

int main() {
  int K, N;
  int q;
  cin >> K >> N;
  for (int i = 0; i < 2; i++) {
    cin >> q;
    V.push_back(q);
  }
  int length = V[1] - V[0];
  int base_total = K - length;
  int total;
  if (N >= 2) {
    for (int i = 2; i <= N; i++) {
      if (i <= N - 1) {
        cin >> q;
        V.push_back(q);
        total = K - (V[2] - V[1]);
        V.erase(V.begin() + 1);
      } else if (i == N) {
        total = V[1] - V[0];
      }
      if (base_total > total) {
        base_total = total;
      }
    }
  }
  cout << base_total << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long n, k;
  long A[100000];
  cin >> n >> k;
  for (long i = 0; i < k; i++) {
    cin >> A[i];
  }
  long base_total = 1000001;
  for (long i = 0; i < k; i++) {
    long length = A[i + 1] - A[i];
    long total = n - length;
    if (i == k - 1) {
      length = A[0] + n - A[i];
      total = n - length;
    }
    if (base_total > total) {
      base_total = total;
    }
  }
  cout << base_total << 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> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }

  int max = 0;
  for (int i = 1; i <= n - 1; i++) {
    if ((p.at(i) - p.at(i - 1)) >= max) {
      max = (p.at(i) - p.at(i - 1));
    }
  }
  if (k - p.at(n - 1) + p.at(0) >= max) {
    max = k - p.at(n - 1) + p.at(0);
  }

  cout << k - max << 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 <bits/stdc++.h>
using namespace std;

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

  int min = k;
  for (int i = 1; i <= n - 1; i++) {
    if ((p.at(i) - p.at(i - 1)) <= min) {
      min = (p.at(i) - p.at(i - 1));
    }
  }
  if (k - p.at(n - 1) + p.at(0) <= min) {
    min = k - p.at(n - 1) + p.at(0);
  }

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

#define __LOCAL_DEBUG__
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL_DEBUG__
#define _debug(x)                                                              \\
  { cerr << "\\033[94m" << __func__ << " " << #x << ": " << (x) << "\\n\\033[0m"; }
#else
#define _debug(x)                                                              \\
  { ((void)0); }
#endif
#define rep(i, n) for (int i = (0); i < (n); i++)
#define repp(i, a, b) for (int i = (a); i < (b); i++)
#define CLR(x, v) memset(x, v, sizeof(x))
#define all(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
using ll = signed long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 700;

const int N = 200005;

// #define __LOCAL_DEBUG__
int n, k;
int a[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d%d", &n, &k);
  repp(i, 1, k + 1) scanf("%d", a + i);
  a[0] = a[k];
  int ma = 0;
  repp(i, 1, k + 1) amax(ma, (a[i] - a[i - 1] + n) % n);
  printf("%d\\n", n - ma);

  return 0;
}


Error: 0

Fixed submission:

#define __LOCAL_DEBUG__
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL_DEBUG__
#define _debug(x)                                                              \\
  { cerr << "\\033[94m" << __func__ << " " << #x << ": " << (x) << "\\n\\033[0m"; }
#else
#define _debug(x)                                                              \\
  { ((void)0); }
#endif
#define rep(i, n) for (int i = (0); i < (n); i++)
#define repp(i, a, b) for (int i = (a); i < (b); i++)
#define CLR(x, v) memset(x, v, sizeof(x))
#define all(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
using ll = signed long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 700;

const int N = 100005;

// #define __LOCAL_DEBUG__
int n, k;
int a[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d%d", &n, &k);
  repp(i, 1, k + 1) scanf("%d", a + i);
  a[0] = a[k];
  int ma = 0;
  repp(i, 1, k + 1) amax(ma, (a[i] - a[i - 1] + n) % n);
  printf("%d\\n", n - ma);

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int distance = K - A.at(0) + A.at(N - 1);
  for (int i = 0; i < N - 1; i++) {
    if (distance < A.at(i) - A.at(i + 1)) {
      distance = A.at(i) - A.at(i + 1);
    }
  }
  int ans = K - distance;

  printf("%d", ans);
}

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 <bits/stdc++.h>
using namespace std;
int main(void) {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int distance = K - A.at(0) + A.at(N - 1);
  for (int i = 0; i < N; i++) {
    if (distance < A.at(i) - A.at(i + 1)) {
      distance = A.at(i) - A.at(i + 1);
    }
  }
  int ans = K - distance;

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define VI vector<int>
#define MOD 1000000007

typedef long long int ll;

const ll INF = (ll)1e19;
// const int INF=(1<<30);

int main() {
  int k, n;

  cin >> k >> n;
  VI a(n);

  REP(i, n) cin >> a[i];

  int d = -1;

  REP(i, n - 1) { d = max(d, a[i + 1] - a[i]); }
  d = max(d, a[0] + (k - a[n - 1]));

  cout << k - d << endl;
}


Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define VI vector<int>
#define MOD 1000000007

typedef long long int ll;

const ll INF = (ll)1e19;
// const int INF=(1<<30);

int main() {
  int k, n;
  VI a(3);

  cin >> k >> n;
  REP(i, n) cin >> a[i];

  int d = -1;

  REP(i, n - 1) { d = max(d, a[i + 1] - a[i]); }
  d = max(d, a[0] + (k - a[n - 1]));

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

#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> il read(T &x) {
  int f = 1;
  char k = getchar();
  x = 0;
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = x * 10 + k - '0';
  x *= f;
}
template <class T> il _print(T x) {
  if (x >= 10)
    _print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> il print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  _print(x);
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int k, int mod) {
  int res = 1, bas = x;
  while (k) {
    if (k & 1)
      res = 1ll * res * bas % mod;
    bas = 1ll * bas * bas % mod, k >>= 1;
  }
  return res;
}
const int N = 2e5 + 5;
int n, k, val[N], ans = INF;
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(k), read(n);
  for (ri i = 1; i <= n; ++i)
    read(val[i]);
  sort(val + 1, val + 1 + n);
  for (ri i = 1; i < n; ++i)
    ans = min(ans, k - val[i + 1] + val[i]);
  ans = min(ans, k - (val[1] + k - val[n]));
  print(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
template <typename T> inline T read() {
  register T sum = 0;
  register char cc = getchar();
  int sym = 1;
  while (cc != '-' && (cc > '9' || cc < '0'))
    cc = getchar();
  if (cc == '-')
    sym = -1, cc = getchar();
  sum = sum * 10 + cc - '0';
  cc = getchar();
  while (cc >= '0' && cc <= '9')
    sum = sum * 10 + cc - '0', cc = getchar();
  return sym * sum;
}
template <typename T> inline T read(T &a) {
  a = read<T>();
  return a;
}
template <typename T, typename... Others> inline void read(T &a, Others &...b) {
  a = read(a);
  read(b...);
}
int n, m, a[maxn];
long long s, ans = 0x7fffffff;
int main() {
  read(m, n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    a[n + i] = a[i] + m;
  for (int i = 2; i <= n; i++)
    s += a[i] - a[i - 1];
  for (int i = 1; i <= n; i++) {
    ans = min(ans, s);
    s -= a[i + 1] - a[i];
    s += a[n + i] - a[n + i - 1];
  }
  printf("%lld\\n", ans);
  return 0;
}