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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 1e18, MOD = 1e9 + 7, MAX = 1e5 + 2;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS int n;
  cin >> n;
  ll a[n];
  ll sum = 1000, cnt = 0;
  lp(i, 0, n) cin >> a[i];
  lp(i, 1, n) {
    if (a[i] > a[i - 1]) {
      ll x = sum / a[i - 1];
      sum -= a[i - 1] * x;
      cnt += x;
      sum += cnt * a[i];
      cnt = 0;
    }
  }
  cout << sum;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
 *  JUST KEEP GOING
 */
#define endl '\\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 1e5 + 5, mod = 1e9 + 7, mod2 = 998244353;
int n, arr[88];
int dp[81][81 * 200 + 1200][81];
int solve(int i, int money, int have) {
  if (i == n)
    return money;
  int &ret = dp[i][money][have];
  if (~ret)
    return ret;
  ret = solve(i + 1, money, have);
  for (int j = 0; j <= money / arr[i]; ++j) {
    ret = max(ret, solve(i + 1, money - arr[i] * j, have + j));
  }
  for (int j = 0; j <= have; ++j) {
    ret = max(ret, solve(i + 1, money + arr[i] * j, have - j));
  }
  return ret;
}
int main() {
  run();
  cin >> n;
  lp(i, n) cin >> arr[i];
  clr(dp, -1);
  cout << solve(0, 1000, 0);
}