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

#include <bits/stdc++.h>
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define LL long long int
using namespace std;

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> neg, nneg;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x < 0)
      neg.push_back(x);
    else
      nneg.push_back(x);
  }
  sort(neg.begin(), neg.end());
  reverse(neg.begin(), neg.end());
  sort(nneg.begin(), nneg.end());
  if (nneg.empty()) {
    if (!(k & 1)) {
      reverse(neg.begin(), neg.end());
    }
    LL prod = 1;
    for (int i = 0; i < k; ++i) {
      prod = (prod * neg[i]) % MOD;
    }
    if (prod < 0)
      prod += MOD;
    cout << prod << '\\n';
    return;
  }
  if (n == k) {
    LL prod = 1;
    for (auto it : neg) {
      prod = (prod * it) % MOD;
    }
    for (auto it : nneg) {
      prod = (prod * it) % MOD;
    }
    if (prod < 0)
      prod += MOD;
    cout << prod << '\\n';
    return;
  }
  LL prod = 1;
  if (k & 1) {
    prod = (prod * nneg.back()) % MOD;
    nneg.pop_back();
    k--;
  }
  int psz = nneg.size(), nsz = neg.size();
  while (k > 0) {
    if (psz > 1 && nsz > 1) {
      LL x = (neg[nsz - 1] * 1LL * neg[nsz - 2]);
      LL y = (nneg[psz - 1] * 1LL * nneg[psz - 2]);
      if (x > y) {
        neg.pop_back();
        neg.pop_back();
        prod = (prod * (x % MOD)) % MOD;
        nsz -= 2;
        k -= 2;
      } else {
        nneg.pop_back();
        nneg.pop_back();
        prod = (prod * (y % MOD)) % MOD;
        psz -= 2;
        k -= 2;
      }
      continue;
    }
    if (nsz > 1) {
      LL x = (neg[nsz - 1] * 1LL * neg[nsz - 2]);
      neg.pop_back();
      neg.pop_back();
      prod = (prod * (x % MOD)) % MOD;
      nsz -= 2;
      k -= 2;
      continue;
    }
    if (psz > 1) {
      LL y = (nneg[psz - 1] * 1LL * nneg[psz - 2]);
      nneg.pop_back();
      nneg.pop_back();
      prod = (prod * (y % MOD)) % MOD;
      psz -= 2;
      k -= 2;
      continue;
    }
    assert(false);
  }
  if (prod < 0)
    prod += MOD;
  cout << prod << '\\n';
}

int main() {
  FASTIO;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define LL long long int
using namespace std;

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
int v[N];
void solve() {
  int n, k, z = 0;
  cin >> n >> k;
  vector<int> pos, neg;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(x);
    else
      z++;
  }
  int psz = pos.size(), nsz = neg.size();
  if (psz + nsz < k) {
    cout << "0\\n";
  }
  if (psz + nsz == k) {
    LL pmod = 1;
    for (auto it : pos) {
      pmod *= it;
      pmod %= MOD;
    }
    for (auto it : neg) {
      pmod *= it;
      pmod %= MOD;
    }
    if (pmod < 0)
      pmod += MOD;
    if (nsz & 1) {
      if (z > 0) {
        cout << "0\\n";
      } else {
        cout << pmod << '\\n';
      }
    } else {
      cout << pmod << '\\n';
    }
    return;
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  reverse(neg.begin(), neg.end());
  if (psz == 0) {
    if (k & 1) {
      if (z > 0) {
        cout << "0\\n";
      } else {
        LL pmod = 1;
        for (int i = 0; i < k; ++i) {
          int it = neg[i];
          pmod *= it;
          pmod %= MOD;
          if (pmod < 0)
            pmod += MOD;
        }
        cout << pmod << '\\n';
      }
    } else {
      LL pmod = 1;
      for (int i = nsz - 1; i >= nsz - k; --i) {
        int it = neg[i];
        pmod *= it;
        pmod %= MOD;
        if (pmod < 0)
          pmod += MOD;
      }
      cout << pmod << '\\n';
    }
    return;
  }
  int need = k;
  LL ans = 1;
  while (need > 0) {
    if (psz > 1 && nsz > 1 && need > 1) {
      LL x = pos[psz - 1] * pos[psz - 2];
      LL y = neg[nsz - 1] * neg[nsz - 2];
      if (x > y) {
        ans *= x;
        ans %= MOD;
        pos.pop_back();
        pos.pop_back();
        need -= 2;
        psz -= 2;
      } else {
        ans *= y;
        ans %= MOD;
        neg.pop_back();
        neg.pop_back();
        need -= 2;
        nsz -= 2;
      }
      continue;
    }
    if (psz == 1 && (need & 1)) {
      ans *= pos.back();
      pos.pop_back();
      need--;
      psz--;
      continue;
    }
    if (psz == 0) {
      assert(need > 1);
      ans *= neg.back();
      ans %= MOD;
      neg.pop_back();
      ans *= neg.back();
      ans %= MOD;
      neg.pop_back();
      nsz -= 2;
      need -= 2;
      continue;
    }
    if (nsz <= 1) {
      ans *= pos.back();
      ans %= MOD;
      pos.pop_back();
      need--;
      psz--;
    }
  }
  cout << ans << '\\n';
}

int main() {
  FASTIO;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}