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

#include "bits/stdc++.h"
using namespace std;

#define IOS                                                                    \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define nl cout << "\\n"
#define endl "\\n"
#define ll long long
#define ld long double
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c.size())
#define mp make_pair
#define mt make_tuple
#define get(x, c) get<x>(c)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define f first
#define s second

inline ll max(ll a, ll b) { return ((a > b) ? a : b); }
inline ll min(ll a, ll b) { return ((a > b) ? b : a); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}
ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll a = power(x, y / 2);
  if (y % 2 == 0)
    return a * a;
  else
    return x * a * a;
}
ll powermod(ll x, ll ex, ll md) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % md;
    ex >>= 1ll;
    x = (x * x) % md;
  }
  return ans;
}

const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;

const ll N = 2e5 + 11;
ll n, k, a[N];
ll ans(ll a[], ll n, ll k) {
  sort(a, a + n, greater<ll>());
  if (a[0] < 0) {
    ll p = 1, x = 1;
    if (k & 1) {
      rep(i, k) {
        p *= a[i];
        p %= mod;
      }
    } else {
      rep(i, k) {
        p *= a[n - 1 - i];
        p %= mod;
      }
    }
    return (p + mod) % mod;
  }
  if (n == k) {
    ll p = 1;
    ll x = 1;
    rep(i, n) {
      p *= a[i];
      p %= mod;
    }
    return (p + mod) % mod;
  }
  vll pos, neg;
  rep(i, n) {
    if (a[i] >= 0)
      pos.pb(a[i]);
    else
      neg.pb(-a[i]);
  }
  sort(all(pos), greater<ll>());
  sort(all(neg), greater<ll>());
  vll pfxpos(sz(pos) + 1), pfxneg(sz(neg) + 1);
  pfxpos[0] = pfxneg[0] = 1;
  rep(i, sz(pos)) pfxpos[i + 1] = (pfxpos[i] % mod * pos[i] % mod) % mod;
  rep(i, sz(neg)) pfxneg[i + 1] = (pfxneg[i] % mod * neg[i] % mod) % mod;
  pfxpos[0] = pfxneg[0] = 1;
  ll ans = 0;
  /*
  rep(i ,sz(pos)) cout << pos[i] << " "
  ;nl;rep(i , sz(neg)) cout << neg[i] << " ";
  nl;
  */
  for (ll i = 0; i <= min(k, sz(neg)); i += 2) {
    ll p1, p2;
    ll l1 = i, l2 = k - i;
    if (i == 0 && k - i <= sz(pos))
      ans = pfxpos[k];
    else {
      p1 = neg[i - 1] * neg[i - 2];
      if (k - i >= 0 && k - i <= sz(pos)) {
        // cout << i << ": " << k - i <<" " << p1 <<" " << p2 << endl;nl;
        if (k - i >= 0 && k - i < sz(pos)) {
          p2 = pos[k - i + 1] * pos[k - i];
          if (p1 < p2)
            break;
          else {
            ans = pfxneg[i];
            ans *= pfxpos[k - i];
            ans %= mod;
          }
        } else {
          ans = pfxneg[i];
          if (k - i >= 0 && k - i <= sz(pos))
            ans *= pfxpos[k - i];
          ans %= mod;
        }
      }
    }
    // cout << " ans " << ans << " i " << i << " " << k - i << endl;
  }
  return ans;
}
void solve() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  cout << ans(a, n, k);
}
int main() {
  IOS;
  cout << fixed << setprecision(20);
  ll NTC = 1;
  // cin >> NTC;
  ll PTC = 0;
  while (PTC++ < NTC) {
    // cout << "Case #" << PTC << ":" << ' ';
    solve();
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\\n";
}


Error: 0
Time : 53.026ms


Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define IOS                                                                    \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define nl cout << "\\n"
#define endl "\\n"
#define ll long long
#define ld long double
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c.size())
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define f first
#define s second

inline ll max(ll a, ll b) { return ((a > b) ? a : b); }
inline ll min(ll a, ll b) { return ((a > b) ? b : a); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}
ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll a = power(x, y / 2);
  if (y % 2 == 0)
    return a * a;
  else
    return x * a * a;
}
ll powermod(ll x, ll ex, ll md) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % md;
    ex >>= 1ll;
    x = (x * x) % md;
  }
  return ans;
}

const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;

const ll N = 2e5 + 11;
ll n, a[N], k;
ll max_k_length_subsequence_product(ll a[], ll n, ll k, ll mod) {
  sort(a, a + n, greater<ll>());
  if (n == k) {
    ll p = 1;
    rep(i, n) {
      p *= a[i];
      if (p > 0)
        p %= mod;
    }
    return p;
  } /*
   rep(i ,n )  cout << a[i] << " " ;
   nl;*/
  if (a[0] < 0) {
    ll p = 1;
    if (!(k & 1)) {
      rep(i, k) {
        p *= a[n - 1 - i];
        if (p > 0)
          p %= mod;
      }
    } else {
      rep(i, k) {
        p *= a[i];
        if (p > 0)
          p %= mod;
      }
    }
    return (p + mod) % mod;
  }
  vll vplus, vminus, pfxplus(n + 1, 1), pfxminus(n + 1, 1);
  rep(i, n) {
    if (a[i] > 0)
      vplus.pb(a[i]);
    else
      vminus.pb(-a[i]);
  }
  sort(all(vplus), greater<ll>());
  sort(all(vminus), greater<ll>());
  ll pr = 1;
  rep(i, sz(vplus)) {
    pr *= vplus[i];
    pr %= mod;
    pfxplus[i + 1] = (pr);
  }
  pr = 1;
  rep(i, sz(vminus)) {
    pr *= vminus[i];
    pr %= mod;
    pfxminus[i + 1] = (pr);
  } /*
   cout <<"plus\\n";
   rep(i , sz(vplus)) cout << vplus[i] <<" ";
   nl;
   cout << "pfxplus\\n";
   rep(i , n) cout << pfxplus[i + 1] <<"  ";
   nl;
   cout <<" minus\\n";
   rep(i , sz(vminus)) cout << vminus[i] <<" ";
   nl;
   cout <<"pfxminus\\n";
   rep(i ,n ) cout << pfxminus[i + 1] << " " ;
   nl;*/
  ll ans = pfxplus[k];
  for (ll i = 2; i <= sz(vminus); i += 2) {
    ll p1 = vminus[i - 1] * vminus[i - 1 - 1];
    ll p2 = vplus[k - i + 1] * vplus[k - i];
    // cout << " p1 " << p1 << " " << p2 << endl;
    if (p1 < p2)
      break;
    else {
      ans = pfxplus[k - i];
      ans *= pfxminus[i];
      ans %= mod;
    }
  }
  return ans;
}
void solve() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  cout << max_k_length_subsequence_product(a, n, k, mod) << endl;
}
int main() {
  IOS;
  cout << fixed << setprecision(20);
  ll NTC = 1;
  // cin >> NTC;
  ll PTC = 0;
  while (PTC++ < NTC) {
    // cout << "Case #" << PTC << ":" << ' ';
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}