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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD = 1e9 + 7;
using P = pair<ll, ll>;
ll gcd(ll a, ll b) { // aとbの最大公約数を返す//計算量は0(log(max(a,b))
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { // aとbの最小公倍数を返す//計算量は0(log(max(a,b))
  return a * b / gcd(a, b);
}
ll mod_pow(ll x, ll y) { // x^yを返す//計算量は0(log(y))
  ll tmp = x, res = 1;
  while (y) {
    if (y % 2) {
      res = res * tmp % MOD;
    }
    y >>= 1;
    tmp = tmp * tmp % MOD;
  }
  return res;
}
ll nature(ll a) { // 絶対値を返す
  if (a >= 0) {
    return (a);
  } else {
    return (-1 * a);
  }
}
ll prime(ll a) { // 素数なら1を返す//計算量は0(sqrt(a))
  if (a == 2) {
    return 1;
  } else if (a % 2 == 0 || a == 1) {
    return 0;
  }
  for (ll i = 3; i * i <= a; i += 2) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  long long N, check = 0, maxA = 0;
  cin >> N;
  vector<long long> A(N);
  long long lasta = 0;
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
    maxA = max(maxA, A.at(i));
  }
  // cout<<A.size()<<endl;
  long long count;
  long long money;
  money = 1000;
  count = 0;
  long long minimum = A.at(0);
  for (long long i = 1; i < N; i++) {
    if (A.at(i) < minimum) {
      minimum = A.at(i);
    } else if (i < N - 1) {
      if (A.at(i + 1) < A.at(i) && A.at(i) > minimum) {
        count = money / minimum;
        money -= count * minimum;
        money += count * A.at(i);
        // cout<<count<<" "<<minimum<<" "<<A.at(i)<<endl;
        count = 0;
        minimum = 1000;
      }
    }
    if (i == N - 1) {
      if (A.at(i) > minimum) {
        count = money / minimum;
        money -= count * minimum;
        money += count * A.at(i);
        // cout<<count<<" "<<minimum<<" "<<A.at(i)<<endl;
        count = 0;
      }
    }
  }
  cout << money << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD = 1e9 + 7;
using P = pair<ll, ll>;
ll gcd(ll a, ll b) { // aとbの最大公約数を返す//計算量は0(log(max(a,b))
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { // aとbの最小公倍数を返す//計算量は0(log(max(a,b))
  return a * b / gcd(a, b);
}
ll mod_pow(ll x, ll y) { // x^yを返す//計算量は0(log(y))
  ll tmp = x, res = 1;
  while (y) {
    if (y % 2) {
      res = res * tmp % MOD;
    }
    y >>= 1;
    tmp = tmp * tmp % MOD;
  }
  return res;
}
ll nature(ll a) { // 絶対値を返す
  if (a >= 0) {
    return (a);
  } else {
    return (-1 * a);
  }
}
ll prime(ll a) { // 素数なら1を返す//計算量は0(sqrt(a))
  if (a == 2) {
    return 1;
  } else if (a % 2 == 0 || a == 1) {
    return 0;
  }
  for (ll i = 3; i * i <= a; i += 2) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  int N, count = 0, money = 1000, check = 0;
  cin >> N;
  vector<ll> A(0);
  int maxA = 0;
  queue<int> b;
  queue<int> s;
  int lasta = -1;
  for (ll i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a != lasta) {
      A.push_back(a);
      maxA = max(maxA, a);
      lasta = a;
    }
  }
  for (int i = 0; i < (int)A.size(); i++) {
    if (i > 0 && i < (int)A.size() - 1) {
      if (A.at(i) > A.at(i - 1) && A.at(i) > A.at(i + 1)) { // 極大
        b.push(i);
        if (check == 0) {
          s.push(0);
        }
        check = 1;
      }
      if (A.at(i) < A.at(i - 1) && A.at(i) < A.at(i + 1)) { // 極小
        s.push(i);
        if (check == 0) {
          b.push(0);
        }
        check = 1;
      }
    }
    if (i == (int)A.size() - 1) {
      if (A.at(i) > A.at(i - 1)) {
        b.push(i);
        s.push(i - 1);
      }
    }
  }
  ll M = A.at(0);
  for (int i = 1; i < N; i++) {
    /*
    if(A.at(i)>M){
      M=A.at(i);
      money+=count*M;
      count=0;
      cout<<i<<" "<<money<<endl;
      if(A.at(i)!=maxA){
        count+=money/A.at(i);
        money-=count*A.at(i);
      }
    }
    //cout<<i<<" "<<money<<endl;
    */
  }
  // cout<<money<<endl;
  while (!b.empty()) {
    ll B = b.front();
    b.pop();
    ll S, BB;
    // cout<<money<<" "<<count<<endl;
    // cout<<B<<endl;
    if (!s.empty()) {
      S = s.front();
      // cout<<S<<endl;
      s.pop();
      count = money / A.at(S);
      money += count * (A.at(B) - A.at(S));
      // cout<<count<<" "<<money<<endl;
      count = 0;
    } else if (!b.empty()) {
      while (!b.empty()) {
        BB = b.front();
        if (BB > B) {
          count += money / A.at(B);
          money -= count * A.at(B);
          money += count * A.at(BB);
          count = 0;
          break;
        }
      }
    }
    // cout<<money<<endl;
  }
  cout << money << endl;
}