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

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using ll = long long;
#define ull unsigned ll
#define endl "\\n"
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define mst(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ppi pair<int, pii>
#define mt make_tuple
#define eb emplace_back
using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;
const int inf = 1e9;

ll power(ll x, ll p) {
  ll r = 1ll;
  x = x % mod;
  while (p > 0) {
    if (p & 1)
      r = (r * x) % mod;
    p = p >> 1;
    x = (x * x) % mod;
  }
  return r;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a, b;
  ll c1 = 0, temp = 1ll, c0 = 0;
  for (ll i = 0; i < n; i++) {
    ll j;
    cin >> j;
    // temp = (temp * abs(j)) % mod;
    if (j > 0) {
      a.pb(j);
      temp = (temp * j) % mod;
    } else if (j == 0) {
      c0++;
      temp = 0ll;
    } else {
      b.pb(-j);
      c1++;
      temp = (temp * (-j)) % mod;
    }
  }
  if (k == n) {
    if (c0 > 0) {
      cout << 0 << endl;
      return;
    }
    if (c1 % 2 == 1) {
      cout << (mod - temp) % mod << endl;
    } else
      cout << temp << endl;
    return;
  }
  sort(all(a));
  sort(all(b));
  n = a.size();
  ll m = b.size();
  if (a.size() == 0 && k % 2 == 1) {
    if (c0 > 0) {
      cout << 0 << endl;
      return;
    }
    assert(k <= m);
    ll ans = 1ll;
    for (ll i = 0; i < k; i++) {
      ans = (ans * b[i]) % mod;
    }
    cout << (-ans + mod) % mod << endl;
    return;
  }
  ll ans = 1ll;
  if (n + m < k) {
    cout << 0 << endl;
    return;
  }
  ll c = 0, i = n - 1, j = m - 1;
  while (k > 0) {
    if (i >= 0 && j >= 0) {
      if (a[i] > b[j]) {
        ans = (ans * a[i]) % mod;
        i--;
      } else {
        ans = (ans * b[j]) % mod;
        j--;
        c++;
      }
    } else if (i >= 0) {
      ans = (ans * a[i]) % mod;
      i--;
    } else if (j >= 0) {
      ans = (ans * b[j]) % mod;
      j--;
      c++;
    }
    k--;
  }
  if (c % 2 == 1) {
    if (i >= 0 && j + 1 < m && j >= 0 && i + 1 < n) {
      if (a[i] * a[i + 1] >= b[j] * b[j + 1]) {
        ans = (ans * power(b[j + 1], mod - 2)) % mod;
        ans = (ans * a[i]) % mod;
      } else {
        ans = (ans * power(a[i + 1], mod - 2)) % mod;
        ans = (ans * b[j]) % mod;
      }
    } else if (j >= 0 && i + 1 < n) {
      ans = (ans * power(a[i + 1], mod - 2)) % mod;
      ans = (ans * b[j]) % mod;
    } else if (i >= 0 && j + 1 < m) {
      ans = (ans * power(b[j + 1], mod - 2)) % mod;
      ans = (ans * a[i]) % mod;
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, p;
  t = 1;
  // cin>>t;
  for (p = 1; p <= t; p++) {
    // cout << power(2, 5);
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using ll = long long;
#define ull unsigned ll
#define endl "\\n"
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define mst(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ppi pair<int, pii>
#define mt make_tuple
#define eb emplace_back
using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;
const int inf = 1e9;

ll power(ll x, ll p) {
  ll r = 1ll;
  x = x % mod;
  while (p > 0) {
    if (p & 1)
      r = (r * x) % mod;
    p = p >> 1;
    x = (x * x) % mod;
  }
  return r;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a, b;
  ll temp = 1ll, c0 = 0;
  for (ll i = 0; i < n; i++) {
    ll j;
    cin >> j;
    // temp = (temp * abs(j)) % mod;
    if (j > 0) {
      a.pb(j);
      temp = (temp * j) % mod;
    } else if (j == 0) {
      c0++;
      temp = 0ll;
    } else {
      b.pb(-j);
      temp = (temp * (-j)) % mod;
    }
  }
  if (k == n) {
    if (c0 > 0) {
      cout << 0 << endl;
      return;
    }
    cout << temp << endl;
    return;
  }
  sort(all(a));
  sort(all(b));
  n = a.size();
  ll m = b.size();
  if (a.size() == 0 && k % 2 == 1) {
    if (c0 > 0) {
      cout << 0 << endl;
      return;
    }
    assert(k <= m);
    ll ans = 1ll;
    for (ll i = 0; i < k; i++) {
      ans = (ans * b[i]) % mod;
    }
    cout << (-ans + mod) % mod << endl;
    return;
  }
  ll ans = 1ll;
  if (n + m < k) {
    cout << 0 << endl;
    return;
  }
  ll c = 0, i = n - 1, j = m - 1;
  while (k > 0) {
    if (i >= 0 && j >= 0) {
      if (a[i] > b[j]) {
        ans = (ans * a[i]) % mod;
        i--;
      } else {
        ans = (ans * b[j]) % mod;
        j--;
        c++;
      }
    } else if (i >= 0) {
      ans = (ans * a[i]) % mod;
      i--;
    } else if (j >= 0) {
      ans = (ans * b[j]) % mod;
      j--;
      c++;
    }
    k--;
  }
  ll ta = ans;
  if (c % 2 == 1) {
    bool f = 0;
    if (i >= 0 && j + 1 < m && j >= 0 && i + 1 < n) {
      if (a[i] * a[i + 1] >= b[j] * b[j + 1]) {
        ans = (ans * power(b[j + 1], mod - 2)) % mod;
        ans = (ans * a[i]) % mod;
        f = 1;
      } else {
        ans = (ans * power(a[i + 1], mod - 2)) % mod;
        ans = (ans * b[j]) % mod;
        f = 1;
      }
    } else if (j >= 0 && i + 1 < n) {
      ans = (ans * power(a[i + 1], mod - 2)) % mod;
      ans = (ans * b[j]) % mod;
      f = 1;
    } else if (i >= 0 && j + 1 < m) {
      ans = (ans * power(b[j + 1], mod - 2)) % mod;
      ans = (ans * a[i]) % mod;
      f = 1;
    }
    assert(ta <= ans);
    assert(f == 1);
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, p;
  t = 1;
  // cin>>t;
  for (p = 1; p <= t; p++) {
    // cout << power(2, 5);
    solve();
  }
  return 0;
}