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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll k, n;
  cin >> k >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  ll fans = LLONG_MAX, ans1, ans2;
  for (ll i = 0; i < n; i++) {
    ans1 = 0;
    ll j = (i + 1) % n;
    while (arr[j] != arr[i]) {
      ans1 += min(abs(arr[(j) % n] - arr[(j - 1 + n) % n]),
                  k - abs(arr[(j) % n] - arr[(j - 1 + n) % n]));
      j = (j + 1) % n;
    }

    ans2 = 0;
    j = (i - 1 + n) % n;
    while (arr[j] != arr[i]) {
      ans2 += min(abs(arr[(j) % n] - arr[(j + 1) % n]),
                  k - abs(arr[(j) % n] - arr[(j + 1) % n]));
      j = (j - 1 + n) % n;
    }
    fans = min(ans1, ans2);
  }
  cout << fans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll k, n;
  cin >> k >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  ll ans = LLONG_MIN;
  for (ll i = 1; i < n; i++)
    ans = max(ans, arr[i] - arr[i - 1]);
  ans = max(ans, (k - arr[n - 1]) + arr[0]);
  cout << k - ans << endl;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> m >> n;
  int a[1005], maxn = -1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    maxn = max(a[i + 1] - a[i], maxn);
  }
  maxn = max(m + a[0] - a[n - 1], maxn);
  cout << m - maxn;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> m >> n;
  int a[1000005], maxn = -1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    maxn = max(a[i + 1] - a[i], maxn);
  }
  maxn = max(m + a[0] - a[n - 1], maxn);
  cout << m - maxn;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[100000];
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int x = (k + a[0]) - a[n - 1];
  for (int i = 0; i < n - 1; i++) {
    x = max(x, a[i + 1] - a[i]);
  }

  cout << k - x << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[200000];
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int x = (k + a[0]) - a[n - 1];
  for (int i = 0; i < n - 1; i++) {
    x = max(x, a[i + 1] - a[i]);
  }

  cout << k - x << endl;

  return 0;
}
Problem p02725 (C++)

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

const int N = 100005;

int k, n;
int a[N];

int solve() {
  scanf("%d %d", &k, &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

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

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  return 0;
}


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

const int N = 1000005;

int k, n;
int a[N];

int solve() {
  scanf("%d %d", &k, &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

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

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  return 0;
}
Problem p02725 (C++)

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

int main() {
  long long k, n, ans = 2000000;
  cin >> k >> n;
  long long a[300000];
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    a[i] = l;
  }
  for (int i = 0; i < n; i++) {
    a[i + n] = a[i] + k;
  }
  for (int i = 0; i < n; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  cout << ans << endl;
}


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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int K, N;
vector<int> A(2 * 10 ^ 5);
int tmp = 0;
int ans = 0;
int tmpMaxDist = 0;
int edgeDist = 0;

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

  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > tmpMaxDist) {
      tmpMaxDist = A[i] - A[i - 1];
    }
  }

  edgeDist = (K - A[N - 1]) + A[0];

  if (edgeDist > tmpMaxDist) {
    ans = K - edgeDist;
  } else {
    ans = K - tmpMaxDist;
  }

  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int K, N;
vector<int> A(2 * 10 * 10 * 10 * 10 * 10);
int tmp = 0;
int ans = 0;
int tmpMaxDist = 0;
int edgeDist = 0;

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

  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > tmpMaxDist) {
      tmpMaxDist = A[i] - A[i - 1];
    }
  }

  edgeDist = (K - A[N - 1]) + A[0];

  if (edgeDist > tmpMaxDist) {
    ans = K - edgeDist;
  } else {
    ans = K - tmpMaxDist;
  }

  cout << ans;
}
Problem p02725 (C++)

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

const int N = 1e5 + 40;

long long a[N], k, mn = 1e18, n;

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

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

const int N = 2e5 + 40;

