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

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }
#define M 1000000007
using namespace std;
int main() {
  IOS ll n, k;
  cin >> n >> k;
  vl a(n);
  ll positive = 0, negative = 0, zero = 0;
  Fori(i, 0, n) {
    cin >> a[i];
    if (a[i] > 0) {
      positive++;
    } else if (a[i] == 0) {
      zero++;
    } else {
      negative++;
    }
  }
  sort(a.begin(), a.end());
  int i = 0, j = n - 1;
  vl b;
  ll temp = k;
  while (k--) {
    if (negative == n) {
      if (temp % 2) {
        b.push_back(a[j--]);
        continue;
      } else {
        b.push_back(a[i++]);
        continue;
      }
    } else if (positive == n) {
      b.push_back(a[j--]);
      continue;
    }
    if (k == 0) {
      b.push_back(a[j--]);
      continue;
    }
    // if(k == 2) {
    //     b.push_back(a[i++]);
    //     b.push_back(a[i++]);
    //     k--;
    //     continue;
    // }
    ll prod1 = 1, prod2 = 1;
    prod1 = a[j] * a[j - 1];
    prod2 = a[i] * a[i + 1];
    if (prod1 > prod2) {
      b.push_back(a[j--]);
      // b.push_back(a[j--]);
      // k--;
    } else {
      b.push_back(a[i++]);
      b.push_back(a[i++]);
      k--;
    }
  }
  sort(b.begin(), b.end());
  ll ans = 1;
  ll cond = b.size();
  for (ll i = 0; i < cond; i++) {
    ans *= b[i];
    ans = ans % M;
  }
  if (ans < 0) {
    ans = M + ans;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }
#define M 1000000007
using namespace std;
int main() {
  IOS ll n, k;
  cin >> n >> k;
  vl a(n);
  ll positive = 0, negative = 0, zero = 0;
  Fori(i, 0, n) {
    cin >> a[i];
    if (a[i] > 0) {
      positive++;
    } else if (a[i] == 0) {
      zero++;
    } else {
      negative++;
    }
  }
  sort(a.begin(), a.end());
  int i = 0, j = n - 1;
  vl b;
  ll temp = k;
  while (k--) {
    if (negative == n) {
      if (temp % 2) {
        b.push_back(a[j--]);
        continue;
      } else {
        b.push_back(a[i--]);
        continue;
      }
    } else if (positive == n) {
      b.push_back(a[j--]);
      continue;
    }
    if (k == 0) {
      b.push_back(a[j--]);
      continue;
    }
    // if(k == 2) {
    //     b.push_back(a[i++]);
    //     b.push_back(a[i++]);
    //     k--;
    //     continue;
    // }
    ll prod1 = 1, prod2 = 1;
    prod1 = a[j] * a[j - 1];
    prod2 = a[i] * a[i + 1];
    if (prod1 > prod2) {
      b.push_back(a[j--]);
      b.push_back(a[j--]);
      k--;
    } else {
      b.push_back(a[i++]);
      b.push_back(a[i++]);
      k--;
    }
  }
  sort(b.begin(), b.end());
  ll ans = 1;
  ll cond = b.size();
  for (ll i = 0; i < cond; i++) {
    ans *= b[i];
    ans = ans % M;
  }
  if (ans < 0) {
    ans = M + ans;
  }
  cout << ans << endl;
  return 0;
}