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

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define LOCAL 1

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(...)                                                               \\
  cout << #__VA_ARGS__ ": [";                                                  \\
  for (auto e : __VA_ARGS__) {                                                 \\
    cout << e << ", ";                                                         \\
  }                                                                            \\
  cout << "]\\n"
#define dbg1(...) cout << #__VA_ARGS__ " " << __VA_ARGS__
#else
#define dbg(...) 1;
#define dbg1(...) 1;
#endif

typedef long long ll;
typedef unsigned long long ull;
constexpr int R = 5e5 + 69, INF = 2e9 + 6969;

ll n, T[R];
vector<ll> v, t;

int main() {
  cin >> n;
  for (ll i = 0; i < n; ++i)
    cin >> T[i];
  t.pb(T[0]);
  for (ll i = 1; i < n; ++i)
    if (T[i] != T[i - 1])
      t.pb(T[i]);
  v.pb(T[0]);
  for (ll i = 1; i < t.size() - 1; ++i) {
    if (t[i] > t[i - 1] && t[i] > t[i + 1])
      v.pb(t[i]);
    if (t[i] < t[i - 1] && t[i] < t[i + 1])
      v.pb(t[i]);
  }
  v.pb(t[t.size() - 1]);
  ll ans = 1000;
  ll st = 0;
  for (ll i = 0; i < v.size() - 1; ++i) {
    //  prllf("%d ", v[i]);
    if (v[i] < v[i + 1]) {
      ll cur = ans / v[i];
      st = cur;
      ans %= v[i];
    } else if (v[i] > v[i + 1]) {
      ll cur = st * v[i];
      ans += cur;
      st = 0;
    }
  }
  if (st > 0) {
    ans += st * v[v.size() - 1];
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define LOCAL 1

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(...)                                                               \\
  cout << #__VA_ARGS__ ": [";                                                  \\
  for (auto e : __VA_ARGS__) {                                                 \\
    cout << e << ", ";                                                         \\
  }                                                                            \\
  cout << "]\\n"
#define dbg1(...) cout << #__VA_ARGS__ " " << __VA_ARGS__
#else
#define dbg(...) 1;
#define dbg1(...) 1;
#endif

typedef long long ll;
typedef unsigned long long ull;
constexpr int R = 5e5 + 69, INF = 2e9 + 6969;

int n, T[R];
vector<int> v, t;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> T[i];
  t.pb(T[0]);
  for (int i = 1; i < n; ++i)
    if (T[i] != T[i - 1])
      t.pb(T[i]);
  v.pb(T[0]);
  for (int i = 1; i < n - 1; ++i) {
    if (t[i] > t[i - 1] && t[i] > t[i + 1])
      v.pb(t[i]);
    if (t[i] < t[i - 1] && t[i] < t[i + 1])
      v.pb(t[i]);
  }
  v.pb(t[n - 1]);
  int ans = 1000;
  int st = 0;
  for (int i = 0; i < v.size() - 1; ++i) {
    //  printf("%d ", v[i]);
    if (v[i] < v[i + 1]) {
      int cur = ans / v[i];
      st += cur;
      ans -= cur * v[i];
    } else if (v[i] > v[i + 1]) {
      int cur = st * v[i];
      ans += cur;
      st = 0;
    }
  }
  if (st > 0) {
    ans += st * v[v.size() - 1];
  }
  printf("%d\\n", ans);
  return 0;
}

//  7
//  100 130 130 130 115 115 150

//  ans: 1685