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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2 * acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 100010;
const int MX = 123456;
const int mod = (int)1e9 + 7;
const int base = 97;
const ull BS1 = 10000019ULL;
const int INF = (1 << 29);

template <class T> inline void fastScan(T &x) {
  register char c = getchar();
  int neg = 0;
  x = 0;
  for (; (c < 48 || c > 57) && (c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}

// OrderedSet
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template < typename T > using ordered_set = tree < T, null_type, greater < T
// >, rb_tree_tag, tree_order_statistics_node_update >;

// typedef uint64_t hash_t;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// const int HASH_COUNT = 2;
// const hash_t HASH_P = (uint32_t)-13337;
// uniform_int_distribution<hash_t> MULT_DIST(0.25 * HASH_P, 0.75 * HASH_P);
// const hash_t HASH_MULT[HASH_COUNT] = {MULT_DIST(rng), MULT_DIST(rng)};

int n;

ll inp[N];
ll dp[105];

ll f(ll hand, ll x, ll y) {
  ll how = hand / x;
  return (hand % x) + how * y;
}

ll Solve() {
  dp[1] = 1000;
  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 1; j < i; j++) {
      if (inp[j] < inp[i]) {
        dp[i] = max(dp[i], f(dp[j], inp[j], inp[i]));
      }
    }
  }
  return dp[n];
}

int main() {
  fastScan(n);
  for (int i = 1; i <= n; i++) {
    fastScan(inp[i]);
  }
  printf("%lld\\n", Solve());
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2 * acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 100010;
const int MX = 123456;
const int mod = (int)1e9 + 7;
const int base = 97;
const ull BS1 = 10000019ULL;
const int INF = (1 << 29);

template <class T> inline void fastScan(T &x) {
  register char c = getchar();
  int neg = 0;
  x = 0;
  for (; (c < 48 || c > 57) && (c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}

// OrderedSet
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template < typename T > using ordered_set = tree < T, null_type, greater < T
// >, rb_tree_tag, tree_order_statistics_node_update >;

// typedef uint64_t hash_t;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// const int HASH_COUNT = 2;
// const hash_t HASH_P = (uint32_t)-13337;
// uniform_int_distribution<hash_t> MULT_DIST(0.25 * HASH_P, 0.75 * HASH_P);
// const hash_t HASH_MULT[HASH_COUNT] = {MULT_DIST(rng), MULT_DIST(rng)};

int n;

ll inp[N];
int dp[105][N];

ll f(ll hand, ll x, ll y) {
  ll how = hand / x;
  return (hand % x) + how * y;
}

ll go(int pos, ll hand) {
  if (pos > n) {
    return hand;
  }
  ll ret = go(pos + 1, hand);
  for (int j = pos + 1; j <= n; j++) {
    if (inp[j] > inp[pos]) {
      ret = max(ret, go(j + 1, f(hand, inp[pos], inp[j])));
    }
  }
  return dp[pos][hand] = ret;
}

int main() {
  fastScan(n);
  for (int i = 1; i <= n; i++) {
    fastScan(inp[i]);
  }
  printf("%lld\\n", go(1, 1000));
}