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

#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IINF 1000000000
#define INF 9223372036854775807
#define MOD 1000000007
#define mod 1000000007
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo - r(ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
// #define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define chmax(x, y) x = mmax(x, y)
#define chmin(x, y) x = mmin(x, y)
#define all(x) (x).begin(), (x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n, k) ((n >> k) & 1)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> Pin;
typedef pair<ll, ll> Pll;
template <class T> using V = vector<T>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
ll pom(ll a, ll n, int m) {
  ll x = 1;
  for (a %= m; n; n /= 2)
    n & 1 ? x = x * a % m : 0, a = a * a % m;
  return x;
}
#define invp(a, p) pom(a, p - 2, p)
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll cmp1(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.fi != b.fi)
    return a.fi < b.fi;
  else
    return a.se > b.se;
}
ll cmp(ll a, ll b) { return abs(a) < abs(b); }
//----------------------------------------------------------------------

//----------------------------------------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //-------------------------------
  // ll begin_t=clock();
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  //------------------------------
  ll n, k;
  cin >> n >> k;
  V<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  if (n == k) {
    for (ll i = 0; i < n; i++)
      ans = ans * a[i] % mod;
    if (ans < 0)
      ans += mod;
    cout << ans << Endl;
    return 0;
  }
  ll po = 0, ne = 0, zero = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] < 0)
      ne++;
    else if (a[i] == 0)
      zero++;
    else
      po++;
  }
  if (n == ne && k % 2 != 0) {
    sort(all(a), cmp);
    for (ll i = 0; i < k; i++) {
      // cout<<a[i]<<endl;
      ans *= a[i];
      ans %= mod;
    }
    ans += mod;
    cout << ans << Endl;
    return 0;
  }
  if (zero > n - k) {
    cout << 0 << endl;
    return 0;
  }
  sort(all(a), cmp);
  ll p = 1;
  for (ll i = n - 1; i > n - 1 - k; i--) {
    p *= a[i];
    p %= mod;
  }
  if (p >= 0) {
    cout << p << endl;
    return 0;
  }
  reverse(all(a));
  ans = p;
  // 使ったもののうち、正で絶対値最小、負で絶対値最小を探す
  ll p1 = INF, m1 = INF;
  rep(i, 0, k) {
    if (a[i] < 0)
      m1 = a[i];
    else
      p1 = a[i];
  }

  // 使っていないもののうち、非負で絶対値最大、負で絶対値最大を探す
  ll p2 = INF, m2 = INF;
  for (int i = n - 1; i >= k; i--) {
    if (a[i] < 0)
      m2 = a[i];
    else
      p2 = a[i];
  }

  if (p2 == INF) {
    // 正の数を抜いて負の数を入れるしかない
    ans = ans * invp(p1, MOD) % MOD * (MOD + m2) % MOD;
  } else if (m2 == INF || p1 == INF) {
    // 負の数を抜いて非負の数を入れるしかない
    ans = ans * invp(MOD + m1, MOD) % MOD * p2 % MOD;
  } else {
    // 正の数を抜いて負の数を入れる・負の数を抜いて非負の数を入れる
    // どちらがいいか選ぶ
    if (p2 * p1 >= m2 * m1) { // p2/m1>=m2/p1
      // 負の数を抜いて非負の数を入れる
      ans = ans * invp(MOD + m1, MOD) % MOD * p2 % MOD;
    } else {
      // 正の数を抜いて負の数を入れる
      ans = ans * invp(p1, MOD) % MOD * (MOD + m2) % MOD;
    }
  }
  printf("%lld\\n", (ans + MOD) % MOD);
  //------------------------------
  // fclose(stdin);
  // fclose(stdout);
  // ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
  //-------------------------------
  return 0;
}
//----------------------------------------------------------------------


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IINF 1000000000
#define INF 9223372036854775807
#define MOD 1000000007
#define mod 1000000007
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo - r(ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
// #define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define chmax(x, y) x = mmax(x, y)
#define chmin(x, y) x = mmin(x, y)
#define all(x) (x).begin(), (x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n, k) ((n >> k) & 1)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> Pin;
typedef pair<ll, ll> Pll;
template <class T> using V = vector<T>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll cmp1(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.fi != b.fi)
    return a.fi < b.fi;
  else
    return a.se > b.se;
}
ll cmp(ll a, ll b) { return abs(a) < abs(b); }
//----------------------------------------------------------------------

//----------------------------------------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //-------------------------------
  // ll begin_t=clock();
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  //------------------------------
  ll n, k;
  cin >> n >> k;
  V<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  if (n == k) {
    for (ll i = 0; i < n; i++)
      ans = ans * a[i] % mod;
    if (ans < 0)
      ans += mod;
    cout << ans << Endl;
    return 0;
  }
  ll po = 0, ne = 0, zero = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] < 0)
      ne++;
    else if (a[i] == 0)
      zero++;
    else
      po++;
  }
  if (n == ne && k % 2 != 0) {
    sort(all(a));
    for (ll i = n - 1; i > n - 1 - k; i--) {
      ans *= a[i];
      ans %= mod;
    }
    ans += mod;
    cout << ans << Endl;
    return 0;
  }
  if (zero > n - k) {
    cout << 0 << endl;
    return 0;
  }
  sort(all(a), cmp);
  ll p = 1;
  for (ll i = n - 1; i > n - 1 - k; i--) {
    p *= a[i];
    p %= mod;
  }
  if (p >= 0) {
    cout << p << endl;
    return 0;
  }

  ll leftpo = 0, leftne = 0;
  ll rightpo = 0, rightne = 0;
  for (ll i = 0; i <= n - 1 - k; i++) {
    if (a[i] > 0)
      chmax(leftpo, a[i]);
    else if (a[i] < 0)
      chmin(leftne, a[i]);
  }
  for (ll i = n - k; i < n; i++) {
    if (a[i] > 0)
      chmax(rightpo, a[i]);
    else if (a[i] < 0)
      chmin(rightne, a[i]);
  }
  ll p1 = p * rightpo / leftne;
  ll p2 = p * rightne / leftpo;
  ans = mmax(p1, p2);
  cout << ans % mod << endl;
  //------------------------------
  // fclose(stdin);
  // fclose(stdout);
  // ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
  //-------------------------------
  return 0;
}
//----------------------------------------------------------------------