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 loop(n, i, a) for (int i = a; i < n; i++)
#define loopR(n, i, a) for (int i = n - 1; i >= a; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<ll, ll>>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\\n"
#define Endl "\\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
#define test(i) cout << i << endl
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
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__)
const ll mxN = 3e2 + 10, oo = 0x3f3f3f3f, mod = 998244353;
const double PI = 3.14159265358979323846;
int arr[mxN], dp[100][5000][500], n;
int go(int idx, int cur, int stocks) {
  if (idx == n)
    return cur;
  int &ret = dp[idx][cur][stocks];
  if (~ret)
    return ret;
  return ret = max({go(idx + 1, cur, stocks),
                    go(idx + 1, cur + stocks * arr[idx], 0),
                    go(idx + 1, cur % arr[idx], stocks + cur / arr[idx])});
}
void solve() {
  cin >> n;
  loop(n, i, 0) cin >> arr[i];
  int cur = arr[0], x = 0;
  vi v;
  loop(n, i, 1) {
    if (!x) {
      if (arr[i] > arr[i - 1])
        v.pb(cur), cur = arr[i], x = 1;
      else
        cur = min(cur, arr[i]);
    } else {
      if (arr[i] < arr[i - 1])
        v.pb(cur), cur = arr[i], x = 0;
      else
        cur = max(cur, arr[i]);
    }
  }
  v.pb(cur);
  ll res = 1000, mx = 1000, st = 0;
  loop(sz(v), i, 0) {
    if (i % 2 == 0) {
      st = res / v[i];
      res %= v[i];
    } else {
      res += st * v[i];
      st = 0;
      mx = max(res, mx);
    }
  }
  cout << mx << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast();
  solve();
}

Error: -11

Fixed submission:

#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 loop(n, i, a) for (int i = a; i < n; i++)
#define loopR(n, i, a) for (int i = n - 1; i >= a; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<ll, ll>>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\\n"
#define Endl "\\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
#define test(i) cout << i << endl
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
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__)
const ll mxN = 3e2 + 10, oo = 0x3f3f3f3f, mod = 998244353;
const double PI = 3.14159265358979323846;
int n;
int arr[mxN], dp[100][5000][500];
int go(int idx, int cur, int stocks) {
  if (idx == n)
    return cur;
  int &ret = dp[idx][cur][stocks];
  if (~ret)
    return ret;
  return ret = max({go(idx + 1, cur, stocks),
                    go(idx + 1, cur + stocks * arr[idx], 0),
                    go(idx + 1, cur % arr[idx], stocks + cur / arr[idx])});
}
void solve() {
  cin >> n;
  loop(n, i, 0) cin >> arr[i];
  mem(dp, -1);
  cout << go(0, 1000, 0) << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast();
  solve();
}