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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
// constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  /*
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  */

  ll n, k;
  scanf("%lld %lld", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  vector<ll> vmn, vpl;
  ll z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      z++;
    else if (a[i] > 0)
      vpl.emplace_back(a[i]);
    else
      vmn.emplace_back(llabs(a[i]));
  }

  bool ok = false;
  if (k <= vpl.size())
    ok = true;
  else {
    for (ll i = 0; i <= vpl.size(); i++) {
      if ((k - i) % 2 == 1)
        continue;
      ll r = k - i;
      if (r <= vmn.size()) {
        ok = true;
        break;
      }
    }
  }
  if (!ok) {
    if (z > 0)
      puts("0");
    else {
      vector<ll> v;
      for (int i = 0; i < n; i++) {
        v.emplace_back(llabs(a[i]));
      }
      sort(v.begin(), v.end());
      mint res = 1;
      for (int i = 0; i < k; i++)
        res *= v[i];
      res *= -1;
      cout << res.x << "\\n";
    }
    return 0;
  }

  sort(vmn.rbegin(), vmn.rend());
  sort(vpl.rbegin(), vpl.rend());

  mint res = 1;
  int ppos, mpos;
  ppos = mpos = 0;
  if (k & 1) {
    k--;
    res *= vpl[ppos++];
  }
  while (k > 0) {
    if (ppos + 1 < vpl.size() and mpos + 1 < vmn.size()) {
      ll val_pl = vpl[ppos] * vpl[ppos + 1];
      ll val_mn = vmn[mpos] * vmn[mpos + 1];
      if (val_mn > val_pl) {
        res *= val_mn % 1000000007;
        mpos += 2;
      } else {
        res *= val_pl % 1000000007;
        ppos += 2;
      }
    } else if (ppos + 1 < vpl.size()) {
      res *= vpl[ppos++];
      res *= vpl[ppos++];
    } else {
      res *= vmn[mpos++];
      res *= vmn[mpos++];
    }
    k -= 2;
  }
  cout << res.x << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
#define pvec(vec)                                                              \\
  {                                                                            \\
    for (auto v : vec)                                                         \\
      cout << v << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define pivec(vec)                                                             \\
  {                                                                            \\
    rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' ';                   \\
    cout << endl;                                                              \\
  }

using namespace std;
using ll = long long;

const ll MOD = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;

  if (N == K) {
    ll ret = 1;
    rep(i, 0, N) {
      ll A;
      cin >> A;
      ret *= A;
      ret %= MOD;
    }
    cout << ret << endl;
    return 0;
  }

  vector<ll> plus, minus;
  rep(i, 0, N) {
    ll A;
    cin >> A;
    if (A >= 0)
      plus.push_back(A);
    else
      minus.push_back(A);
  }

  bool is_ok = false;
  rep(i, 0, plus.size() + 1) {
    int r = K - i;
    if (r & 1)
      continue;
    if (r < minus.size())
      is_ok = true;
  }

  if (!is_ok) {
    vector<ll> all_nums(minus);
    sort(all_nums.rbegin(), all_nums.rend());

    ll ret = 1;
    rep(i, 0, K) {
      ret *= all_nums[i];
      ret %= MOD;
    }
    if (ret < 0)
      ret += MOD;
    cout << ret << endl;
    return 0;
  }

  // answer is non-negative
  ll ret = 1;
  ll cnt = K;
  ll ppos, mpos;
  ppos = mpos = 0;

  sort(plus.rbegin(), plus.rend());
  sort(minus.begin(), minus.end());

  if ((cnt & 1) and plus.size() > 0) {
    ret *= plus[ppos++];
    --cnt;
  }

  while (cnt > 0) {
    ll plus_mul = 0, minus_mul = 0;

    if (ppos + 1 < (int)plus.size())
      plus_mul = plus[ppos] * plus[ppos + 1];
    if (mpos + 1 < (int)minus.size())
      minus_mul = minus[mpos] * minus[mpos + 1];

    if (plus_mul >= minus_mul) {
      ret *= (plus_mul % MOD);
      ret %= MOD;
      ppos += 2;
    } else {
      ret *= (minus_mul % MOD);
      ret %= MOD;
      mpos += 2;
    }

    cnt -= 2;
  }

  cout << ret << endl;
  return 0;
}