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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF = 1e13 + 7;
const int INF = 1e9 + 7;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class Y> T GCD(T a, Y b) {
  if (a < b) {
    int c = a;
    a = b;
    b = c;
  }
  if (a % b == 0) {
    return b;
  }
  return GCD(b, a % b);
}
template <class T, class Y> T LCM(T a, Y b) { return (a * b) / GCD(a, b); }
void clear(queue<pair<int, int>> &q) {
  queue<pair<int, int>> empty;
  swap(q, empty);
} // queueの中身の型は適時変更を忘れない

using vi = vector<int>;
using vii = vector<vi>;

#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(), (a).end()
#define decimal(x) fixed << setprecision(x)

int main() {
  ll n, money = 1000, t = 0;
  cin >> n;
  vi a(n);
  vector<bool> b(n, false), c(n, false);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll m;
    REP(j, i, n - 1) {
      if (a[j] >= a[j + 1]) {
        m = j;
        break;
      }
    }
    i = max(i, m);
    b[m] = true;
    // cout<<m<<endl;
  }
  rep(i, n - 1) {
    if (a[i] < a[i + 1])
      c[i] = true;
  }
  rep(i, n) {
    if (b[i] && t != 0) {
      money += t * a[i];
      t = 0;
    } else {
      if (money >= a[i] && c[i]) {
        t = money / a[i];
        money -= t * a[i];
      }
    }
    if (i == n - 1) {
      money += t * a[i];
      t = 0;
    }
    // cout<<b[i]<<":"<<money<<":"<<t<<endl;
  }
  cout << money << endl;
  return 0;
}

// a=97,z=122

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF = 1e13 + 7;
const int INF = 1e9 + 7;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class Y> T GCD(T a, Y b) {
  if (a < b) {
    int c = a;
    a = b;
    b = c;
  }
  if (a % b == 0) {
    return b;
  }
  return GCD(b, a % b);
}
template <class T, class Y> T LCM(T a, Y b) { return (a * b) / GCD(a, b); }
void clear(queue<pair<int, int>> &q) {
  queue<pair<int, int>> empty;
  swap(q, empty);
} // queueの中身の型は適時変更を忘れない

using vi = vector<int>;
using vii = vector<vi>;

#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(), (a).end()
#define decimal(x) fixed << setprecision(x)

int main() {
  ll n, money = 1000, t = 0;
  cin >> n;
  vi a(n);
  vector<bool> b(n, false), c(n, false);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll m;
    REP(j, i, n - 1) {
      if (a[j] >= a[j + 1]) {
        m = j;
        break;
      }
    }
    i = m;
    b[m] = true;
    // cout<<m<<endl;
  }
  rep(i, n - 1) {
    if (a[i] < a[i + 1])
      c[i] = true;
  }
  rep(i, n) {
    if (b[i] && t != 0) {
      money += t * a[i];
      t = 0;
    } else {
      if (money >= a[i] && c[i]) {
        t = money / a[i];
        money -= t * a[i];
      }
    }
    // cout<<b[i]<<":"<<money<<":"<<t<<endl;
  }
  cout << money << endl;
  return 0;
}

// a=97,z=122