Problem p02725 (C++)

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

#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define forn(i, n) for (ll i = 0; i < n; i++)
#define forn1(i, n) for (ll i = 1; i < n; i++)
#define forab(i, a, b) for (ll i = a; i < b; i++)
#define clr(x) memset(x, 0, sizeof(x))
#define count_bit(x) __builtin_popcount(x)
#define endl "\n"
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define trace(v)                                                               \
  for (auto it = v.begin(); it != v.end(); it++)                               \
    cout << *it << " ";                                                        \
  cout << endl;
#define eps 1e-10
#define nedl endl
const ll sz = 1e6;
const ll mod = 1e9 + 7;
typedef vector<ll> VL;
typedef vector<pll> VLL;
typedef vector<vector<ll>> VVL;

ll power(ll n, ll k, ll m) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = (ans * n) % m;
    k /= 2;
    n = (n * n) % m;
  }
  return ans;
}

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll T = 1; // cin>>T;

  while (T--) {
    ll k, n;
    cin >> k >> n;
    ll a[n];
    forn(i, n) cin >> a[i];
    sort(a, a + n);
    vector<ll> v;
    forn1(i, n) { v.pb(a[i] - a[i - 1]); }
    v.pb(a[0] + k - a[n - 1]);

    sort(ALL(v));
    ll ans = 0;
    forn(i, n - 1) ans += v[i];
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// fahadmd16(IIEST Shibpur)
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define forn(i, n) for (ll i = 0; i < n; i++)
#define forn1(i, n) for (ll i = 1; i < n; i++)
#define forab(i, a, b) for (ll i = a; i < b; i++)
#define clr(x) memset(x, 0, sizeof(x))
#define count_bit(x) __builtin_popcount(x)
#define endl "\n"
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define trace(v)                                                               \
  for (auto it = v.begin(); it != v.end(); it++)                               \
    cout << *it << " ";                                                        \
  cout << endl;
#define eps 1e-10
#define nedl endl
const ll sz = 1e6;
const ll mod = 1e9 + 7;
typedef vector<ll> VL;
typedef vector<pll> VLL;
typedef vector<vector<ll>> VVL;

ll power(ll n, ll k, ll m) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = (ans * n) % m;
    k /= 2;
    n = (n * n) % m;
  }
  return ans;
}

int main() {
  IOS;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  ll T = 1; // cin>>T;

  while (T--) {
    ll k, n;
    cin >> k >> n;
    ll a[n];
    forn(i, n) cin >> a[i];
    sort(a, a + n);
    vector<ll> v;
    forn1(i, n) { v.pb(a[i] - a[i - 1]); }
    v.pb(a[0] + k - a[n - 1]);

    sort(ALL(v));
    ll ans = 0;
    forn(i, n - 1) ans += v[i];
    cout << ans << endl;
  }
  return 0;
}