prithivMLmods/Coder-Stat huggingface dataset:
Problem p02724 (C++)

Buggy submission (status: Runtime Error):
/**
 * ATCODER 160 B
 * Kartikeya (doomsday861)
 **/
#include <bits/stdc++.h>
#include <time.h>
#define ll long long
#define testcase                                                               \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define timeb auto start = high_resolution_clock::now();
#define timee                                                                  \
  auto stop = high_resolution_clock::now();                                    \
  auto duration = duration_cast<seconds>(stop - start);                        \
  cout << "Time taken by function: " << duration.count() << "seconds" << endl;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("output.ans", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  using namespace std::chrono;
  // timeb
  ll x;
  cin >> x;
  ll h = 0;
  ll n = x / 500;
  h += n * 1000;
  x %= 500;
  // cout<<x<<endl;
  while (x > 5) {
    ll d = x / 5;
    h += d * 5;
    x %= 5;
  }
  cout << h << endl;
  // timee
  return 0;
}

Fixed submission:
/**
 * ATCODER 160 B
 * Kartikeya (doomsday861)
 **/
#include <bits/stdc++.h>
#include <time.h>
#define ll long long
#define testcase                                                               \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define timeb auto start = high_resolution_clock::now();
#define timee                                                                  \
  auto stop = high_resolution_clock::now();                                    \
  auto duration = duration_cast<seconds>(stop - start);                        \
  cout << "Time taken by function: " << duration.count() << "seconds" << endl;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  using namespace std::chrono;
  // timeb
  ll x;
  cin >> x;
  ll h = 0;
  ll n = x / 500;
  h += n * 1000;
  x %= 500;
  // cout<<x<<endl;
  while (x > 5) {
    ll d = x / 5;
    h += d * 5;
    x %= 5;
  }
  cout << h << endl;
  // timee
  return 0;
}
Problem p02724 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define ll long long
#define test(t)                                                                \
  ll int t;                                                                    \
  cin >> t;                                                                    \
  while (t--)
#define F(i, L, R) for (ll int i = L; i < R; i++)
#define F2(i, L, R) for (ll int i = L; i >= R; i--)
#define get1(a)                                                                \
  ll int a;                                                                    \
  cin >> a;
#define get2(a, b)                                                             \
  ll int a, b;                                                                 \
  cin >> a >> b;
#define get3(a, b, c)                                                          \
  ll int a, b, c;                                                              \
  cin >> a >> b >> c;
#define pb push_back
#define mp make_pair
#define MAX 1000000001
using namespace std;

int main() {

  ios_base::sync_with_stdio(0);

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

  get1(a);

  cout << (1000 * (a / 500) + 5 * ((a % 500) / 5));

  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define ll long long
#define test(t)                                                                \
  ll int t;                                                                    \
  cin >> t;                                                                    \
  while (t--)
#define F(i, L, R) for (ll int i = L; i < R; i++)
#define F2(i, L, R) for (ll int i = L; i >= R; i--)
#define get1(a)                                                                \
  ll int a;                                                                    \
  cin >> a;
#define get2(a, b)                                                             \
  ll int a, b;                                                                 \
  cin >> a >> b;
#define get3(a, b, c)                                                          \
  ll int a, b, c;                                                              \
  cin >> a >> b >> c;
#define pb push_back
#define mp make_pair
#define MAX 1000000001
using namespace std;

int main() {

  ios_base::sync_with_stdio(0);

  get1(a);

  cout << (1000 * (a / 500) + 5 * ((a % 500) / 5));

  return 0;
}
Problem p02724 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main(void) {
  int money;
  int kind = 0;

  do {
    scanf("%d", &money);
  } while (money <= 0 || money >= 1000000000);

  while (money >= 500) {
    kind += 1000;
    money -= 500;
  }
  while (money >= 5) {
    kind += 5;
    money -= 5;
  }
  printf("%d\n", kind);

  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int money;
  int kind = 0;

  do {
    scanf("%d", &money);
  } while (money < 0 || money > 1000000000);

  while (money >= 500) {
    kind += 1000;
    money -= 500;
  }
  while (money >= 5) {
    kind += 5;
    money -= 5;
  }
  printf("%d\n", kind);

  return 0;
}
Problem p02724 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int x;
  cin >> x;
  int y = 0;
  y = 1000 * (x / 500) + 5 * ((x % 500) / 5);
  cout << y << endl;
  return y;
}

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

