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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
//*********************************************DO IT
//NOW****************************************************************
#include <bits/stdc++.h>
#include <random>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define all(v) (v).begin(), (v).end()
#define s(v) sort(v, v + n)
#define mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
//**********************************************DO IT
//NOW***************************************************************
void fastio() {
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dp[101][1001], ans1 = 1e10, dp1[1000001];
int num[10];
signed main() {
  fastio();
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, xx = 0, ans = 0;
    cin >> n;
    int a[n + 2];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    // cout<<xx<<"\\n";
    // int c=0;
    int mm = -1, z;
    for (int i = 0; i < n - 1; i++) {
      if (a[i + 1] < a[i]) {
        ans += (a[i] - a[i + 1]);
        z = a[i] - a[i + 1];
        a[i + 1] = z + a[i + 1];
      }
      // c++;
    }
    cout << ans;
  }
}
// 🍪 🍪 🍪

Error: -11

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
//*********************************************DO IT
//NOW****************************************************************
#include <bits/stdc++.h>
#include <random>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define all(v) (v).begin(), (v).end()
#define s(v) sort(v, v + n)
#define mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
//**********************************************DO IT
//NOW***************************************************************
void fastio() {
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dp[101][1001], ans1 = 1e10, dp1[1000001];
int num[10];
signed main() {
  fastio();
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, xx, ans = 0;
    cin >> n;
    int a[n];
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    int x = *max_element(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
      if (a[i] == x) {
        xx = i;
        break;
      }
    }
    // cout<<xx<<"\\n";
    int c = 0;
    for (int i = 1; i < xx - 1; i++) {
      if (a[i + 1] < a[i])
        ans += (a[i] - a[i + 1]);
      c++;
    }
    // cout<<ans<<"\\n";
    for (int i = xx; i < n; i++) {
      if (a[i + 1] < a[i])
        ans += (x - a[i + 1]);
    }
    cout << ans;
  }
}
// 🍪 🍪 🍪