long long a[N], k, mn = 1e18, n;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int k, n, arr[100005], mini;
int main() {
  cin >> k >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  mini = arr[n - 1] - arr[0];
  for (int i = 1; i < n; ++i) {
    int tmp;
    tmp = k - arr[i] + arr[i - 1];
    mini = min(mini, tmp);
  }
  cout << mini << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int k, n, arr[200005], mini;
int main() {
  cin >> k >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  mini = arr[n - 1] - arr[0];
  for (int i = 1; i < n; ++i) {
    int tmp;
    tmp = k - arr[i] + arr[i - 1];
    mini = min(mini, tmp);
  }
  cout << mini << "\n";
}
Problem p02725 (C++)

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

using namespace std;

const int N = 100010;

int k, n, a[N], maxn = -1;

int main() {
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  for (int i = 2; i <= n + 1; i++)
    maxn = max(maxn, a[i] - a[i - 1]);
  cout << k - maxn << endl;

  return 0;
}

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

using namespace std;

const int N = 1000010;

int k, n, a[N], maxn = -1;

int main() {
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  for (int i = 2; i <= n + 1; i++)
    maxn = max(maxn, a[i] - a[i - 1]);
  cout << k - maxn << endl;

  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
int c[N];
int main() {
  int k, n;
  scanf("%d %d", &k, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  int ans = c[n] - c[1];
  for (int i = 2; i <= n; i++) {
    ans = min(ans, k - c[i] + c[i - 1]);
  }
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
int c[N];
int main() {
  int k, n;
  scanf("%d %d", &k, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  int ans = c[n] - c[1];
  for (int i = 2; i <= n; i++) {
    ans = min(ans, k - c[i] + c[i - 1]);
  }
  printf("%d", ans);
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[100010];
int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = mod;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      ans = min(ans, K - (K - (A[N - 1] - A[i])));
      ans = min(ans, K - (A[i + 1] - A[i]));
    } else if (i == N - 1) {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (K - (A[N - 1] - A[0])));
    } else {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (A[i + 1] - A[i]));
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[200010];
int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = mod;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      ans = min(ans, K - (K - (A[N - 1] - A[i])));
      ans = min(ans, K - (A[i + 1] - A[i]));
    } else if (i == N - 1) {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (K - (A[N - 1] - A[0])));
    } else {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (A[i + 1] - A[i]));
    }
  }
  cout << ans << endl;
}
Problem p02725 (C++)

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

#define pb push_back
#define ss second
#define ff first
#define N 100005
#define inf 1000000009
#define ll long long
#define all(a) a.begin(), a.end()
#define mid(a, b) (a + b) / 2
#define pii pair<int, int>

using namespace std;

int k, n, a[N], d[N], mx;

int main() {
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    d[i] = a[i] - a[i - 1];
    mx = max(mx, d[i]);
  }
  mx = max(mx, k - a[n] + a[1]);
  cout << k - mx;
}


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

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define all(a) a.begin(), a.end()
#define mid(a, b) (a + b) / 2
#define pii pair<int, int>

using namespace std;

int k, n, a[N], d[N], mx;

int main() {
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    d[i] = a[i] - a[i - 1];
    mx = max(mx, d[i]);
  }
  mx = max(mx, k - a[n] + a[1]);
  cout << k - mx;
}
Problem p02725 (C++)

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

typedef long long ll;

const int maxn = 200005;

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

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

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

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

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

  cout << sol << "\n";

  return 0;
}


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

typedef long long ll;

const int maxn = 200005;

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

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

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

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

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

  cout << sol << "\n";

  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define MAX_N 400

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int K, N;
int A[MAX_N];

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

  int max_diff = 0;
  int tmp = A[N - 1];
  for (int i = N - 1; i > 0; i--) {
    A[i] -= A[i - 1];
  }
  A[0] = A[0] + K - tmp;
  for (int i = 0; i < N; i++) {
    chmax(max_diff, A[i]);
  }
  cout << K - max_diff << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define MAX_N 200000

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int K, N;
int A[MAX_N];

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

  int max_diff = 0;
  int tmp = A[N - 1];
  for (int i = N - 1; i > 0; i--) {
    A[i] -= A[i - 1];
  }
  A[0] = A[0] + K - tmp;
  for (int i = 0; i < N; i++) {
    chmax(max_diff, A[i]);
  }
  cout << K - max_diff << endl;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define MOD1 1000000007
