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

#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, n) if (i == 0) cout << v[i];                                          \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << v[i][j] << " "; }                                      \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

//*---mint---*/
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint operator-() const { return mint(0) - *this; }
  mint operator~() const { return mint(1) / *this; }
  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) {
    (x *= a.pow(mod - 2).x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint res = pow(t / 2);
    res *= res;
    return (t & 1) ? res * x : res;
  }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
mint ex(mint x, ll t) { return x.pow(t); }
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
struct comb {
  vm f, g;
  comb() {}
  comb(int mx) : f(mx + 1), g(mx + 1) {
    f[0] = 1;
    drep(i, 1, mx) f[i] = f[i - 1] * i;
    g[mx] = f[mx].pow(mod - 2);
    for (int i = mx; i > 0; i--)
      g[i - 1] = g[i] * i;
  }
  mint c(int a, int b) {
    if (a < b)
      return 0;
    return f[a] * g[b] * g[a - b];
  }
} c(3e6);
// c.c(a,b)=>aCb mod

int main() {
  int n, k;
  cin >> n >> k;
  mint ans = 1;
  vector<ll> a(n), ap, am, az;
  rep(i, n) cin >> a[i];
  int apn = 0, amn = 0, azn = 0;
  rep(i, n) {
    if (a[i] > 0) {
      ap.push_back(a[i]);
      apn++;
    }
    if (a[i] < 0) {
      am.push_back(a[i]);
      amn++;
    }
    if (a[i] == 0) {
      az.push_back(a[i]);
      azn++;
    }
  }
  SORT(ap);
  REV(ap);
  SORT(am);
  int pi = 0, mi = 0;
  if (k > 2 * (apn / 2 + amn / 2)) {
    if (k - 2 * (apn / 2 + amn / 2) == 1 && apn % 2 == 1) {
      ans = ap[0];
      k--;
      pi++;
    } else if (k - 2 * (apn / 2 + amn / 2) == 1 && amn % 2 == 1) {
      REV(am);
      REV(ap);
      ans = am[0];
      k--;
      mi++;
    } else if (azn > 0) {
      cout << 0 << endl;
      return 0;
    } else {
      REV(am);
      REV(ap);
      ans = ap[0] * am[0];
      k -= 2;
      pi++;
      mi++;
    }
  } else if (k % 2 == 1) {
    if (apn > 0) {
      ans = ap[0];
      k--;
      pi++;
    } else if (azn > 0) {
      ans = 0;
      k--;
    } else {
      REV(am);
      ans = am[0];
      k--;
      mi++;
    }
  }
  while (k) {
    ll p2, m2;
    if (pi + 1 > apn - 1)
      p2 = -1;
    else
      p2 = ap[pi] * ap[pi + 1];
    if (mi + 1 > amn - 1)
      m2 = -1;
    else
      m2 = am[mi] * am[mi + 1];
    // cout << p2 << " " << m2 << endl;
    if (p2 > m2) {
      ans *= p2;
      pi += 2;
    } else {
      ans *= m2;
      mi += 2;
    }
    k -= 2;
  }

  cout << ans << endl;
  //  printf("%.10f\\n",ans);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, n) if (i == 0) cout << v[i];                                          \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << v[i][j] << " "; }                                      \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

//*---mint---*/
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint operator-() const { return mint(0) - *this; }
  mint operator~() const { return mint(1) / *this; }
  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) {
    (x *= a.pow(mod - 2).x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint res = pow(t / 2);
    res *= res;
    return (t & 1) ? res * x : res;
  }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
mint ex(mint x, ll t) { return x.pow(t); }
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
struct comb {
  vm f, g;
  comb() {}
  comb(int mx) : f(mx + 1), g(mx + 1) {
    f[0] = 1;
    drep(i, 1, mx) f[i] = f[i - 1] * i;
    g[mx] = f[mx].pow(mod - 2);
    for (int i = mx; i > 0; i--)
      g[i - 1] = g[i] * i;
  }
  mint c(int a, int b) {
    if (a < b)
      return 0;
    return f[a] * g[b] * g[a - b];
  }
} c(3e6);
// c.c(a,b)=>aCb mod

int main() {
  int n, k;
  cin >> n >> k;
  mint ans = 1;
  vector<ll> a(n), ap, am, az;
  rep(i, n) cin >> a[i];
  int apn = 0, amn = 0, azn = 0;
  rep(i, n) {
    if (a[i] > 0) {
      ap.push_back(a[i]);
      apn++;
    }
    if (a[i] < 0) {
      am.push_back(a[i]);
      amn++;
    }
    if (a[i] == 0) {
      az.push_back(a[i]);
      azn++;
    }
  }
  SORT(ap);
  REV(ap);
  SORT(am);
  int pi = 0, mi = 0;
  if (k > 2 * (apn / 2 + amn / 2)) {
    if (k - 2 * (apn / 2 + amn / 2) == 1 && apn % 2 == 1) {
      ans = ap[0];
      k--;
      pi++;
    } else if (azn > 0) {
      cout << 0 << endl;
      return 0;
    } else {
      REV(am);
      REV(ap);
      ans = ap[0] * am[0];
      k -= 2;
      pi++;
      mi++;
    }
  } else if (k % 2 == 1) {
    if (apn > 0) {
      ans = ap[0];
      k--;
      pi++;
    } else if (azn > 0) {
      ans = 0;
      k--;
    } else {
      REV(am);
      ans = am[0];
      k--;
      mi++;
    }
  }
  while (k) {
    ll p2, m2;
    if (pi + 1 > apn - 1)
      p2 = -1;
    else
      p2 = ap[pi] * ap[pi + 1];
    if (mi + 1 > amn - 1)
      m2 = -1;
    else
      m2 = am[mi] * am[mi + 1];
    // cout << p2 << " " << m2 << endl;
    if (p2 > m2) {
      ans *= p2;
      pi += 2;
    } else {
      ans *= m2;
      mi += 2;
    }
    k -= 2;
  }

  cout << ans << endl;
  //  printf("%.10f\\n",ans);

  return 0;
}