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

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

void yn(int o) {
  if (o == 0)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}

int main() {
  ll n;
  cin >> n;
  ll ans = 1000;
  ll kb = 0;
  vector<ll> ar(n + 2);
  rep(i, n) cin >> ar[i + 1];
  ar[0] = ar[2];
  ar[n + 1] = ar[n - 1];
  int s = 0;
  int g = n;
  for (ll i = n; i > 0; i--) {
    if (ar[i - 1] <= ar[i] && ar[i] >= ar[i + 1]) {
      g = i;
      break;
    }
  }
  repl(i, n) {
    if (ar[i] >= ar[i + 1] && ar[i + 1] <= ar[i + 2]) {
      s = i;
      break;
    }
  }
  if (s > g) {
    cout << ans << endl;
    return 0;
  }
  vector<ll> al;
  repp(i, g + 2, s) al.emplace_back(ar[i]);
  n = al.size() - 2;
  rep(i, n) {
    if (al[i] <= al[i + 1] && al[i + 1] >= al[i + 2]) {
      ans += kb * al[i + 1];
      kb = 0;
    }
    if (al[i] >= al[i + 1] && al[i + 1] <= al[i + 2]) {
      kb += ans / al[i + 1];
      ans %= al[i + 1];
    }
  }
  ans += kb * al[n];
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

void yn(int o) {
  if (o == 0)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}

int main() {
  int n;
  cin >> n;
  int ans = 1000;
  int kb = 0;
  vector<int> ar(n + 2);
  rep(i, n) cin >> ar[i + 1];
  ar[0] = ar[2];
  ar[n + 1] = ar[n - 1];
  int s = 0;
  int g = n;
  for (int i = n; i > 0; i--) {
    if (ar[i - 1] <= ar[i] && ar[i] >= ar[i + 1]) {
      g = i;
      break;
    }
  }
  rep(i, n) {
    if (ar[i] >= ar[i + 1] && ar[i + 1] <= ar[i + 2]) {
      s = i;
      break;
    }
  }
  if (s > g) {
    cout << ans << endl;
    return 0;
  }
  vector<int> all;
  repp(i, g + 2, s) all.emplace_back(ar[i]);
  n = all.size() - 2;
  vector<int> al;
  al.emplace_back(all[0]);
  rep(i, n) {
    if (all[i] != all[i + 1])
      al.emplace_back(all[i + 1]);
  }
  al.emplace_back(all[n + 1]);
  rep(i, n) {
    if (al[i] <= al[i + 1] && al[i + 1] >= al[i + 2]) {
      ans += kb * al[i + 1];
      kb = 0;
    }
    if (al[i] >= al[i + 1] && al[i + 1] <= al[i + 2]) {
      kb += ans / al[i + 1];
      ans %= al[i + 1];
    }
  }
  ans += kb * al[n];
  cout << ans << endl;
}