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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;

#define INF 1e+9
#define MAX_V 300

struct edge {
  int to;
  int cost;
};
using P = pair<ll, ll>;
ll gcd(ll x, ll y) {
  if (x < y) {
    swap(x, y);
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}
int main() {
  ll n;
  cin >> n;
  VL a(n);
  REP(i, n) { cin >> a[i]; }
  // 買いシグナル1売りシグナリ-1
  VL b(n);
  for (int i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) {
      b[i] = 1;
    }
    if (a[i] > a[i + 1]) {
      b[i] = -1;
    }
    if (a[i] == a[i + 1]) {
      b[i] = 0;
    }
  }
  b[n - 1] = -1;
  ll moti = 1000;
  ll kabu = 0;
  for (int i = 0; i < n; i++) {
    if (b[i] == 1) {
      moti = moti + kabu * a[i];
      kabu = 0;
      kabu = moti / a[i];
      moti = moti % a[i];
    }
    if (b[i] == -1) {
      moti = moti + kabu * a[i];
      kabu = 0;
    }
    // cout<<moti<<" "<<kabu<<endl;
    if (b[i] == 0) {
      continue;
    }
  }
  cout << moti << endl;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;

#define INF 1e+9
#define MAX_V 300

struct edge {
  int to;
  int cost;
};
using P = pair<ll, ll>;
ll gcd(ll x, ll y) {
  if (x < y) {
    swap(x, y);
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}
int main() {
  ll n;
  cin >> n;
  VL a(n);
  REP(i, n) { cin >> a[i]; }
  // 買いシグナル1売りシグナリ-1
  VL b(n);
  int flag = 1;
  int end = 0;
  for (int i = n; i >= 1; i--) {
    if (flag == 1) {
      if (a[i] <= a[i - 1]) {
        b[i] = 0;
      } else {
        b[i] = -1;
        flag = 0;
        end = i;
        break;
      }
    }
  }
  for (int i = 0; i < end; i++) {
    if (a[i] < a[i + 1]) {
      b[i] = 1;
    }
    if (a[i] > a[i + 1]) {
      b[i] = -1;
    }
    if (a[i] == a[i + 1]) {
      b[i] = 0;
    }
  }
  b[n - 1] = -1;
  ll moti = 1000;
  ll kabu = 0;
  for (int i = 0; i < n; i++) {
    if (b[i] == 1) {
      kabu = moti / a[i];
      moti = moti % a[i];
    }
    if (b[i] == -1) {
      moti = moti + kabu * a[i];
      kabu = 0;
    }
    if (b[i] == 0) {
      continue;
    }
  }
  cout << moti << endl;
}