#define MOD2 998244353
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace std;

ll k, n;
vector<ll> h;
void solve() {
  cin >> k >> n;
  h.resize(n);
  for (auto &i : h)
    cin >> i;
  ll ans = k - h[n - 1] + h[0];
  for (int i = 1; i < n; i++)
    ans = max(ans, h[i] - h[i - 1]);
  cout << k - ans;
}

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

  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define MOD1 1000000007
#define MOD2 998244353
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace std;

ll k, n;
vector<ll> h;
void solve() {
  cin >> k >> n;
  h.resize(n);
  for (auto &i : h)
    cin >> i;
  ll ans = k - h[n - 1] + h[0];
  for (int i = 1; i < n; i++)
    ans = max(ans, h[i] - h[i - 1]);
  cout << k - ans;
}

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

  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
}
void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("story.in", "r", stdin);*/
#endif
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int main() {
  fast();
  File();
  int n, m;
  cin >> m >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(all(arr));
  ll sum = 0;
  vector<int> res;
  for (int i = 0; i < n - 1; i++) {
    res.push_back(arr[i + 1] - arr[i]);
  }
  res.push_back(m - arr[n - 1] + arr[0]);
  sort(all(res));
  for (int i = 0; i < n - 1; i++)
    sum += res[i];
  cout << sum << endl;
  return 0;
}

Fixed submission:
#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
}
void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("story.in", "r", stdin);*/
#endif
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int main() {
  fast();
  // File();
  int n, m;
  cin >> m >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(all(arr));
  ll sum = 0;
  vector<int> res;
  for (int i = 0; i < n - 1; i++) {
    res.push_back(arr[i + 1] - arr[i]);
  }
  res.push_back(m - arr[n - 1] + arr[0]);
  sort(all(res));
  for (int i = 0; i < n - 1; i++)
    sum += res[i];
  cout << sum << endl;
  return 0;
}
Problem p02725 (C++)

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

using namespace std;

const int mod = 1007050321;
const int base = 521;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9;

int n, K, a[N];

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

  cin >> K >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[i + n] = a[i] + K;
  }

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

  cout << ans;
  return 0;
}

/// a[i] -> a[n] -> a[i] -> a[1]
/// a[n] - a[i-1] + a[n] - a[i-1] + a[i] - a[1]

/// a[i] -> a[n] -> a[1] -> a[i-1]
/// a[n] - a[i-1] + K - a[n] + a[1] + a[i-1] - a[1]


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

using namespace std;

const int mod = 1007050321;
const int base = 521;
const int N = 4e5 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9;

int n, K, a[N];

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

  cin >> K >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[i + n] = a[i] + K;
  }

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

  cout << ans;
  return 0;
}

/// a[i] -> a[n] -> a[i] -> a[1]
/// a[n] - a[i-1] + a[n] - a[i-1] + a[i] - a[1]

