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

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define endl "\\n"
#define forn(i, x, n) for (int i = x; i < n; i++)
#define forit(it, m)                                                           \\
  for (decltype(m)::iterator it = m.begin(); it != m.end(); ++it)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {

  zeus();

  int n;
  cin >> n;

  int a[n];

  int money = 1000;
  int stocks = 0;

  forn(i, 0, n) { cin >> a[i]; }

  vi minima, maxima;

  if (a[0] < a[1]) {
    minima.pb(0);
  }
  forn(i, 1, n - 1) {
    if ((a[i] >= a[i - 1] && a[i] > a[i + 1]) ||
        (a[i] > a[i - 1] && a[i] >= a[i + 1])) {
      maxima.pb(i);
    } else if ((a[i] <= a[i - 1] && a[i] < a[i + 1]) ||
               (a[i] < a[i - 1] && a[i] <= a[i + 1])) {
      minima.pb(i);
    }
  }
  if (a[n - 1] > a[n - 2]) {
    maxima.pb(n - 1);
  }

  // forn(i, 0, minima.size()) {
  //     cout << minima[i] << " ";
  // }
  // cout << endl;
  // forn(i, 0, maxima.size()) {
  //     cout << maxima[i] << " ";
  // }
  // cout << endl;

  int i = 0, j = 0;
  bool mode = 0;
  int prevx = -1, prevy = -1;

  while (j != maxima.size()) {
    // cout << money << " " << stocks << endl;
    if (!mode) {
      while (minima[i] <= prevy) {
        i++;
        if (i >= minima.size()) {
          break;
        }
      }
      if (i >= minima.size()) {
        break;
      }
      int x = minima[i];
      i++;
      stocks = money / a[x];
      money = money - a[x] * stocks;
      mode = 1;
      prevx = x;
    } else {
      while (maxima[j] <= prevx) {
        j++;
        if (j >= maxima.size()) {
          break;
        }
      }
      if (j >= maxima.size()) {
        break;
      }
      int y = maxima[j];
      j++;
      money = money + a[y] * stocks;
      stocks = 0;
      prevy = y;
      mode = 0;
    }
  }

  cout << money << endl;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define endl "\\n"
#define forn(i, x, n) for (int i = x; i < n; i++)
#define forit(it, m)                                                           \\
  for (decltype(m)::iterator it = m.begin(); it != m.end(); ++it)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {

  zeus();

  int n;
  cin >> n;

  int a[n];

  int money = 1000;
  int stocks = 0;

  forn(i, 0, n) { cin >> a[i]; }

  vi minima, maxima;

  if (a[0] < a[1]) {
    minima.pb(0);
  }
  forn(i, 1, n - 1) {
    if ((a[i] >= a[i - 1] && a[i] > a[i + 1]) ||
        (a[i] > a[i - 1] && a[i] >= a[i + 1])) {
      maxima.pb(i);
    } else if ((a[i] <= a[i - 1] && a[i] < a[i + 1]) ||
               (a[i] < a[i - 1] && a[i] <= a[i + 1])) {
      minima.pb(i);
    }
  }
  if (a[n - 1] > a[n - 2]) {
    maxima.pb(n - 1);
  }

  // forn(i, 0, minima.size()) {
  //     cout << minima[i] << " ";
  // }
  // cout << endl;
  // forn(i, 0, maxima.size()) {
  //     cout << maxima[i] << " ";
  // }
  // cout << endl;

  int i = 0, j = 0;
  bool mode = 0;
  int prevx = -1, prevy = -1;

  while (j != maxima.size()) {
    // cout << money << " " << stocks << endl;
    if (!mode) {
      while (minima[i] <= prevy) {
        i++;
      }
      int x = minima[i];
      i++;
      stocks = money / a[x];
      money = money - a[x] * stocks;
      mode = 1;
      prevx = x;
    } else {
      while (maxima[j] <= prevx) {
        j++;
      }
      int y = maxima[j];
      j++;
      money = money + a[y] * stocks;
      stocks = 0;
      prevy = y;
      mode = 0;
    }
  }

  cout << money << endl;
}