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

/*input
4 3
-1 -2 -3 -4
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll k, m;
  cin >> m >> k;
  vll a;
  ll cz = 0;
  rep(i, 0, m) {
    cin >> j;
    if (j)
      a.pb(j);
    else
      cz = 1;
  }
  sort(a.rbegin(), a.rend(), [](ll &b, ll &c) { return abs(c) > abs(b); });
  if ((ll)a.size() < k) {
    cout << 0 << endl;
    return;
  }
  vll n, p;
  rep(i, 0, k) {
    if (a[i] < 0)
      n.pb(a[i]);
    else
      p.pb(a[i]);
  }
  if ((((ll)n.size()) % 2) == 0) {
    ll ans = 1;
    for (auto i : p)
      ans *= i, ans %= M;
    for (auto i : n)
      ans *= (-i), ans %= M;
    cout << ans << endl;
    return;
  }
  ll ap = 0, an = 0;
  rep(i, k, (ll)a.size()) {
    if (a[i] > 0) {
      ap = a[i];
      break;
    }
  }
  rep(i, k, (ll)a.size()) {
    if (a[i] < 0) {
      an = a[i];
      break;
    }
  }
  if ((!ap && !an) || (!ap && (ll)p.size() == 0)) {
    if (cz) {
      cout << 0 << endl;
      return;
    }
    ll ans = 1;
    reverse(a.begin(), a.end());
    rep(i, 0, k) { ans *= a[i], ans %= M; }
    if (ans < 0)
      ans += M;
    cout << ans << endl;
    return;
  }
  if (p.size() == 0 ||
      abs(an) * abs(n[(ll)n.size() - 1]) < ap * p[(ll)p.size() - 1]) {
    ll ans = 1;
    rep(i, 0, (ll)n.size() - 1) ans *= n[i], ans %= M;
    for (auto i : p)
      ans *= i, ans %= M;
    ans *= ap;
    ans %= M;
    if (ans < 0)
      ans += M;
    cout << ans << endl;
  } else {
    ll ans = 1;
    rep(i, 0, (ll)p.size() - 1) ans *= p[i], ans %= M;
    for (auto i : n)
      ans *= i, ans %= M;
    ans *= an;
    if (ans < 0)
      ans += M;
    ans %= M;
    cout << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/*input
4 3
-1 -2 -3 -4
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll k, m;
  cin >> m >> k;
  vll a;
  ll cz = 0;
  rep(i, 0, m) {
    cin >> j;
    if (j)
      a.pb(j);
    else
      cz = 1;
  }
  sort(a.rbegin(), a.rend(), [](ll &b, ll &c) { return abs(c) > (b); });
  if ((ll)a.size() < k) {
    cout << 0 << endl;
    return;
  }
  vll n, p;
  rep(i, 0, k) {
    if (a[i] < 0)
      n.pb(a[i]);
    else
      p.pb(a[i]);
  }
  if ((((ll)n.size()) % 2) == 0) {
    ll ans = 1;
    for (auto i : p)
      ans *= i, ans %= M;
    for (auto i : n)
      ans *= (-i), ans %= M;
    cout << ans << endl;
    return;
  }
  ll ap = 0, an = 0;
  rep(i, k, (ll)a.size()) {
    if (a[i] > 0) {
      ap = a[i];
      break;
    }
  }
  rep(i, k, (ll)a.size()) {
    if (a[i] < 0) {
      an = a[i];
      break;
    }
  }
  if ((!ap && !an) || (!ap && (ll)p.size() == 9)) {
    if (cz) {
      cout << 0 << endl;
      return;
    }
    ll ans = 1;
    reverse(a.begin(), a.end());
    rep(i, 0, k) {
      ans *= a[i], ans %= M;
      if (a[i] < 0)
        ans += M;
    }
    cout << ans << endl;
    return;
  }
  if (p.size() == 0 ||
      abs(an) * abs(n[(ll)n.size() - 1]) < ap * p[(ll)p.size() - 1]) {
    ll ans = 1;
    rep(i, 0, (ll)n.size() - 1) ans *= abs(n[i]), ans %= M;
    for (auto i : p)
      ans *= abs(i), ans %= M;
    ans *= ap;
    ans %= M;
    cout << ans << endl;
  } else {
    ll ans = 1;
    rep(i, 0, (ll)p.size() - 1) ans *= p[i], ans %= M;
    for (auto i : n)
      ans *= abs(i), ans %= M;
    ans *= an;
    if (ans < 0)
      ans += M;
    ans %= M;
    cout << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}