/// a[i] -> a[n] -> a[1] -> a[i-1]
/// a[n] - a[i-1] + K - a[n] + a[1] + a[i-1] - a[1]
Problem p02725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  uint64_t K, N;
  cin >> K;
  cin >> N;

  vector<uint64_t> A;
  for (uint64_t i = 0; i < N; i++) {
    uint64_t tmp;
    cin >> tmp;
    A.push_back(tmp);
  }

  uint64_t i = 0;
  uint64_t ans = UINT64_MAX;
  const uint64_t max_idx = A.size() - 1;
  do {
    uint64_t tmp = 0;
    uint64_t count;
    uint64_t idx0;
    uint64_t idx1;

    // 時計回り。
    count = 0;
    idx0 = (i <= max_idx) ? i : 0;
    idx1 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
    while (count < A.size() - 1) {
      int64_t a0 = (int64_t)A[idx0];
      int64_t a1 = (int64_t)A[idx1];
      uint64_t t = (a1 - a0 >= 0) ? a1 - a0 : K + a1 - a0;
      tmp += t;

      idx0 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
      idx1 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
      count++;
    }
    ans = min(ans, tmp);
#if 0
        // 反時計回り。
        count = 0;
        idx0 = (i <= 0) ? i : max_idx;
        idx1 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
        while(count < A.size()-1) {
            int64_t a0 = (int64_t)A[idx0];
            int64_t a1 = (int64_t)A[idx1];
            uint64_t t = (a1 - a0 >= 0) ? a1 - a0 : K + a1 - a0;
            tmp += t;
            
            idx0 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
            idx1 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
            count++;
        }
        ans = min(ans, tmp);
#endif
    i++;
  } while (i < A.size());

  cout << ans;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  uint64_t K, N;
  cin >> K;
  cin >> N;

  vector<uint64_t> A;
  for (uint64_t i = 0; i < N; i++) {
    uint64_t tmp;
    cin >> tmp;
    A.push_back(tmp);
  }

  uint64_t max_dist = 0;
  for (uint64_t i = 0; i < N - 1; i++) {
    uint64_t dist = A[i + 1] - A[i];
    max_dist = max(max_dist, dist);
  }
  max_dist = max(max_dist, A[0] - A[N - 1] + K);
  uint64_t ans = K - max_dist;
  cout << ans;
  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)
using namespace std;
typedef long long ll;

int main(void) {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i + n] = a[i] + k;
  int answer = k;
  rep(i, n) {
    if (a[i + n - 1] - a[i] < answer)
      answer = a[i + n - 1] - a[i];
  }
  cout << answer << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main(void) {
  int k, n;
  cin >> k >> n;
  vector<int> a(2 * n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i + n] = a[i] + k;
  int answer = k;
  rep(i, n) {
    if (a[i + n - 1] - a[i] < answer)
      answer = a[i + n - 1] - a[i];
  }
  cout << answer << endl;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
/******************************************
/@Author: LeafBelief
/@Date: 2020-04-07
/@Remark:
/@FileName: minumdistance
******************************************/
#include <bits/stdc++.h>
#define CSE(x, y) memset(x, y, sizeof(x))
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int maxn = 111111;
ll dis[maxn];

int main() {
  FAST;
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> dis[i];
  }
  ll all = 0, mxdis = 0;
  for (int i = 1; i < n; i++) {
    all += dis[i] - dis[i - 1];
    mxdis = max(mxdis, dis[i] - dis[i - 1]);
  }
  all += k - dis[n - 1] + dis[0] - 0;
  mxdis = max(mxdis, k - dis[n - 1] + dis[0] - 0);
  cout << all - mxdis << endl;
  return 0;
}

Fixed submission:
/******************************************
/@Author: LeafBelief
/@Date: 2020-04-07
/@Remark:
/@FileName: minumdistance
******************************************/
#include <bits/stdc++.h>
#define CSE(x, y) memset(x, y, sizeof(x))
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int maxn = 211111;
ll dis[maxn];

int main() {
  FAST;
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> dis[i];
  }
  ll all = 0, mxdis = 0;
  for (int i = 1; i < n; i++) {
    all += dis[i] - dis[i - 1];
    mxdis = max(mxdis, dis[i] - dis[i - 1]);
  }
  all += k - dis[n - 1] + dis[0] - 0;
  mxdis = max(mxdis, k - dis[n - 1] + dis[0] - 0);
  cout << all - mxdis << endl;
  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

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

Fixed submission:
#include <iostream>
using namespace std;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 110;
int n, a[N], k;

