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

/*
    ThunderClap and Flash
    Raunaq Singh
*/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define ThunderClap ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define debugg(something) cout << #something << " - " << something << "\\n";
#define vi vector
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define LB lower_bound
#define UB upper_bound
#define mp make_pair
#define pb push_back
#define in insert
#define s second
#define f first
#define pQueue priority_queue
#define maxElement *max_element
#define minElement *min_element
#define MAX 100000LL
#define mod 1000000007LL
#define inf 1000000000000000000LL
#define mI2 500000004LL
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }

void Flash() {
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll currMoney = 1000;
  ll currStocks;
  ll ans = 1000;
  for (ll i = 0; i < n - 1; i++) {
    if (a[i + 1] > a[i]) {
      currStocks = (currMoney / a[i]);
      currMoney = (currMoney % a[i]);

      currMoney += (currStocks * a[i + 1]);
      currStocks = 0;
    }
  }
  ans = max(ans, currMoney);
  cout << ans << "\\n";
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ThunderClap;
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    Flash();
  }
  return 0;
}


Error: -11

Fixed submission:

/*
    ThunderClap and Flash
    Raunaq Singh
*/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define ThunderClap ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define debugg(something) cout << #something << " - " << something << "\\n";
#define vi vector
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define LB lower_bound
#define UB upper_bound
#define mp make_pair
#define pb push_back
#define in insert
#define s second
#define f first
#define pQueue priority_queue
#define maxElement *max_element
#define minElement *min_element
#define MAX 100000LL
#define mod 1000000007LL
#define inf 1000000000000000000LL
#define mI2 500000004LL
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }

ll dp[81][10000];

ll findAns(ll i, ll j, ll money, ll a[], ll n) {
  if (i == n) {
    return money;
  }
  if (dp[i][money] != -1) {
    return dp[i][money];
  }
  ll ret = findAns(i + 1, j, money, a, n);
  ll currMoney = money - a[i];
  ll cnt = j + 1;
  while (currMoney >= 0) {
    currMoney -= a[i];
    ++cnt;
  }
  ret = max(ret, findAns(i + 1, cnt - 1, currMoney + a[i], a, n));

  currMoney = money + a[i];
  cnt = j - 1;
  while (cnt >= 0) {
    currMoney += a[i];
    --cnt;
  }
  ret = max(ret, findAns(i + 1, cnt + 1, currMoney - a[i], a, n));
  return dp[i][money] = ret;
}

void Flash() {
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  ll ans = 1000;
  ans = max(ans, findAns(0, 0, 1000, a, n));
  cout << ans << "\\n";
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ThunderClap;
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    Flash();
  }
  return 0;
}