Problem p02725 (C++)

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

#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define T()                                                                    \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  rep1(qq, 1, tt)
#define rep(i, start, lim) for (int i = start; i < lim; i++)
#define rep1(i, start, lim) for (int i = start; i <= lim; ++i)
#define repd(i, start, lim) for (int i = start; i >= lim; i--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define br cout << "\n"
#define MOD (1000000007LL)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define eps (1e-6)
#define what_is(x) cerr << #x << " is " << x << "\n";
#define int long long int
#define infll 9e18
#define inf 1e12

#define lnode (2 * node + 1)
#define rnode (2 * node + 2)

using namespace std;

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

const int N = 1e5 + 5;

int k, n;
int a[N];

int32_t main() {

  FIO
          // cout << fixed << setprecision(10);
          // freopen("input.txt", "r", stdin);
          // freopen("output.txt", "w", stdout);

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

  return 0;
}

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

#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define T()                                                                    \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  rep1(qq, 1, tt)
#define rep(i, start, lim) for (int i = start; i < lim; i++)
#define rep1(i, start, lim) for (int i = start; i <= lim; ++i)
#define repd(i, start, lim) for (int i = start; i >= lim; i--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define br cout << "\n"
#define MOD (1000000007LL)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define eps (1e-6)
#define what_is(x) cerr << #x << " is " << x << "\n";
#define int long long int
#define infll 9e18
#define inf 1e12

#define lnode (2 * node + 1)
#define rnode (2 * node + 2)

using namespace std;

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

const int N = 200005;

int k, n;
int a[N];

int32_t main() {

  FIO
          // cout << fixed << setprecision(10);
          // freopen("input.txt", "r", stdin);
          // freopen("output.txt", "w", stdout);

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

  return 0;
}