int main() {

  cin >> k >> n;

  int ans = 0x3f3f3f3f;

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

  for (int i = 0; i < n; i++) {
    int pre = (i - 1 + n) % n;
    int nex = (i + 1) % n;

    int x = (a[pre] - a[i] + k) % k;
    ans = min(ans, x);
    x = (a[i] - a[nex] + k) % k;
    ans = min(ans, x);
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 2e5 + 100;
int n, a[N], k;

int main() {

  cin >> k >> n;

  int ans = 0x3f3f3f3f;

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

  for (int i = 0; i < n; i++) {
    int pre = (i - 1 + n) % n;
    int nex = (i + 1) % n;

    int x = (a[pre] - a[i] + k) % k;
    ans = min(ans, x);
    x = (a[i] - a[nex] + k) % k;
    ans = min(ans, x);
  }

  cout << ans << endl;
  return 0;
}
Problem p02725 (Python)

Buggy submission (status: Runtime Error):
K, N = [int(x) for x in input().split()]
A = list([int(x) for x in input().split()])

result = []

for i in range(N):
    if i == N - 1:
        result.append(K - A[i] + A[0])
    else:
        result.appnd(A[i + 1] - A[i])

result.sort()

print(sum(result[:-1]))


Fixed submission:
K, N = [int(x) for x in input().split()]
A = list([int(x) for x in input().split()])

result = []

for i in range(N):
    if i == N - 1:
        result.append(K - A[i] + A[0])
    else:
        result.append(A[i + 1] - A[i])

result.sort()

print(sum(result[:-1]))
Problem p02725 (Python)

Buggy submission (status: Runtime Error):
K, N = map(int, input().split())
A = [int(e) for e in input().split()]

ans = 0
for i in range(N - 1):
    S = [A[i + 1] - A[i]]
    S.append((K - A[N - 1]) + A[0])
    m = sorted(S)
    for n in range(N - 1):
        ans += m[n]
print(ans)


Fixed submission:
K, N = map(int, input().split())
A = [int(e) for e in input().split()]

ans = 0
S = [(A[i + 1] - A[i]) for i in range(N - 1)]
S.append((K - A[N - 1]) + A[0])
S.sort
m = sorted(S)
for n in range(N - 1):
    ans += m[n]
print(ans)
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int t = 1e6, a[20005];
int main() {
  int i, k, n, p;
  scanf("%d %d", &k, &n);
  scanf("%d", &a[0]);
  for (i = 1; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  for (i = 1; i < n; i++) {
    p = k - a[i] + a[i - 1];
    if (p < t)
      t = p;
  }
  p = a[n - 1] - a[0];
  if (p < t)
    t = p;
  printf("%d", t);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int t = 1e6, a[200005];
int main() {
  int i, k, n, p;
  scanf("%d %d", &k, &n);
  scanf("%d", &a[0]);
  for (i = 1; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  for (i = 1; i < n; i++) {
    p = k - a[i] + a[i - 1];
    if (p < t)
      t = p;
  }
  p = a[n - 1] - a[0];
  if (p < t)
    t = p;
  printf("%d", t);
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
int main() {
  int k, n;
  int sum = 0;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> d(n);
  for (int i = 1; i < n; i++) {
    d[i] = a[i] - a[i - 1];
  }
  d[0] = k + a[0] - a[n - 1];
  sort(d.begin(), d.end());
  rep(i, n - 1) sum += d[i];
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
int main() {
  int k, n;
  int sum = 0;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> d(n);
  for (int i = 1; i < n; i++) {
    d[i] = a[i] - a[i - 1];
  }
  d[0] = k + a[0] - a[n - 1];
  sort(d.begin(), d.end());
  rep(i, n - 1) sum += d[i];
  cout << sum << endl;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <string.h>
int lst[110];
int main() {
  int k;
  int n;
  int ans;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> lst[i];
  }
  ans = -lst[n - 1] + lst[0] + k;
  for (int i = 0; i < n - 1; i++) {
    if (lst[i + 1] - lst[i] > ans)
      ans = lst[i + 1] - lst[i];
  }
  cout << k - ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <string.h>
int lst[200010];
int main() {
  int k;
  int n;
  int ans;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> lst[i];
  }
  ans = -lst[n - 1] + lst[0] + k;
  for (int i = 0; i < n - 1; i++) {
    if (lst[i + 1] - lst[i] > ans)
      ans = lst[i + 1] - lst[i];
  }
  cout << k - ans << endl;
  return 0;
}
Problem p02725 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define PB push_back
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll K, N, A[10000], sec[10000];
  cin >> K >> N;
  F(i, 0, N, 1)
  cin >> A[i];
  ll j = 0;
  F(i, 0, N - 1, 1) {
    sec[j] = A[i + 1] - A[i];
    j++;
  }
  sec[j] = K - (A[N - 1] - A[0]);
  ll mini = INT_MAX, sum = 0, sumi;
  F(i, 0, j + 1, 1) { sum += sec[i]; }
  F(i, 0, j + 1, 1) {
    sumi = sum - sec[i];
    if (mini > sumi) {
      mini = sumi;
    }
  }

  cout << mini;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define PB push_back
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll K, N, A[1000000], sec[1000000];
  cin >> K >> N;
  F(i, 0, N, 1)
  cin >> A[i];
  ll j = 0;
  F(i, 0, N - 1, 1) {
    sec[j] = A[i + 1] - A[i];
    j++;
  }
  sec[j] = K - (A[N - 1] - A[0]);
  ll mini = INT_MAX, sum = 0, sumi;
  F(i, 0, j + 1, 1) { sum += sec[i]; }
  F(i, 0, j + 1, 1) {
    sumi = sum - sec[i];
    if (mini > sumi) {
      mini = sumi;
    }
  }

  cout << mini;

  return 0;
}
Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;

int k, n;
int a[111111];
signed main() {

  cin >> k >> n;
  rep(i, n) cin >> a[i];
  a[n] = a[0] + k;
  int ans = 0;
  rep(i, n) { ans = max(a[i + 1] - a[i], ans); }
  cout << k - ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;

int k, n;
int a[211111];
signed main() {

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a[100005];
  int k, n;
  cin >> k >> n;
  a[0] = 0;
  a[n + 1] = k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = a[n] - a[1];
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i + 1] - a[i]));
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a[200005];
  int k, n;
  cin >> k >> n;
  a[0] = 0;
  a[n + 1] = k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = a[n] - a[1];
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i + 1] - a[i]));
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  printf("%d\n", ans);
}
Problem p02725 (C++)

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


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

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

  int ans = A[N - 1] - A[0];

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

  cout << ans << endl;
}


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

