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

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> po;
  vector<ll> ne;
  ll cnt_zero = 0;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x >= 0) {
      po.pb(x);
    } else if (x != 0) {
      ne.pb(x);
    }
    if (x == 0) {
      cnt_zero++;
    }
  }
  ll po_len = po.size();
  ll ne_len = ne.size();

  // for(int i = 0; i < po_len; i++) {
  // 	cout << ne[i] << endl;
  // }

  if (k == po_len + ne_len) {

    sort(ALL(po), greater<ll>());
    sort(ALL(ne));
    vector<ll> sum_po(po_len + 1, 1LL);
    vector<ll> sum_ne(ne_len + 1, 1LL);

    for (ll i = 0; i < po_len; i++) {
      sum_po[i + 1] = sum_po[i] * po[i];
      sum_po[i + 1] %= mod;
    }
    for (ll i = 0; i < ne_len; i++) {
      sum_ne[i + 1] = sum_ne[i] * ne[i];
      sum_ne[i + 1] %= mod;
      if (sum_ne[i + 1] < 0LL) {
        sum_ne[i + 1] += mod;
      }
    }

    if (cnt_zero != 0LL) {
      cout << 0 << endl;
    } else {
      if (po_len == 0) {
        cout << sum_ne[ne_len] << endl;
      } else if (ne_len == 0) {
        cout << sum_po[po_len] << endl;
      } else {
        cout << (sum_po[po_len] * sum_ne[ne_len]) % mod << endl;
      }
    }
    return 0;
  }

  else if (k % 2 == 1 && po_len == 0) {
    ll res;
    sort(ALL(ne), greater<ll>());
    vector<ll> sum_ne(ne_len + 1, 1LL);
    for (ll i = 0; i < ne_len; i++) {
      sum_ne[i + 1] = sum_ne[i] * ne[i];
      sum_ne[i + 1] %= mod;
      // 	if(sum_ne[i+1] < 0LL){
      // 	sum_ne[i+1] += mod;
      // }
    }
    if (cnt_zero != 0) {
      res = max(0LL, sum_ne[k]);
    } else {
      res = sum_ne[k];
    }
    if (res < 0LL) {
      res += mod;
    }
    cout << res << endl;
    return 0;
  } else {
    sort(ALL(po), greater<ll>());
    sort(ALL(ne));
    ll ans = 1LL;
    ll id_p = 0;
    ll id_n = 0;
    ll element = 0;
    // if (k % 2 == 1){
    // 	ans *= po[0];
    // 	id_p++;
    // 	element++;
    // }
    while (element < k) {
      if (element < k - 1 && id_n + 1 < ne_len &&
          ((id_p + 1 >= po_len) ||
           (id_p + 1 < po_len &&
            po[id_p] * po[id_p + 1] <= ne[id_n] * ne[id_n + 1]))) {
        ans = ans * ne[id_n] % mod;
        ans = ans * ne[id_n + 1] % mod;
        id_n += 2;
        element += 2;
      } else {
        ans = ans * po[id_p] % mod;
        id_p += 1;
        element += 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> po;
  vector<ll> ne;
  ll cnt_zero = 0;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0) {
      po.pb(x);
    } else if (x != 0) {
      ne.pb(x);
    } else {
      cnt_zero++;
    }
  }
  ll po_len = po.size();
  ll ne_len = ne.size();

  k -= cnt_zero;

  // for(int i = 0; i < po_len; i++) {
  // 	cout << ne[i] << endl;
  // }

  if (k == po_len + ne_len) {

    sort(ALL(po), greater<ll>());
    sort(ALL(ne));
    vector<ll> sum_po(po_len + 1, 1LL);
    vector<ll> sum_ne(ne_len + 1, 1LL);

    for (ll i = 0; i < po_len; i++) {
      sum_po[i + 1] = sum_po[i] * po[i];
      sum_po[i + 1] %= mod;
    }
    for (ll i = 0; i < ne_len; i++) {
      sum_ne[i + 1] = sum_ne[i] * ne[i];
      sum_ne[i + 1] %= mod;
      if (sum_ne[i + 1] < 0LL) {
        sum_ne[i + 1] += mod;
      }
    }

    if (cnt_zero != 0LL) {
      cout << 0 << endl;
    } else {
      if (po_len == 0) {
        cout << sum_ne[ne_len] << endl;
      } else if (ne_len == 0) {
        cout << sum_po[po_len] << endl;
      } else {
        cout << (sum_po[po_len] * sum_ne[ne_len]) % mod << endl;
      }
    }
    return 0;
  }

  else if (k % 2 == 1 && po_len == 0) {
    ll res;
    sort(ALL(ne), greater<ll>());
    vector<ll> sum_ne(ne_len + 1, 1LL);
    for (ll i = 0; i < ne_len; i++) {
      sum_ne[i + 1] = sum_ne[i] * ne[i];
      sum_ne[i + 1] %= mod;
      // 	if(sum_ne[i+1] < 0LL){
      // 	sum_ne[i+1] += mod;
      // }
    }
    if (cnt_zero != 0) {
      res = max(0LL, sum_ne[k]);
    } else {
      res = sum_ne[k];
    }
    if (res < 0LL) {
      res += mod;
    }
    cout << res << endl;
    return 0;
  } else {
    sort(ALL(po), greater<ll>());
    sort(ALL(ne));
    ll ans = 1LL;
    ll id_p = 0;
    ll id_n = 0;
    ll element = 0;
    // if (k % 2 == 1){
    // 	ans *= po[0];
    // 	id_p++;
    // 	element++;
    // }
    while (element < k) {
      if (element < k - 1 && id_n + 1 < ne_len &&
          (id_p >= po_len + 1 ||
           (id_p < po_len + 1 &&
            po[id_p] * po[id_p + 1] <= ne[id_n] * ne[id_n + 1]))) {
        ans *= ne[id_n] % mod;
        ans *= ne[id_n + 1] % mod;
        id_n += 2;
        element += 2;
      } else {
        ans *= po[id_p] % mod;
        id_p += 1;
        element += 1;
      }
      ans %= mod;
      if (ans < 0) {
        ans += mod;
      }
    }
    cout << ans << endl;
  }
  return 0;
}