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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll num = 0;
  ll yen = 1000;

  // vector<int> maxs, mins;
  // for(int i=0; i<N-1; i++){
  // 	if(A[i] < A[i+1]) mins.push_back(i);
  // 	else if(A[i] > A[i+1]) maxs.push_back(i);
  // }
  // if(A[N-2]<A[N-1]) maxs.push_back(N-1);
  // if(A[N-2]>A[N-1]) mins.push_back(N-1);

  // reverse(mins.begin(), mins.end());
  // reverse(maxs.begin(), maxs.end());
  // while(true){
  // 	if(mins.back() < maxs.back()){
  // 		num += yen / A[mins.back()];
  // 		yen -= yen / A[mins.back()] * A[mins.back()];
  // 		yen += A[maxs.back()] * num;
  // 		num = 0;
  // 		mins.pop_back();
  // 		maxs.pop_back();
  // 	}
  // 	else maxs.pop_back();
  // 	if((int)maxs.size() == 0) break;
  // }
  // cout << yen << endl;
  // return 0;

  // vector<int> is_up(N, 0);//0: 無視, 1:前日より上がった、 -1:
  // 前日より下がった for(int i=0; i<N; i++){ 	if(A)
  // }
  if (A[0] <= A[1]) { // 明日上がるのでとりあえず買う
    num = yen / A[0];
    yen -= yen / A[0] * A[0];
  }
  for (int i = 1; i < N - 1; i++) {
    if (A[i] >= A[i - 1]) { // 前日より上がった　ので売る
      yen += num * A[i];
      num = 0;
    }
    if (A[i] < A[i - 1]) { // 前日より下がった ので買う
      num += yen / A[i];
      yen -= yen / A[i] * A[i];
    }
    if (A[i] >= A[i + 1]) { // 明日下がる ので　売る
      yen += num * A[i];
      num = 0;
    }
    if (A[i] < A[i + 1]) { // 明日上がる ので買う
      num += yen / A[i];
      yen -= yen / A[i] * A[i];
    }

    // cout << " i : "<< i << " yen: " << yen << " num : " << num << endl;
  }
  yen += num * A.back();
  cout << yen << endl;

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> maxs, mins;
  for (int i = 0; i < N - 1; i++) {
    if (A[i] < A[i + 1])
      mins.push_back(i);
    else if (A[i] > A[i + 1])
      maxs.push_back(i);
  }
  if (A[N - 2] < A[N - 1])
    maxs.push_back(N - 1);
  if (A[N - 2] > A[N - 1])
    mins.push_back(N - 1);

  int num = 0;
  int yen = 1000;
  reverse(mins.begin(), mins.end());
  reverse(maxs.begin(), maxs.end());
  while (true) {
    if (mins.back() < maxs.back()) {
      num += yen / A[mins.back()];
      yen -= yen / A[mins.back()] * A[mins.back()];
      yen += A[maxs.back()] * num;
      num = 0;
      mins.pop_back();
      maxs.pop_back();
    } else
      maxs.pop_back();
    if ((int)maxs.size() == 0)
      break;
  }
  cout << yen << endl;
  return 0;

  // vector<int> is_up(N, 0);//0: 無視, 1:前日より上がった、 -1:
  // 前日より下がった for(int i=0; i<N; i++){ 	if(A)
  // }
  if (A[0] <= A[1]) { // 明日上がるのでとりあえず買う
    num = yen / A[0];
    yen -= yen / A[0] * A[0];
  }
  for (int i = 1; i < N - 1; i++) {
    if (A[i] >= A[i - 1]) { // 前日より上がった　ので売る
      yen += num * A[i];
      num = 0;
    }
    if (A[i] < A[i - 1]) { // 前日より下がった ので買う
      num += yen / A[i];
      yen -= yen / A[i] * A[i];
    }
    if (A[i] >= A[i + 1]) { // 明日下がる ので　売る
      yen += num * A[i];
      num = 0;
    }
    if (A[i] < A[i + 1]) { // 明日上がる ので買う
      num += yen / A[i];
      yen -= yen / A[i] * A[i];
    }

    cout << " i : " << i << " yen: " << yen << " num : " << num << endl;
  }
  yen += num * A.back();
  cout << yen << endl;

  return 0;
}