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

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
map<int, int> ct;
map<int, int> ct2;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i = (a); i <= (signed)(b); i++)
#define AFOR(i, a) for (int i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 100005
int n, m, l;
int a, b, c;
string s[MN];
string str;
string ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*
int N;
cin >> N;
vi vect(N);
FOR(i,N) cin >> vec.at(i);*/
// sort(vec.begin(),vec.end());
// vvi data(N,vi(4));

/*	int bfs(int x, int y,vvi &A,vvi flag) {
                queue<pii> que;
                que.push(pii(x, y));
                while (que.size()) {
                        pii p = que.front(); que.pop();
                        AFOR(i, 4) {
                                int nx = p.first + dx[i]; int ny = p.second +
   dy[i]; if (0 <= nx && nx < N && 0 <= ny && ny < N) { if
   (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
   A.at(nx).at(ny)) { A.at(nx).at(ny) = A.at(p.first).at(p.second) +
   flag.at(p.first).at(p.second); que.push(pii(nx, ny));
                                        }
                                }
                        }
                }
                return 0;
        }
        */
/*int gcd(int p, int q){
        while(q != 0){
        int r = p % q;
        p = q;
        q = r;
        }
return p;
}*/
int N;
int A[200100];
int L[200100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int K;
  cin >> K >> N;

  AFOR(i, N) { cin >> A[i]; }
  int ans = A[1] - A[0];
  AFOR(i, N - 2) { ans = max(A[i + 2] - A[i + 1], ans); }
  ans = max(A[0] + K - A[N - 1], ans);

  cout << K - ans << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
map<int, int> ct;
map<int, int> ct2;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i = (a); i <= (signed)(b); i++)
#define AFOR(i, a) for (int i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 100005
int n, m, l;
int a, b, c;
string s[MN];
string str;
string ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*
int N;
cin >> N;
vi vect(N);
FOR(i,N) cin >> vec.at(i);*/
// sort(vec.begin(),vec.end());
// vvi data(N,vi(4));

/*	int bfs(int x, int y,vvi &A,vvi flag) {
                queue<pii> que;
                que.push(pii(x, y));
                while (que.size()) {
                        pii p = que.front(); que.pop();
                        AFOR(i, 4) {
                                int nx = p.first + dx[i]; int ny = p.second +
   dy[i]; if (0 <= nx && nx < N && 0 <= ny && ny < N) { if
   (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
   A.at(nx).at(ny)) { A.at(nx).at(ny) = A.at(p.first).at(p.second) +
   flag.at(p.first).at(p.second); que.push(pii(nx, ny));
                                        }
                                }
                        }
                }
                return 0;
        }
        */
/*int gcd(int p, int q){
        while(q != 0){
        int r = p % q;
        p = q;
        q = r;
        }
return p;
}*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long K;
  long long N;
  cin >> K >> N;
  vll A(N);
  vll L(N);
  AFOR(i, N) { cin >> A.at(i); }
  L.at(0) = A.at(1) - A.at(0);
  long long ans = L.at(0);
  AFOR(i, N - 1) {
    L.at(i + 1) = (A.at(i + 2) - A.at(i + 1));
    if (L.at(i + 1) > L.at(i)) {
      ans = L.at(i + 1);
    }
  }
  if (A.at(0) + K - A.at(N - 1) > ans) {
    ans = A.at(0) + K - A.at(N - 1);
  }

  ans = K - ans;
  cout << ans << endl;

  return 0;
}