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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define long long long
#define PI 3.141592653589793238

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<long int, null_type, less<long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef cc_hash_table<
    long int, long int, hash<long int>, equal_to<long int>,
    direct_mask_range_hashing<long int>,
    hash_standard_resize_policy<hash_exponential_size_policy<>,
                                hash_load_check_resize_trigger<true>, true>>
    ht;
typedef gp_hash_table<long int, long int> HashTable;

vector<long int> v;
long int n;

int main() {
  IOS;
  /*    #ifndef ONLINE_JUDGE
          freopen("in.txt",  "r",  stdin);
          freopen("out.txt", "w", stdout);
      #endif */

  cin >> n;
  v.resize(n);

  for (long int i = 0; i < n; i++)
    cin >> v[i];

  long int money = 1000;

  for (long int i = 0; i < n; i++) {
    while (i + 1 < n && v[i] >= v[i + 1])
      i++;

    if (i + 1 < n) {
      long int diff = v[i + 1] - v[i];
      long int units = money / v[i];
      money += diff * units;
    }
  }

  cout << money;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define long long long
#define PI 3.141592653589793238

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<long int, null_type, less<long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef cc_hash_table<
    long int, long int, hash<long int>, equal_to<long int>,
    direct_mask_range_hashing<long int>,
    hash_standard_resize_policy<hash_exponential_size_policy<>,
                                hash_load_check_resize_trigger<true>, true>>
    ht;
typedef gp_hash_table<long int, long int> HashTable;

class SparseTable {
  vector<vector<long int>> table;

public:
  SparseTable(long int n, long int m) {
    table.resize(n);
    for (long int i = 0; i < n; i++)
      table[i].resize(m);
  }

  void buildSparseTable(vector<long int> v, long int n) {
    for (long int i = 0; i < n; i++)
      table[i][0] = v[i];

    for (long int j = 1; (1LL << j) <= n; j++) {
      for (long int i = 0; (i + (1LL << j) - 1) < n; i++)
        table[i][j] = min(table[i][j - 1], table[i + (1LL << (j - 1))][j - 1]);
    }
  }

  long int query(long int l, long int r) {
    long int j = (long int)log2(r - l + 1);
    return min(table[l][j], table[r - (1LL << j) + 1][j]);
  }
};

vector<long int> v;
long int n;

int main() {
  IOS;
  /*    #ifndef ONLINE_JUDGE
          freopen("in.txt",  "r",  stdin);
          freopen("out.txt", "w", stdout);
      #endif */

  cin >> n;
  v.resize(n);

  for (long int i = 0; i < n; i++)
    cin >> v[i];

  SparseTable st(8, n);
  st.buildSparseTable(v, n);

  vector<long int> nextGreater(n, n), nextSmaller(n, n);
  stack<long int> s;

  for (long int i = 0; i < n; i++) {
    if (s.empty() || v[s.top()] > v[i])
      s.push(i);
    else {
      while (s.size() && v[s.top()] <= v[i]) {
        nextGreater[s.top()] = i;
        s.pop();
      }
      i--;
    }
  }

  while (s.size())
    s.pop();

  for (long int i = 0; i < n; i++) {
    if (s.empty() || v[s.top()] < v[i])
      s.push(i);
    else {
      while (s.size() && v[s.top()] >= v[i]) {
        nextSmaller[s.top()] = i;
        s.pop();
      }
      i--;
    }
  }

  long int money = 1000;

  for (long int i = 0; i < n;) {
    long int curPrice = v[i];
    long int next = nextGreater[i];
    long int next2 = nextSmaller[i];

    if (next == n)
      break;
    if (next2 < next)
      i = next2;
    else {
      long int sell = v[next];
      long int profit = sell - curPrice;
      long int units = money / curPrice;
      money += units * profit;
      i = next;
    }
  }

  cout << money;

  return 0;
}