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

//
// Created on 2020/07/25.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {
  int n;
  cin >> n;
  vector<ll> g(n + 1, 0);
  g[0] = 1000;
  vector<ll> p(n + 1);
  for (int j = 1; j <= n; ++j) {
    cin >> p[j];
  }
  for (int i = 1; i <= n; ++i) {
    ll kabu_num = g[i - 1] / p[i];
    ll cash = g[i - 1] % p[i];

    for (int j = i; j <= n; ++j) {
      if (g[j] < (kabu_num * p[j]) + cash)
        g[j] = kabu_num * p[j] + cash;
    }
  }

  cout << g[n] << endl;
  return 0;
}


Error: -11

Fixed submission:

//
// Created on 2020/07/25.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {
  int n;
  cin >> n;
  vector<vector<set<int>>> g(
      n + 1, vector<set<int>>(5000, set<int>())); // g[date][kabu_num] = money
  g[0][0].insert(1000);
  vector<queue<pair<int, int>>> q(n + 1);
  q[0].push({0, 1000});
  for (int i = 0; i < n; ++i) {
    int price;
    cin >> price;
    //        while(!q[i].empty()){
    //            int j = q[i].front().first;
    //            int l = q[i].front().second;
    //            q[i].pop();
    for (int j = 0; j < 5000; ++j) {
      for (auto l : g[i][j]) {

        // buy
        for (int k = 0; k <= l / price; ++k) {
          g[i + 1][j + k].insert(l - (k * price));
        }
        // sale
        for (int k = 0; k <= j; ++k) {
          g[i + 1][j - k].insert(l + (price * k));
        }
      }
    }
  }
  int ans = 0;
  for (int j = 0; j < 50; ++j) {
    ans = max(*g[n][j].rbegin(), ans);
  }
  cout << ans << endl;
  return 0;
}