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

#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 20000000000000000
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005

ll a[ma], n;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll k, i, j, x, y, z;
  while (cin >> n) {
    for (i = 1; i <= n; i++)
      cin >> a[i];
    ll ans = 1000;

    for (i = 1; i <= n; i++) {
      ll x = a[i];
      ll y = -1;
      for (j = i + 1; j <= n; j++) {
        if (a[j] > a[j - 1]) {
          y = a[j];
          continue;
        }
        break;
      }
      i = j - 1;
      if (y == -1)
        continue;
      ll p = (ans / x);
      ans = ans + (y - x) * p;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 20000000000000000
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005

ll a[ma], n;

ll dp[85][100005];

ll cal(ll x, ll y) {
  if (x > n)
    return y;
  ll &r = dp[x][y];
  if (r != -1)
    return r;
  r = cal(x + 1, y);
  ll p = y / a[x];
  for (ll i = x + 1; i <= y; i++) {
    if (a[i] <= a[x])
      continue;
    ll q = (a[i] - a[x]) * p;
    ll r1 = cal(i, y + q);
    r = max(r, r1);
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll k, i, j, x, y, z;
  while (cin >> n) {
    for (i = 1; i <= n; i++)
      cin >> a[i];
    memset(dp, -1, sizeof(dp));
    ll ans = cal(1, 1000);
    cout << ans << endl;
  }
  return 0;
}