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

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

ll modval(ll num) { return (num % MODV + MODV) % MODV; }

int main() {
  Init();
  ll n, k, ans = 1, zcnt = 0;
  cin >> n >> k;
  vector<ll> a(n), pos, neg, zero;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else if (a[i] < 0)
      neg.push_back(a[i]);
    if (a[i] == 0)
      zcnt++;
  }

  ll pcnt = pos.size() - zcnt;
  ll ncnt = neg.size();

  // nとkが同じなら全部掛け合わせるしか無い
  if (n == k)
    rep(i, n) ans = (ans * a[i]) % MODV;
  // k個抜き出すと絶対0を含んでしまう場合
  else if (pcnt + ncnt < k)
    ans = 0;
  // kが1なら最大値を抜き出すだけ
  else if (k == 1) {
    sort(all(a), greater<ll>());
    ans = a[0];
  }
  // kが奇数で、正の値が1つも無い場合は負の値（or 0）となる
  else if (k % 2 == 1 && pcnt == 0) {
    sort(all(a), greater<ll>());
    rep(i, k) ans = (ans * a[i]) % MODV;
  }
  // それ以外は絶対正にできる
  else {
    sort(all(pos), greater<ll>());
    sort(all(neg));
    ll ppos = 0, npos = 0;
    // 奇数だったら正の数の最大値を1個かけておく
    if (k % 2 == 1) {
      ans *= pos[ppos];
      ppos++;
      k--;
    }
    vector<ll> mulpair;
    if (pos.size() - ppos >= 2)
      for (ppos; ppos < pos.size() - 1; ppos += 2)
        mulpair.push_back(pos[ppos] * pos[ppos + 1]);
    if (neg.size() >= 2)
      for (npos; npos < neg.size() - 1; npos += 2)
        mulpair.push_back(neg[npos] * neg[npos + 1]);
    sort(all(mulpair), greater<ll>());
    k /= 2;
    rep(i, k) { ans = ans * (mulpair[i] % MODV) % MODV; }
  }
  cout << modval(ans) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

ll modval(ll num) { return (num % MODV + MODV) % MODV; }

int main() {
  Init();
  ll n, k, ans = 1, zcnt = 0;
  cin >> n >> k;
  vector<ll> a(n), pos, neg, zero;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else if (a[i] < 0)
      neg.push_back(a[i]);
    if (a[i] == 0)
      zcnt++;
  }

  ll pcnt = pos.size() - zcnt;
  ll ncnt = neg.size();

  // nとkが同じなら全部掛け合わせるしか無い
  if (n == k)
    rep(i, n) ans = (ans * a[i]) % MODV;
  // k個抜き出すと絶対0を含んでしまう場合
  else if (pcnt + ncnt < k)
    ans = 0;
  // kが1なら最大値を抜き出すだけ
  else if (k == 1) {
    sort(all(a), greater<ll>());
    ans = a[0];
  }
  // kが奇数で、正の値が1つも無い場合は負の値（or 0）となる
  else if (k % 2 == 1 && pcnt == 0) {
    sort(all(a), greater<ll>());
    rep(i, k) ans = (ans * a[i]) % MODV;
  }
  // それ以外は絶対正にできる
  else {
    sort(all(pos), greater<ll>());
    sort(all(neg));
    ll ppos = 0, npos = 0;
    // 奇数だったら正の数の最大値を1個かけておく
    if (k % 2 == 1) {
      ans *= pos[ppos];
      ppos++;
      k--;
    }
    vector<ll> mulpair;
    for (ppos; ppos < pos.size() - 1; ppos += 2)
      mulpair.push_back(pos[ppos] * pos[ppos + 1]);
    for (npos; npos < neg.size() - 1; npos += 2)
      mulpair.push_back(neg[npos] * neg[npos + 1]);
    sort(all(mulpair), greater<ll>());
    k /= 2;
    rep(i, k) { ans = ans * (mulpair[i] % MODV) % MODV; }
  }
  cout << modval(ans) << endl;
}