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

// Hey u r far away to know this
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define pb push_back
#define ss second
#define ll long long
#define MAX 5000
#define mpi map<int, int>
#define mpll map<ll, ll>
#define pll pair<ll, ll>
#define plld pair<ll, double>
#define SKILLER                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vi std::vector<int>
#define vll std::vector<ll>
#define vs std::vector<string>
#define vvi std::vector<std::vector<int>>
#define vvll std::vector<std::vector<ll>>
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define debug(x)                                                               \\
  cout << x << " "                                                             \\
       << "\\n";
#define debug1(x, y) cout << x << " " << y << "\\n";
#define debug2(x, y, z) cout << x << " " << y << " " << z << "\\n";
#define int ll
const int N = 1e6 + 5, Mod = 1e9 + 7;
int dp[85][26005];
vector<int> v;
int n;

int32_t main() {
  SKILLER;

  cin >> n;
  int amt = 1000, stock = 0;
  int maxt = 1000;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.pb(x);
  }
  int flag = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!flag) {
      if (v[i] >= v[i + 1])
        continue;
      else {
        stock = amt / v[i];
        amt = amt % v[i];
        flag = 1;
      }
    } else {
      if (v[i] <= v[i + 1])
        continue;
      else {
        amt = amt + stock * v[i];
        stock = 0;
        flag = 0;
        // cout << amt <<" ";
        maxt = max(maxt, amt);
      }
    }
  }
  amt += stock * v[n - 1];
  maxt = max(maxt, amt);
  cout << maxt;
  return 0;
}

Error: WA

Fixed submission:

// Hey u r far away to know this
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define pb push_back
#define ss second
#define ll long long
#define MAX 5000
#define mpi map<int, int>
#define mpll map<ll, ll>
#define pll pair<ll, ll>
#define plld pair<ll, double>
#define SKILLER                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vi std::vector<int>
#define vll std::vector<ll>
#define vs std::vector<string>
#define vvi std::vector<std::vector<int>>
#define vvll std::vector<std::vector<ll>>
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define debug(x)                                                               \\
  cout << x << " "                                                             \\
       << "\\n";
#define debug1(x, y) cout << x << " " << y << "\\n";
#define debug2(x, y, z) cout << x << " " << y << " " << z << "\\n";
#define int ll
const int N = 1e6 + 5, Mod = 1e9 + 7;
int dp[85][16005];
vector<int> v;
int n;
int res(int id, int amt, int stock) {
  if (id == n)
    return amt;
  int &ans = dp[id][amt + stock * v[id]];
  if (ans != -1)
    return ans;
  ans = 0;
  ans = max(ans, res(id + 1, amt, stock));
  for (int i = 1; i <= amt / v[id]; i++) {
    ans = max(ans, res(id + 1, amt - i * v[id], stock + i));
  }
  ans = max(ans, res(id + 1, amt + stock * v[id], 0));
  return ans;
}
int32_t main() {
  SKILLER;

  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.pb(x);
  }
  cout << res(0, 1000, 0);
  return 0;
}