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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define pb push_back
#define powerof2 ()
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1 = 998244353;
using vl = vector<ll>;
using pll = pair<ll, ll>;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    is;

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll res = power(x, y / 2);
  res = (res * res);
  if (y & 1)
    res = (res * x);
  return res;
}

ll powermod(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  ll res = powermod(x, y / 2, mod);
  res = (res * res) % mod;
  if (y & 1)
    res = (res * x) % mod;
  return res;
}
/*int n,m2;char s[101];
char mat[501][501];
map<pair<int,int>,vector<pair<int,int>>> m,m1;

void dfs(int i,int j,int in,pair<int,int> p)
{
        int i1=i,j1=j;
        if(s[in]=='\\0')
                return;
        if(s[in]=='L')
                j1--;
        else if(s[in]=='R')
                j1++;
        else if(s[in]=='U')
                i1--;
        else
                i1++;
        if(i1<=0||j1<=0||i1>n||j1>m2||mat[i1][j1]=='#')
        {
                i1=i;
                j1=j;
        }
        in++;
        m[p].pb({i1,j1});
        dfs(i1,j1,in,p);
}

void dfs1(int i,int j,int co,set<pair<int,int>> &st,pair<int,int> p){
        if(st.find({i,j})!=st.end())
        {
                m1[p].pb({i,j});
                return;
        }
        //if(co!=0){
        m1[p].pb({i,j});
        st.insert({i,j});
        co++;
        pair<int,int> p1=m[{i,j}][m[{i,j}].size()-1];
        dfs1(p1.F,p1.S,co,st,p);
}*/
/*int n,m;
vector<vector<pair<int,int>>> gr;
vector<int> visi;
void dfsgr(int c,int x,vector<ll> &v,int &co)
{
        visi[c]=1;
        v.pb(c);co++;
        for(int i=0;i<gr[c].size();i++)
        {
                if(visi[gr[c][i].F]==0&&gr[c][i].S!=x)
                {
                        dfsgr(gr[c][i].F,x,v,co);
                }
        }
}*/
/*string s;
vector<set<int>> tre;
vector<int> h;
void dfs(int c,string &ans){
        if(h[c]==1)
                return;
        h[c]=1;
        ans.pb((char)(c+97));
        for(auto it=tre[c].begin();it!=tre[c].end();it++)
        {
                dfs(*it,ans);
        }
}*/
/*string lsfp(string &s)
{
        //cout<<s<<endl;
        int i=0,j=s.size()-1;
        while(i<j)
        {
                if(s[i]==s[j])
                {

                        int st=i,en=j;
                        //cout<<i<<" "<<j<<endl;
                        while(st<en)
                        {
                                if(s[st]!=s[en])
                                        break;
                                st++;
                                en--;
                        }
                        //cout<<st<<" "<<en<<endl;

                        if(s[st]!=s[en]){
                                i=0;j--;}
                        else
                        {
                                //cout<<st<<" "<<en<<endl;
                                string ans="";
                                for(;i<=j;i++)
                                        ans.pb(s[i]);
                                return ans;
                        }
                }
                else
                        j--;
        }
        string te="";te.pb(s[0]);
        return te;
}*/
/*map<pair<int,int>,ll> m;int fp;
ll solve1(int p,int in)
{
        if(in==n&&p!=0)
                return -(ll)1e18;
        if(p==0)
                return 0;
        if(m[{in,fp-p}]!=0)
                return m[{in,fp-p}];
        ll s=0,ma=solve1(p,in+1);
        for(int i=0;i<min(k,p);i++)
        {
                s+=v[in][i];
                ll va=solve1(p-i-1,in+1);
                ma=max(ma,s+va);
        }
        m[{in,fp-p}]=ma;
        return ma;
}*/
ll findxor(ll a) {
  switch (a & 3) // n % 4
  {
  case 0:
    return a; // if n is multiple of 4
  case 1:
    return 1; // If n % 4 gives remainder 1
  case 2:
    return a + 1; // If n % 4 gives remainder 2
  case 3:
    return 0; // If n % 4 gives remainder 3
  }
  return -1;
}
/*ll binarysearch(ll va)
{
        ll n=1;
        ll l=va+1,u=n;
        ll st=num.order_of_key(va);
        while(l+1<u)
        {
                m=(l+u)/2;
                ll in=num.order_of_key(m);
                if(in-st==m-va)
                {
                        l=m;
                }
                else
                {
            u=m;
                }
        }
        if(num1.find(l)==num1.end())
                return l;
        return u;
}*/
void solve() {
  int n, k;
  cin >> k >> n;
  int a[n + 1];
  a[0] = 0;
  int mi = (int)1e9;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++)
    mi = min(mi, k - a[i] + a[i - 1]);
  mi = min(mi, a[n] - a[1]);
  cout << mi << endl;
}
int main() {
  /*#ifndef ONLINE_JUDGE
      // for getting input from input.txt


      freopen("input.txt", "r", stdin);
      // for writing output to output.txt
      freopen("output1.txt", "w", stdout);
  #endif*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;

  for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define pb push_back
#define powerof2 ()
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1 = 998244353;
using vl = vector<ll>;
using pll = pair<ll, ll>;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    is;

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll res = power(x, y / 2);
  res = (res * res);
  if (y & 1)
    res = (res * x);
  return res;
}

ll powermod(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  ll res = powermod(x, y / 2, mod);
  res = (res * res) % mod;
  if (y & 1)
    res = (res * x) % mod;
  return res;
}
/*int n,m2;char s[101];
char mat[501][501];
map<pair<int,int>,vector<pair<int,int>>> m,m1;

void dfs(int i,int j,int in,pair<int,int> p)
{
        int i1=i,j1=j;
        if(s[in]=='\\0')
                return;
        if(s[in]=='L')
                j1--;
        else if(s[in]=='R')
                j1++;
        else if(s[in]=='U')
                i1--;
        else
                i1++;
        if(i1<=0||j1<=0||i1>n||j1>m2||mat[i1][j1]=='#')
        {
                i1=i;
                j1=j;
        }
        in++;
        m[p].pb({i1,j1});
        dfs(i1,j1,in,p);
}

void dfs1(int i,int j,int co,set<pair<int,int>> &st,pair<int,int> p){
        if(st.find({i,j})!=st.end())
        {
                m1[p].pb({i,j});
                return;
        }
        //if(co!=0){
        m1[p].pb({i,j});
        st.insert({i,j});
        co++;
        pair<int,int> p1=m[{i,j}][m[{i,j}].size()-1];
        dfs1(p1.F,p1.S,co,st,p);
}*/
/*int n,m;
vector<vector<pair<int,int>>> gr;
vector<int> visi;
void dfsgr(int c,int x,vector<ll> &v,int &co)
{
        visi[c]=1;
        v.pb(c);co++;
        for(int i=0;i<gr[c].size();i++)
        {
                if(visi[gr[c][i].F]==0&&gr[c][i].S!=x)
                {
                        dfsgr(gr[c][i].F,x,v,co);
                }
        }
}*/
/*string s;
vector<set<int>> tre;
vector<int> h;
void dfs(int c,string &ans){
        if(h[c]==1)
                return;
        h[c]=1;
        ans.pb((char)(c+97));
        for(auto it=tre[c].begin();it!=tre[c].end();it++)
        {
                dfs(*it,ans);
        }
}*/
/*string lsfp(string &s)
{
        //cout<<s<<endl;
        int i=0,j=s.size()-1;
        while(i<j)
        {
                if(s[i]==s[j])
                {

                        int st=i,en=j;
                        //cout<<i<<" "<<j<<endl;
                        while(st<en)
                        {
                                if(s[st]!=s[en])
                                        break;
                                st++;
                                en--;
                        }
                        //cout<<st<<" "<<en<<endl;

                        if(s[st]!=s[en]){
                                i=0;j--;}
                        else
                        {
                                //cout<<st<<" "<<en<<endl;
                                string ans="";
                                for(;i<=j;i++)
                                        ans.pb(s[i]);
                                return ans;
                        }
                }
                else
                        j--;
        }
        string te="";te.pb(s[0]);
        return te;
}*/
/*map<pair<int,int>,ll> m;int fp;
ll solve1(int p,int in)
{
        if(in==n&&p!=0)
                return -(ll)1e18;
        if(p==0)
                return 0;
        if(m[{in,fp-p}]!=0)
                return m[{in,fp-p}];
        ll s=0,ma=solve1(p,in+1);
        for(int i=0;i<min(k,p);i++)
        {
                s+=v[in][i];
                ll va=solve1(p-i-1,in+1);
                ma=max(ma,s+va);
        }
        m[{in,fp-p}]=ma;
        return ma;
}*/
ll findxor(ll a) {
  switch (a & 3) // n % 4
  {
  case 0:
    return a; // if n is multiple of 4
  case 1:
    return 1; // If n % 4 gives remainder 1
  case 2:
    return a + 1; // If n % 4 gives remainder 2
  case 3:
    return 0; // If n % 4 gives remainder 3
  }
  return -1;
}
/*ll binarysearch(ll va)
{
        ll n=1;
        ll l=va+1,u=n;
        ll st=num.order_of_key(va);
        while(l+1<u)
        {
                m=(l+u)/2;
                ll in=num.order_of_key(m);
                if(in-st==m-va)
                {
                        l=m;
                }
                else
                {
            u=m;
                }
        }
        if(num1.find(l)==num1.end())
                return l;
        return u;
}*/
void solve() {
  int n, k;
  cin >> k >> n;
  int a[n + 1];
  int mi = (int)1e9;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  a[n] = a[0];
  for (int i = 0; i < n; i++)
    mi = min(mi, k - abs(a[i + 1] - a[i]));
  cout << mi << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt

  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;

  for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}