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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n;
  cin >> n;
  vecint a(n, 0);
  REPe(e, a) cin >> e;
  ll x = 0;
  ll y = 1000;
  REP(i, n - 1) {
    if (a[i] < a[i + 1]) {
      ll k = y / a[i];
      y -= k * a[i];
      y += k * a[i + 1];
    }
  }
  cout << y << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n;
  cin >> n;
  vecint b(n, 0);
  REPe(e, b) cin >> e;
  vecint a;
  FOR(i, 1, n) {
    if (a.back() >= b[i]) {
      a.back() = b[i];
    } else {
      a.push_back(b[i]);
    }
  }
  // REPe(e, a) { cout << e << endl; }
  int x = 0;
  int y = 1000;
  int m = a.size();
  REP(i, m - 1) {
    if (a[i] < a[i + 1]) {
      int k = y / a[i];
      x += k;
      y -= a[i] * k;
      x = 0;
      y += a[i + 1] * k;
    } else {
    }
  }
  cout << y << endl;
}