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

/*
ID: Aldew
LANG: C++14
PROG:
*/

#include <bits/stdc++.h>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ss second
#define ff first
#define pb push_back
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef unsigned long long ull;

int pct(int x) { return __builtin_popcount(x); }

using namespace std;

// ifstream fin("lamps.in");
// ofstream fout("lamps.out");
int val[82];
int stock[82];
int money[82];

int n;
vector<int> a;

string b[82];
int mx = 0;
int solve(int index, bool buy, bool sell) {
  // cout << "HERE" << endl;
  for (int i = index; i < n; i++) {
    if (i + 1 == n) {
      money[i] = money[i - 1] + stock[i - 1] * a[i];
      b[i] = "end";

      if (money[i] > mx) {
        for (int i = 0; i < n; i++) {
          cout << b[i] << endl;
        }
        mx = money[i];
      }

      if (b[0] == "buy" && b[1] == "sell" && b[3] == "hold" && b[4] == "buy") {
        cout << "----------" << endl;
        for (int i = 0; i < n; i++) {
          cout << b[i] << endl;
        }
        cout << money[i] << endl;
        cout << "-------------" << endl;
      }
      return money[i];
    } else if (buy && money[i - 1] > 0) {
      // cout << "HERE1" << endl;
      money[i] = (a[i] % money[i - 1]);
      stock[i] = (a[i] / money[i - 1]) + stock[i - 1];
      b[i] = "buy";
    } else if (sell) {
      // cout << "HERE2" << endl;
      money[i] = (stock[i - 1] * a[i]) + money[i - 1];
      stock[i] = 0;
      b[i] = "sell";
    } else {
      money[i] = money[i - 1];
      stock[i] = stock[i - 1];
      b[i] = "hold";
    }

    solve(index + 1, true, false);
    solve(index + 1, false, true);
    solve(index + 1, false, false);
    cout << "HERE " << endl;
    cout << b[0] << endl;
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

  for (auto &it : a)
    cin >> it;

  for (int i = k; i < n; i++) {
    if (a[i] > a[i - k])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  // on each day you either sell all or buy all or do nothings

  return 0;
}


Error: 0

Fixed submission:

/*
ID: Aldew
LANG: C++14
PROG:
*/

#include <bits/stdc++.h>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ss second
#define ff first
#define pb push_back
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef unsigned long long ull;

int pct(int x) { return __builtin_popcount(x); }

using namespace std;

// ifstream fin("lamps.in");
// ofstream fout("lamps.out");

const int MOD = 1000000000;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;

  int it;
  for (int i = 0; i < n; i++) {
    cin >> it;
    a.push_back(it);
  }
  ll p = 1;
  for (int i = 0; i < k; i++) {
    p *= a[i];
  }

  int temp = p;
  bool first = true;

  // cout << k << " " a.size()
  for (int i = k; i < a.size(); i++) {
    p /= (a[i - k] % MOD);
    // cout << "div" << a[i-k] << endl;
    p *= (a[i] % MOD);
    // cout << "mult " << a[i] << endl;

    // cout << "HERE" << endl;

    if (p > temp) {
      cout << "Yes" << endl;
      // cout << temp <<  " " << p << endl;
    } else
      cout << "No" << endl;
    // cout << p << endl;
    temp = p;

    if (i + 1 == n)
      break;
  }

  return 0;
}