int main() {
  int x;
  cin >> x;
  int y = 0;
  y = 1000 * (x / 500) + 5 * ((x % 500) / 5);
  cout << y << endl;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int K, N;
  int A[22000];

  cin >> K >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  int rightRes = 0;
  int leftRes = 0;
  int leftMax = 0;
  int rightMax = 0;
  for (int i = 0; i < N; ++i) {
    if (i == N - 1) {
      rightRes += K - A[i] + A[0];

      if (K - A[i] + A[0] > rightMax) {
        rightMax = K - A[i] + A[0];
      }
    } else {
      rightRes += A[i + 1] - A[i];
      if (A[i + 1] - A[i] > rightMax) {
        rightMax = A[i + 1] - A[i];
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    if (i == 0) {
      leftRes += K - A[N - 1] + A[0];
      if (K - A[N - 1] + A[0] > leftMax) {
        leftMax = K - A[N - 1] + A[0];
      }
    } else {
      leftRes += A[i] - A[i - 1];
      if (A[i] - A[i - 1] > leftMax) {
        leftMax = A[i + 1] - A[i];
      }
    }
  }
  rightRes -= rightMax;
  leftRes -= leftMax;

  if (rightRes > leftRes)
    cout << leftRes << endl;
  else
    cout << rightRes << endl;
}


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

int main() {
  int K, N;
  int A[220000];

  cin >> K >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  int rightRes = 0;
  int leftRes = 0;
  int leftMax = 0;
  int rightMax = 0;
  for (int i = 0; i < N; ++i) {
    if (i == N - 1) {
      rightRes += K - A[i] + A[0];

      if (K - A[i] + A[0] > rightMax) {
        rightMax = K - A[i] + A[0];
      }
    } else {
      rightRes += A[i + 1] - A[i];
      if (A[i + 1] - A[i] > rightMax) {
        rightMax = A[i + 1] - A[i];
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    if (i == 0) {
      leftRes += K - A[N - 1] + A[0];
      if (K - A[N - 1] + A[0] > leftMax) {
        leftMax = K - A[N - 1] + A[0];
      }
    } else {
      leftRes += A[i] - A[i - 1];
      if (A[i] - A[i - 1] > leftMax) {
        leftMax = A[i + 1] - A[i];
      }
    }
  }
  rightRes -= rightMax;
  leftRes -= leftMax;

  if (rightRes > leftRes)
    cout << leftRes << endl;
  else
    cout << rightRes << endl;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull unsigned long long
#define rep(n) for (ll i = 0; i < n; i++)
#define rep2(n) for (ll j = 0; j < n; ++j)
#define repd(n) for (ll i = n - 1; i >= 0; i--)
#define MOD 1000000007
#define pii pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
typedef vector<vector<ll>> mat;
#define nl "\n"
#define INF 0x3f3f3f3f
#define sz(a) a.size()

void solve() {
  ll n, k;
  cin >> n >> k;
  vll a(k);
  rep(k) cin >> a[i];
  sort(a.begin(), a.end());
  rep(k) {
    if (a[i] > n / 2 + a[0])
      a[i] -= n;
  }
  sort(a.begin(), a.end());
  ll mid = a[k / 2];
  ll s = 0;
  for (ll i = mid; i < k - 1; ++i)
    s = s + a[i + 1] - a[i];
  for (ll i = mid; i > 0; --i)
    s = s + a[i] - a[i - 1];
  cout << s;
}

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

  ll t = 1;
  // cin>>t;
  rep(t) { solve(); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull unsigned long long
#define rep(n) for (ll i = 0; i < n; i++)
#define rep2(n) for (ll j = 0; j < n; ++j)
#define repd(n) for (ll i = n - 1; i >= 0; i--)
#define MOD 1000000007
#define pii pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
typedef vector<vector<ll>> mat;
#define nl "\n"
#define INF 0x3f3f3f3f
#define sz(a) a.size()

void solve() {
  ll n, k;
  cin >> k >> n;
  ll a[n];
  rep(n) cin >> a[i];
  sort(a, a + n);
  ll mx = -1;
  rep(n - 1) { mx = max(mx, a[i + 1] - a[i]); }
  cout << k - max(mx, a[0] - a[n - 1] + k);
}

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

  ll t = 1;
  // cin>>t;
  rep(t) { solve(); }
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 1e5 + 5;
ll a[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = a[n] - k;
  ll ans = k;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  cout << ans;
}


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

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 2e5 + 5;
ll a[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = a[n] - k;
  ll ans = k;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  cout << ans;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
/*
 * @Author: Kaizyn
 * @Date: 2020-03-28 21:12:21
 * @LastEditTime: 2020-03-28 21:16:53
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const int N = 1e5 + 7;
const int MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int n, k;
int a[N];

inline void solve() {
  cin >> k >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int res = a[1] - a[n] + k;
  for (int i = 1; i < n; ++i)
    res = max(res, a[i + 1] - a[i]);
  cout << k - res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int testcase = 1;
  // cin >> testcase;
  for (int i = 1; i <= testcase; ++i) {
    solve();
  }
  return 0;
}

Fixed submission:
/*
 * @Author: Kaizyn
 * @Date: 2020-03-28 21:12:21
 * @LastEditTime: 2020-03-28 21:16:53
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const int N = 2e5 + 7;
const int MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int n, k;
int a[N];

inline void solve() {
  cin >> k >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int res = a[1] - a[n] + k;
  for (int i = 1; i < n; ++i)
    res = max(res, a[i + 1] - a[i]);
  cout << k - res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int testcase = 1;
  // cin >> testcase;
  for (int i = 1; i <= testcase; ++i) {
    solve();
  }
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \
  {                                                                            \
    cerr << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  }
void err() { cerr << "\033[39;0m" << endl; }
template <typename T, typename... A> void err(T a, A... x) {
  cerr << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 1e5 + 50;
int n, a[N], k;
int main(int argc, char const *argv[]) {
  // code
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d", &a[i]);
  }
  int mx = 0;
  a[k] = n + a[0];
  for (int i = 0; i < k; ++i)
    mx = max(mx, a[i + 1] - a[i]);
  cout << n - mx << endl;
  return 0;
}


Fixed submission:
// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \
  {                                                                            \
    cerr << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  }
void err() { cerr << "\033[39;0m" << endl; }
template <typename T, typename... A> void err(T a, A... x) {
  cerr << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 2e5 + 50;
int n, a[N], k;
int main(int argc, char const *argv[]) {
  // code
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d", &a[i]);
  }
  int mx = 0;
  a[k] = n + a[0];
  for (int i = 0; i < k; ++i)
    mx = max(mx, a[i + 1] - a[i]);
  cout << n - mx << endl;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define re register
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define lb long double
const int N = 1e4 + 10;
const int mod = 10086;
const int mod2 = 19260811;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
int a[N];
int main() {
  int k, n;
  read(k), read(n);
  for (re int i = 1; i <= n; i++)
    read(a[i]);
  int maxn = a[1] + k - a[n];
  for (re int i = 2; i <= n; i++)
    maxn = max(maxn, a[i] - a[i - 1]);
  printf("%d\n", k - maxn);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define re register
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define lb long double
const int N = 1e6 + 10;
const int mod = 10086;
const int mod2 = 19260811;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
int a[N];
int main() {
  int k, n;
  read(k), read(n);
  for (re int i = 1; i <= n; i++)
    read(a[i]);
  int maxn = a[1] + k - a[n];
  for (re int i = 2; i <= n; i++)
    maxn = max(maxn, a[i] - a[i - 1]);
  printf("%d\n", k - maxn);
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, N) for (i = 0; i < N; ++i)
#define fos(i, b, N) for (i = b; i < N; ++i)
#define forr(i, N) for (i = N - 1; i >= 0; --i)
#define disp(n) cout << n << endl;
#define M 10000007
#define ll long long
#define vll vector<long long int>
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
int main() {
  long a, n;
  cin >> a >> n;
  long N[1000], max = 0;
  int i;
  for (i = 0; i < n; ++i)
    cin >> N[i];
  for (i = 1; i < n; ++i) {
    if (N[i] - N[i - 1] > max)
      max = N[i] - N[i - 1];
  }
  if (a - N[n - 1] + N[0] > max)
    max = a - N[n - 1] + N[0];
  cout << a - max;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, N) for (i = 0; i < N; ++i)
#define fos(i, b, N) for (i = b; i < N; ++i)
#define forr(i, N) for (i = N - 1; i >= 0; --i)
#define disp(n) cout << n << endl;
#define M 10000007
#define ll long long
#define vll vector<long long int>
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
int main() {
  long a, n;
  cin >> a >> n;
  long N[n], max = 0;
  int i;
  for (i = 0; i < n; ++i)
    cin >> N[i];
  for (i = 1; i < n; ++i) {
    if (N[i] - N[i - 1] > max)
      max = N[i] - N[i - 1];
  }
  if (a - N[n - 1] + N[0] > max)
    max = a - N[n - 1] + N[0];
  cout << a - max;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02725 (C++)

Buggy submission (status: 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);

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


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);
  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;
}
Problem p02725 (C++)

Buggy submission (status: 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;
  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;
}

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;
  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;
}
Problem p02725 (C++)

Buggy submission (status: 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.at(i);
  int l = 0;
  for (int i = 0; i < N - 1; i++)
    l = max(l, A.at(i + 1) - A.at(i));
  cout << min(K - l, A.at(N - 1) - A.at(0)) << endl;
}

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);
  int l = 0;
  for (int i = 0; i < N - 1; i++)
    l = max(l, A.at(i + 1) - A.at(i));
  cout << min(K - l, A.at(N - 1) - A.at(0)) << endl;
}
Problem p02725 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02725 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02725 (C++)

Buggy submission (status: 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(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;
}


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(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;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 3e3 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

int n, k, ans;
int a[N];

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin >> k >> n;

  int cur = 0;
  FOR(i, 1, n) {
    cin >> a[i];
    if (i > 1)
      cur += a[i] - a[i - 1];
  }
  ans = cur;
  FOR(i, 2, n) {
    cur -= (a[i] - a[i - 1]);
    if (i == 2)
      cur += k - a[n] + a[1];
    else
      cur += a[i - 1] - a[i - 2];
    ans = min(ans, cur);
  }
  cout << ans;
}


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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 2e5 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

int n, k, ans;
int a[N];

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin >> k >> n;

  int cur = 0;
  FOR(i, 1, n) {
    cin >> a[i];
    if (i > 1)
      cur += a[i] - a[i - 1];
  }
  ans = cur;
  FOR(i, 2, n) {
    cur -= (a[i] - a[i - 1]);
    if (i == 2)
      cur += k - a[n] + a[1];
    else
      cur += a[i - 1] - a[i - 2];
    ans = min(ans, cur);
  }
  cout << ans;
}
Problem p02725 (C++)

Buggy submission (status: 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 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;
}


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 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;
}
Problem p02725 (C++)

Buggy submission (status: 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 + i] - A[i];
  }
  int max = 0;
  rep(i, N) {
    if (max < d[i])
      max = d[i];
  }
  cout << K - max << endl;
}

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 + 1] - A[i];
  }
  int max = 0;
  rep(i, N) {
    if (max < d[i])
      max = d[i];
  }
  cout << K - max << endl;
}
Problem p02725 (C++)

Buggy submission (status: 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[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);
}

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[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);
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int K, N;
  int b;
  cin >> K, N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  b = (K - a.at(N - 1)) + a.at(0);

  for (int i = 0; i < N - 1; i++) {
    b = max(a.at(i + 1) - a.at(i), b);
  }
  cout << K - b << endl;
}


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

int main() {
  int K, N;
  int b;
  cin >> K >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  b = (K - a.at(N - 1)) + a.at(0);

  for (int i = 0; i < N - 1; i++) {
    b = max(a.at(i + 1) - a.at(i), b);
  }
  cout << K - b << endl;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
ll a, b, c, n, i, j, k, m, t, arr[200005], ar[200005];
int main() {
  string s;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> arr[i];
  a = arr[m - 1] - arr[0];
  ll minn = arr[0];
  for (i = 1; i < n; i++) {
    minn = minn > arr[i] - arr[i - 1] ? minn : (arr[i] - arr[i - 1]);
  }
  a = a < a + arr[0] - minn + n - arr[m - 1]
          ? a
          : (a + arr[0] - minn + n - arr[m - 1]);
  cout << a;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
ll a, b, c, n, i, j, k, m, t, arr[200005], ar[200005];
int main() {
  string s;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> arr[i];
  a = arr[m - 1] - arr[0];
  ll minn = arr[0];
  for (i = 1; i < m; i++) {
    minn = minn > arr[i] - arr[i - 1] ? minn : (arr[i] - arr[i - 1]);
  }
  a = a < a + arr[0] - minn + n - arr[m - 1]
          ? a
          : (a + arr[0] - minn + n - arr[m - 1]);
  cout << a;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int, int> pii;
#define x first
#define y second

const int N = 1e5 + 100;

int a[N];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int, int> pii;
#define x first
#define y second

const int N = 2e5 + 100;

int a[N];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mx = a[0] - a[n - 1] + k;
  for (int i = 0; i < n - 1; i++)
    mx = max(mx, a[i + 1] - a[i]);
  cout << k - mx;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define print(n) cout << n << endl
#define all(n) n.begin(), n.end()

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

  ll k, n;
  cin >> k >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  a[n] = k + a[0];

  vector<ll> b(n);
  rep(i, n + 1) b[i] = a[i + 1] - a[i];

  sort(all(b));
  print(accumulate(all(b), 0LL) - b[n - 1]);
  return 0;
}

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define print(n) cout << n << endl
#define all(n) n.begin(), n.end()

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

  ll k, n;
  cin >> k >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  a[n] = k + a[0];

  vector<ll> b(n);
  rep(i, n) b[i] = a[i + 1] - a[i];

  sort(all(b));
  print(accumulate(all(b), 0LL) - b[n - 1]);
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 1e5 + 10;
int a[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> a[i];
  }
  int ans = n;
  for (int i = 2; i <= k; ++i) {
    ans = min(ans, n - a[i] + a[i - 1]);
  }
  ans = min(ans, a[k] - a[1]);
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 2e5 + 10;
int a[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> a[i];
  }
  int ans = n;
  for (int i = 2; i <= k; ++i) {
    ans = min(ans, n - a[i] + a[i - 1]);
  }
  ans = min(ans, a[k] - a[1]);
  cout << ans << '\n';
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long int k, n, a[200000], min;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  min = a[n - 1] - a[0];
  for (int i = 0; i < n - 1; i++) {
    if (k - (a[i + 1] - a[i]) < min) {
      min = k - (a[i + 1] - a[i]);
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll K, N;
  cin >> K >> N;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vec.at(i) = a;
  }
  vector<ll> len(3);
  for (ll i = 0; i < N - 1; i++) {
    len.at(i) = vec.at(i + 1) - vec.at(i);
  }
  len.at(N - 1) = K - vec.at(N - 1) + vec.at(0);
  sort(len.begin(), len.end());
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans += len.at(i);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll K, N;
  cin >> K >> N;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vec.at(i) = a;
  }
  vector<ll> len(N);
  for (ll i = 0; i < N - 1; i++) {
    len.at(i) = vec.at(i + 1) - vec.at(i);
  }
  len.at(N - 1) = K - vec.at(N - 1) + vec.at(0);
  sort(len.begin(), len.end());
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans += len.at(i);
  }
  cout << ans << endl;
}
Problem p02725 (C++)

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

  cout << n - mx;
}


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

  cout << n - mx;
}
Problem p02725 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

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

  cin >> K;
  cin >> N;

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

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

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

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

  cin >> K;
  cin >> N;

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

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

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


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