int main() {

  long long K, N;
  vector<long long> A(N);
  cin >> K >> N;

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

  int ans = A[N - 1] - A[0];

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

  cout << ans << endl;
}
Problem p02725 (C++)

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

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

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


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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 100010;

ll dis(ll x, ll y, ll n) { return n - min(abs(x - y), n - abs(x - y)); }

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

  ll n, m;
  cin >> n >> m;
  ll ar[m];
  for (ll i = 0; i < m; ++i) {
    cin >> ar[i];
  }
  ll ans = INT_MAX;
  for (ll i = 0; i < n; ++i) {
    ans = min(ans, dis(ar[i], ar[(i + 1) % m], n));
    ans = min(ans, dis(ar[i], ar[(i - 1 + m) % m], n));
  }
  cout << ans;

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 100010;

ll dis(ll x, ll y, ll n) { return n - min(abs(x - y), n - abs(x - y)); }

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

  ll n, m;
  cin >> n >> m;
  ll ar[m];
  for (ll i = 0; i < m; ++i) {
    cin >> ar[i];
  }
  ll ans = INT_MAX;
  for (ll i = 0; i < m; ++i) {
    ans = min(ans, dis(ar[i], ar[(i + 1) % m], n));
    ans = min(ans, dis(ar[i], ar[(i - 1 + m) % m], n));
  }
  cout << ans;

  return (0);
}