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

/*Author: Qaseem Hasan alias Mikepayne14(Codeforces,SPOJ)
                           qaseem_hasan_ (HackerEarth,Codechef,HackerRank)
*/

//---------------------------------------------------------
// #pragma GCC optimize "trapv"

#include <bits/stdc++.h>
using namespace std;
//---------------------------------------------------------
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
} // x=min,y=item
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

template <typename ForwardIterator, typename T>
T first_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::lower_bound(first, last, value);
  return (it != last) ? (it - first) : -1;
}

template <typename ForwardIterator, typename T>
T last_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::upper_bound(first, last, value);
  return ((it != last) ? (it - first) : -1) - 1;
}
//--------------------------------------------------------
#define acc accumulate
#define eb emplace_back
#define ll long long int
#define lld long long double
#define mii map<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define seti set<int>
#define F first
#define S second
#define fil(v, x) fill(all(v), x)
#define filcon(v, x) iota(all(v), x)
#define rep(n) for (int i = 0; i < n; ++i)
#define repr(n) for (int i = n - 1; i >= 0; --i)
#define erase(v, e) v.erase(std::remove(v.begin(), v.end(), e), v.end())
#define sqr(x) ((x) * (x))
#define MEM(a, b) memset(a, b, sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b)))
#define FLSH fflush(stdout)
// #define min_all(a,b,c,d,...) min({a,b,c,d,...})
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define make_unique(v)                                                         \\
  sort(all(v));                                                                \\
  v.erase(unique(all(v)), v.end());
#define INF (int)1e9
#define EPS 1e-8
#define MOD 1e9 + 7
#define tr(x) cout << #x << " = { " << x << " }\\n";
#define khatam '\\n'
#define PI acos(-1);

//--------------------------------------------------------

// int ar[40001][40001];

// ll ar[10001];

//--------------------------------------------------------
/*bool compare(const pair<int, int>&i, const pair<int, int>&j)
{
    return i.F < j.F;
}*/

void solve() {
  int n, k;
  cin >> k >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  int maxa = 0;
  for (int i = 1; i < n; i++) {
    maxa = max(maxa, v[i] - v[i - 1]);
  }
  maxa = max(maxa, k - v[n - 1] + v[0]);
  cout << k - maxa << khatam;
}

int main() {
  FAST_IO;
  /*    #ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
      #endif*/
  //--------------------------------------------------------
  solve();
}

Error: WA

Fixed submission:


/*Author: Qaseem Hasan alias Mikepayne14(Codeforces,SPOJ)
                           qaseem_hasan_ (HackerEarth,Codechef,HackerRank)
*/

//---------------------------------------------------------
// #pragma GCC optimize "trapv"

#include <bits/stdc++.h>
using namespace std;
//---------------------------------------------------------
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
} // x=min,y=item
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

template <typename ForwardIterator, typename T>
T first_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::lower_bound(first, last, value);
  return (it != last) ? (it - first) : -1;
}

template <typename ForwardIterator, typename T>
T last_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::upper_bound(first, last, value);
  return ((it != last) ? (it - first) : -1) - 1;
}
//--------------------------------------------------------
#define acc accumulate
#define eb emplace_back
#define ll long long int
#define lld long long double
#define mii map<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define seti set<int>
#define F first
#define S second
#define fil(v, x) fill(all(v), x)
#define filcon(v, x) iota(all(v), x)
#define rep(n) for (int i = 0; i < n; ++i)
#define repr(n) for (int i = n - 1; i >= 0; --i)
#define erase(v, e) v.erase(std::remove(v.begin(), v.end(), e), v.end())
#define sqr(x) ((x) * (x))
#define MEM(a, b) memset(a, b, sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b)))
#define FLSH fflush(stdout)
// #define min_all(a,b,c,d,...) min({a,b,c,d,...})
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define make_unique(v)                                                         \\
  sort(all(v));                                                                \\
  v.erase(unique(all(v)), v.end());
#define INF (int)1e9
#define EPS 1e-8
#define MOD 1e9 + 7
#define tr(x) cout << #x << " = { " << x << " }\\n";
#define khatam '\\n'
#define PI acos(-1);

//--------------------------------------------------------

// int ar[40001][40001];

// ll ar[10001];

//--------------------------------------------------------
/*bool compare(const pair<int, int>&i, const pair<int, int>&j)
{
    return i.F < j.F;
}*/

void solve() {
  int n, k;
  cin >> k >> n;
  int it;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> it;
    sum += it;
  }
  cout << sum - k << khatam;
}

int main() {
  FAST_IO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  //--------------------------------------------------------
  solve();
}