Problem p02725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long long int, long long int> iil;
typedef long long ll;
typedef vector<ll> vl;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define edl '\n'
#define ll long long int
#define forn(i, n) for (int i = 0; i < n; i++)
#define fornl(i, n) for (ll i = 0; i < n; i++)
#define forni(i, j, n) for (int i = j; i < n; i++)
#define filecode                                                               \
  ifstream cin("input.txt");                                                   \
  ofstream cout("output.txt");
#define IOFAST                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define MOD 998244353
#define PI 3.14159265
#define DEBUG 0
#define g(a, b) a > b ? a : b
#define l(a, b) a < b ? a : b
bool comp(ii a, ii b) {
  return (a.first == b.first) ? a.second < b.second : a.first < b.first;
}
ll modInverse(ll a) {
  ll m = MOD;
  ll y = 0, x = 1;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += MOD;
  return x;
}

int main() {
  ll k, n;
  cin >> k >> n;
  vl A(n);
  forn(i, n) { cin >> A[i]; }
  int m = 1e7;
  forn(i, n) {
    m = l(((A[(i + n - 1) % n] - A[i] + k) % k),
          ((A[i] - A[(i - n + 1) % n] + k) % k));
  }
  cout << m << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long long int, long long int> iil;
typedef long long ll;
typedef vector<ll> vl;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define edl '\n'
#define ll long long int
#define forn(i, n) for (int i = 0; i < n; i++)
#define fornl(i, n) for (ll i = 0; i < n; i++)
#define forni(i, j, n) for (int i = j; i < n; i++)
#define filecode                                                               \
  ifstream cin("input.txt");                                                   \
  ofstream cout("output.txt");
#define IOFAST                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define MOD 998244353
#define PI 3.14159265
#define DEBUG 0
#define g(a, b) a > b ? a : b
#define l(a, b) a < b ? a : b
bool comp(ii a, ii b) {
  return (a.first == b.first) ? a.second < b.second : a.first < b.first;
}
ll modInverse(ll a) {
  ll m = MOD;
  ll y = 0, x = 1;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += MOD;
  return x;
}

int main() {
  ll k, n;
  cin >> k >> n;
  vl A(n);
  forn(i, n) { cin >> A[i]; }
  int m = 1e7;
  forn(i, n) {
    if (i == 0) {
      m = l(m, (k - abs(A[1] - A[0])));
      m = l(m, (A[n - 1] - A[0]));
    } else if (i == n - 1) {
      m = l(m, (k - abs(A[n - 1] - A[n - 2])));
      m = l(m, (A[n - 1] - A[0]));

    } else {
      m = l(m, (k - abs(A[(i + 1)] - A[i])));
      m = l(m, (k - abs(A[i] - A[(i - 1)])));
    }
  }
  cout << m << endl;
}