Problem p02725 (C++) — buggy submission, verdict: Runtime Error

/*
    NO
        TIME
            TO
                DIE
                    --007
                            */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli mod = 998244353;
#define OS // single input use oeis
lli INF = 1e17;
lli eps = 1e-8;
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcountll(n) // no of ones bit in binary!!
#define bictz(n) __builtin_ctzll(n)    // no of trailing zeroes in binary!!
#define biclz(n) __builtin_clzll(n)    // no of leading zeroes in binary!!
#define bffs(n) __builtin_ffsll(n)     // index of first one bit!!
typedef pair<lli, lli> ll;
#define mem1(a, x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a, x) fill(&a[0][0], &a[0][0] + sizeof(a) / sizeof(a[0][0]), x)
#define mem3(a, x)                                                             \\
  fill(&a[0][0][0], &a[0][0][0] + sizeof(a) / sizeof(a[0][0][0]), x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
int fmx(int x, int y) { return (((y - x) >> (31)) & (x ^ y)) ^ y; }
int fmi(int x, int y) { return (((y - x) >> (31)) & (x ^ y)) ^ x; }
typedef tree<lli, null_type, less<lli>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;
lli n, z, k, x, m, d, y, q, b, c;
const double pi = 3.14159265358979323846;
// double dp[100005];

// bool cmp
lli dp[200005];
lli back[200005];
lli ans[200005];
int main() {

  IO;
  OS;
  cin >> k >> n;
  lli a[n + 1];
  lli mx = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

  cout << k - mx;
}

Error: WA

Fixed submission:

/*
    NO
        TIME
            TO
                DIE
                    --007
                            */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli mod = 998244353;
#define OS // single input use oeis
lli INF = 1e17;
lli eps = 1e-8;
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcountll(n) // no of ones bit in binary!!
#define bictz(n) __builtin_ctzll(n)    // no of trailing zeroes in binary!!
#define biclz(n) __builtin_clzll(n)    // no of leading zeroes in binary!!
#define bffs(n) __builtin_ffsll(n)     // index of first one bit!!
typedef pair<lli, lli> ll;
#define mem1(a, x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a, x) fill(&a[0][0], &a[0][0] + sizeof(a) / sizeof(a[0][0]), x)
#define mem3(a, x)                                                             \\
  fill(&a[0][0][0], &a[0][0][0] + sizeof(a) / sizeof(a[0][0][0]), x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
int fmx(int x, int y) { return (((y - x) >> (31)) & (x ^ y)) ^ y; }
int fmi(int x, int y) { return (((y - x) >> (31)) & (x ^ y)) ^ x; }
typedef tree<lli, null_type, less<lli>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;
lli n, z, k, x, m, d, y, q, b, c;
const double pi = 3.14159265358979323846;
// double dp[100005];

// bool cmp
lli dp[200005];
lli back[200005];
lli ans[200005];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IO;
  OS;
  cin >> k >> n;
  lli a[n + 1];
  lli mx = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

  cout << k - mx;
}