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

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

/*define*/
#define rep(i, n) for (int i = 0; i < n; ++i)
#define endl "\\n"
typedef long long ll;

const long long mod = 1e18;
const long long INF = 1e9;
int ci;

// dx[5] = {-1, 0, 1, 0, 0};
// dy[5] = {0, 1, 0, -1, 0};
// 1行目を辞書順で繰り返す
// for(int i=0; i<1<<n; ++i){}
// 2進数を入力
// for(int j=0; j<n; ++j) g[0][i] = (i>>j)&1==1;

/*unique文字の確認
    set<int> all;
    all.insert(a[i]);
    map<int, int> count;
    count[a[i++]]++;
    if(count[a[j++]]++==0) num++;
*/
// typedef pair<int, int> P;

// 配列
// vector<long long> v(max_n);
// vector<int> v[max_n];
// vector<vector<int>> v(max_n);
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q; 降順;
// priority_queue<pair<int, int>, vector<pair<int, int>>, less<>> q; 昇順;

// tips
// 最大値の最小化は２分探索
// printf("IMPOSSIBLE"\\n);
// printf("%d%c", opt[i][j], j+1==N? \\n : '');

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll x = 1000;
  rep(i, n - 1) {
    int s = a[i];
    int t = a[i + 1];
    if (s < t) {
      ll k = x / s;
      x %= s;
      x += k * t;
    }
  }
  cout << x << endl;
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

/*define*/
#define rep(i, n) for (int i = 0; i < n; ++i)
#define endl "\\n"
typedef long long ll;

const long long mod = 1e18;
const long long INF = 1e9;
int ci;

// dx[5] = {-1, 0, 1, 0, 0};
// dy[5] = {0, 1, 0, -1, 0};
// 1行目を辞書順で繰り返す
// for(int i=0; i<1<<n; ++i){}
// 2進数を入力
// for(int j=0; j<n; ++j) g[0][i] = (i>>j)&1==1;

/*unique文字の確認
    set<int> all;
    all.insert(a[i]);
    map<int, int> count;
    count[a[i++]]++;
    if(count[a[j++]]++==0) num++;
*/
// typedef pair<int, int> P;

// 配列
// vector<long long> v(max_n);
// vector<int> v[max_n];
// vector<vector<int>> v(max_n);
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q; 降順;
// priority_queue<pair<int, int>, vector<pair<int, int>>, less<>> q; 昇順;

// tips
// 最大値の最小化は２分探索
// printf("IMPOSSIBLE"\\n);
// printf("%d%c", opt[i][j], j+1==N? \\n : '');

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<int> yen(n + 1), kabu(n + 1);
  a[0] = 500;
  yen[0] = 1000;
  kabu[0] = 0;
  for (int i = 1; i < n + 1; ++i)
    cin >> a[i];
  int i = 0;
  while (i < n + 1) {
    if ((a[i] > a[i + 1]) & ((int)(yen[i] / a[i + 1]) > 0)) {
      while ((a[i] > a[i + 1]) & (i < n + 1)) {
        yen[i + 1] = yen[i];
        kabu[i + 1] = kabu[i];
        ++i;
      }
      kabu[i] = yen[i] / a[i];
      yen[i] = yen[i - 1] - kabu[i] * a[i];
    }
    if ((a[i] < a[i + 1]) & (kabu[i] > 0)) {
      while ((a[i] < a[i + 1]) & (i < n + 1)) {
        yen[i + 1] = yen[i];
        kabu[i + 1] = kabu[i];
        ++i;
      }
      yen[i] = yen[i - 1] + a[i] * kabu[i - 1];
      kabu[i] = 0;
    } else {
      yen[i + 1] = yen[i];
      kabu[i + 1] = kabu[i];
      ++i;
    }
  }
  cout << yen[n] + a[n] * kabu[n] << endl;
  cin >> ci;
}