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

/**
 * @author yuzining
 **/
#ifdef local
#include "D:\\vscode\\acm\\local.hpp"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
void Main();
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  Main();
  return 0;
}
#endif

const int MOD = 1e9 + 7;

void Main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  bool havePositive = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      havePositive = true;
  }
  if (n == k) {
    int ans = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] < 0)
        a[i] += MOD;
      ans = 1ll * ans * a[i] % MOD;
    }
    cout << ans << '\\n';
    return;
  }
  sort(a.begin(), a.end(),
       [&](const int &x, const int &y) { return abs(x) > abs(y); });
  if ((k & 1) && !havePositive) {
    int ans = 1;
    reverse(a.begin(), a.end());
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * (MOD + a[i]) % MOD;
    }
    cout << ans << '\\n';
    return;
  }
  int ans = 1;
  if (k & 1) {
    for (int i = 0; i < n; i++) {
      if (a[i] > 0) {
        ans = 1ll * ans * a[i] % MOD;
        a.erase(a.begin() + i);
        n--, k--;
        break;
      }
    }
  }
  vector<long long> tmp;
  int t1 = INT_MIN, t2 = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0) {
      if (t1 == INT_MIN) {
        t1 = a[i];
      } else {
        tmp.push_back(1ll * t1 * a[i]);
        t1 = INT_MIN;
      }
    } else if (a[i] > 0) {
      if (t2 == INT_MIN) {
        t2 = a[i];
      } else {
        tmp.push_back(1ll * t2 * a[i]);
        t2 = INT_MIN;
      }
    }
  }
  k >>= 1;
  if (k <= int(tmp.size())) {
    sort(tmp.begin(), tmp.end());
    reverse(tmp.begin(), tmp.end());
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * (tmp[i] % MOD) % MOD;
    }
    cout << ans << '\\n';
  } else {
    cout << 0 << '\\n';
  }
}

Error: 0

Fixed submission:

/**
 * @author yuzining
 **/
#ifdef local
#include "D:\\vscode\\acm\\local.hpp"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
void Main();
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  Main();
  return 0;
}
#endif

const int MOD = 1e9 + 7;

void Main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  bool havePositive = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      havePositive = true;
  }
  if (n == k) {
    int ans = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] < 0)
        a[i] += MOD;
      ans = 1ll * ans * a[i] % MOD;
    }
    cout << ans << '\\n';
    return;
  }
  sort(a.begin(), a.end(),
       [&](const int &x, const int &y) { return abs(x) > abs(y); });
  if ((k & 1) && !havePositive) {
    int ans = 1;
    reverse(a.begin(), a.end());
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * (MOD + a[i]) % MOD;
    }
    cout << ans << '\\n';
    return;
  }
  int ans = 1;
  if (k & 1) {
    for (int i = 0; i < n; i++) {
      if (a[i] > 0) {
        ans = 1ll * ans * a[i] % MOD;
        a.erase(a.begin() + i);
        n--, k--;
        break;
      }
    }
  }
  vector<long long> tmp;
  int t1 = INT_MIN, t2 = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0) {
      if (t1 == INT_MIN) {
        t1 = a[i];
      } else {
        tmp.push_back(1ll * t1 * a[i]);
        t1 = INT_MIN;
      }
    } else if (a[i] > 0) {
      if (t2 == INT_MIN) {
        t2 = a[i];
      } else {
        tmp.push_back(1ll * t2 * a[i]);
        t2 = INT_MIN;
      }
    }
  }
  k >>= 1;
  sort(tmp.begin(), tmp.end());
  reverse(tmp.begin(), tmp.end());
  for (int i = 0; i < k; i++) {
    ans = 1ll * ans * (tmp[i] % MOD) % MOD;
  }
  cout << ans << '\\n';
}