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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define endl '\\n'
#define pb push_back
#define t_case                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
#define all(v) v.begin(), v.end()
#define ub(a, x) upper_bound(all(a), x) - a.begin()
#define lb(a, x) lower_bound(all(a), x) - a.begin()
#define fr(a, b, c) for (ll a = b; a < c; a++)
#define take(xx, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> xx[i];
#define take1(xx, n)                                                           \\
  for (int i = 1; i <= n; i++)                                                 \\
    cin >> xx[i];
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int main() {
  fastio
  // t_case
  {
    ll n, m, q, ans = 1000, f = 1, d, k, x, y, sum = 0, h, w;
    cin >> n;
    ll a[n + 1];
    take(a, n);
    fr(i, 0, n - 1) {
      if (a[i] < a[i + 1]) {
        x = ans / a[i];
        ans += x * (a[i + 1] - a[i]);
      }
    }
    cout << ans;
    cout << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define endl '\\n'
#define pb push_back
#define t_case                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
#define all(v) v.begin(), v.end()
#define ub(a, x) upper_bound(all(a), x) - a.begin()
#define lb(a, x) lower_bound(all(a), x) - a.begin()
#define fr(a, b, c) for (ll a = b; a < c; a++)
#define take(xx, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> xx[i];
#define take1(xx, n)                                                           \\
  for (int i = 1; i <= n; i++)                                                 \\
    cin >> xx[i];
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int N;
int a[81], arr[2][82][5005];
int stock(int cnt, int n, int money) {
  if (n == N)
    return money;
  if (n == N - 1)
    return money + cnt * a[n];
  if (cnt > 0 && arr[1][n][money] != -1)
    return arr[1][n][money];
  if (arr[0][n][money] != -1 && cnt == 0)
    return arr[0][n][money];
  if (cnt > 0)
    return arr[1][n][money] = max(stock(0, n + 1, money + (cnt * a[n])),
                                  stock(cnt, n + 1, money));
  return arr[0][n][money] = max(stock(money / a[n], n + 1, money % a[n]),
                                stock(0, n + 1, money));
}
int main() {
  fastio
  // t_case
  {
    ll sum = 0, ans = 1, f = 1, n, m, k, x, y, z;
    cin >> N;
    take(a, N);
    fr(i, 0, 2) fr(j, 0, 82) fr(l, 0, 5005) arr[i][j][l] = -1;
    cout << stock(0, 0, 1000);

    cout << endl;
  }
  return 0;
}