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

#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke                                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
#define pb push_back
#define ld long double
#define int long long
#define print(t) cout << "Case #" << t << ": ";
int32_t main() {
  tani_nachi_ke

      int n,
      k;
  cin >> n >> k;
  int a[n];
  vector<int> pos, neg;
  int const mod = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(-a[i]);
  }

  int ans = 1;
  if (n == k) {
    for (int i = 0; i < n; i++) {
      ans = (ans * a[i]) % mod;
      ans += mod;
      ans %= mod;
      // cout << ans << endl;
    }
    cout << ans << '\\n';
  } else if (pos.size() == 0) {
    if (k % 2 == 1) {
      sort(neg.begin(), neg.end());
      for (int i = 0; i < k; i++) {
        ans = (ans * neg[i]) % mod;
        ans += mod;
        ans %= mod;
      }
      cout << (ans * -1 + mod) % mod << '\\n';
    } else {
      sort(neg.begin(), neg.end());
      for (int i = 0; i < k; i++) {
        ans = (ans * neg[n - 1 - i]) % mod;
        ans += mod;
        ans %= mod;
      }
      cout << ans << '\\n';
    }
  } else {
    sort(neg.begin(), neg.end(), greater<int>());
    sort(pos.begin(), pos.end(), greater<int>());
    if (k % 2 == 1) {
      // odd -> even (negs) + odd (pos)

      ans = (ans * pos[0]) % mod;
      vector<int> final;
      for (int i = 2; i < pos.size(); i += 2) {
        final.push_back(pos[i] * pos[i - 1]);
      }
      for (int i = 1; i < neg.size(); i += 2)
        final.push_back(neg[i] * neg[i - 1]);

      sort(final.begin(), final.end(), greater<int>());
      k--;
      for (int i = 0; i < k / 2; i++)
        ans = (ans * (final[i] % mod)) % mod;
      cout << ans << '\\n';
    } else {
      // even -> even (negs) + even (pos)
      vector<int> final;
      for (int i = 1; i < pos.size(); i += 2) {
        final.push_back(pos[i] * pos[i - 1]);
      }
      for (int i = 1; i < neg.size(); i += 2)
        final.push_back(neg[i] * neg[i - 1]);

      sort(final.begin(), final.end(), greater<int>());

      for (int i = 0; i < k / 2; i++)
        ans = (ans * (final[i] % mod)) % mod;
      cout << ans << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke                                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
#define pb push_back
#define ld long double
#define int long long
#define print(t) cout << "Case #" << t << ": ";
int32_t main() {
  tani_nachi_ke

      int n,
      k;
  cin >> n >> k;
  vector<int> pos, neg;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a > 0)
      pos.push_back(a);
    else
      neg.push_back(a);
  }
  int ans = 1;
  int mod = 1e9 + 7;
  if (k > neg.size() + pos.size()) {
    cout << 0 << '\\n';
    return 0;
  }
  if (k > pos.size() && (k - pos.size()) % 2 == 1) {
    // negative
    vector<int> final;
    for (int u : pos)
      final.push_back(u);
    for (int u : neg)
      final.push_back(-u);
    sort(final.begin(), final.end());
    int i = 0;
    while (k--) {
      ans = (ans * final[i]) % mod;
      i++;
    }
    cout << ((ans * -1) % mod + mod) % mod << endl;
  } else {
    // positive
    sort(pos.begin(), pos.end(), greater<int>());
    sort(neg.begin(), neg.end());
    int i = 1, j = 1;
    while (i < pos.size() && j < neg.size() && k > 1) {
      if (pos[i] * pos[i - 1] > neg[j] * neg[j - 1]) {
        ans = (ans * (pos[i] * pos[i - 1]) % mod) % mod;
        i += 2;
        k -= 2;
      } else {
        ans = (ans * (neg[j] * neg[j - 1]) % mod) % mod;
        j += 2;
        k -= 2;
      }
    }
    while (k--) {
      ans = (ans * pos[i - 1]) % mod;
      i++;
    }
    cout << ans << '\\n';
  }

  return 0;
}