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

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define all(a) begin(a), end(a)
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << ": " << a << ENDL;
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef pair<lli, lli> ii;
typedef long double ld;
typedef vector<lli> vi;

lli MOD = 1e9 + 7;

int main() {
  _ lli n, k, num;
  cin >> n >> k;
  vi pos, neg, both;
  lli zeros = 0;
  fore(i, 0, n) {
    cin >> num;
    if (num == 0) {
      ++zeros;
      continue;
    }
    if (num > 0)
      pos.pb(num);
    else
      neg.pb(abs(num));
    both.pb(abs(num));
  }
  sort(all(both));
  if (sz(pos) + sz(neg) < k) {
    cout << 0 << ENDL;
    return 0;
  }
  if (sz(pos) == 0) {
    if (k % 2) {
      if (zeros) {
        cout << 0 << ENDL;
        return 0;
      }
      sort(all(neg));
      lli cur = 1;
      fore(i, 0, k) { cur = (cur * neg[i]) % MOD; }
      cur = (MOD - cur) % MOD;
      cout << cur << ENDL;
      return 0;
    } else {
      sort(rbegin(neg), rend(neg));
      lli cur = 1;
      fore(i, 0, k) cur = (cur * neg[i]) % MOD;
      cout << cur << ENDL;
      return 0;
    }
  }
  if (sz(pos) + sz(neg) == k) {
    if (sz(neg) % 2 and zeros) {
      cout << 0 << ENDL;
      return 0;
    }
    lli cur = 1;
    for (auto it : pos)
      cur = (cur * it) % MOD;
    for (auto it : neg)
      cur = (cur * it) % MOD;
    if (sz(neg) % 2)
      cur = (MOD - cur) % MOD;
    cout << cur << ENDL;
    return 0;
  }
  sort(rbegin(pos), rend(pos));
  sort(rbegin(neg), rend(neg));
  int bundlePos = (sz(pos) - sz(pos) % 2);
  int bundleNeg = (sz(neg) - sz(neg) % 2);
  int up = 0, un = 0;
  vi packP, packN;
  for (int i = 1; i < sz(neg); i += 2) {
    packN.pb(neg[i] * neg[i - 1]);
  }
  for (int i = 1; i < sz(pos) - ((sz(pos) % 2) - (k % 2)); i += 2) {
    packP.pb(pos[i] * pos[i - 1]);
  }
  int kk = (k - k % 2) / 2;
  int cntP = 0, cntN = 0;
  lli ans = 1;
  if (sz(packN) == 0) {
    fore(i, 0, kk) ans = (ans * (packP[cntP++] % MOD)) % MOD;
    if (k % 2)
      ans = (ans * pos[cntP * 2]) % MOD;
    cout << ans << ENDL;
    return 0;
  }
  if (sz(packP) == 0) {
    fore(i, 0, kk) ans = (ans * (packN[i] % MOD)) % MOD;
    if (k % 2)
      ans = (ans * pos[0]) % MOD;
    cout << ans << ENDL;
    return 0;
  }
  lli cur = 1;
  fore(i, 0, kk) {
    if (cntP == sz(packP)) {
      cur = (cur * (packN[cntN++] % MOD)) % MOD;
      continue;
    }
    if (cntN == sz(packN)) {
      cur = (cur * (packP[cntP++] % MOD)) % MOD;
      continue;
    }
    if (packP[cntP] > packN[cntN]) {
      cur = (cur * (packP[cntP++] % MOD)) % MOD;
    } else
      cur = (cur * (packN[cntN++] % MOD)) % MOD;
  }
  if (k % 2)
    cur = (cur * (pos[cntP * 2] % MOD)) % MOD;
  cout << cur << ENDL;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define all(a) begin(a), end(a)
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << ": " << a << ENDL;
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef pair<lli, lli> ii;
typedef long double ld;
typedef vector<lli> vi;

lli MOD = 1e9 + 7;

int main() {
  _ lli n, k, num;
  cin >> n >> k;
  vi pos, neg, both;
  lli zeros = 0;
  fore(i, 0, n) {
    cin >> num;
    if (num == 0) {
      ++zeros;
      continue;
    }
    if (num > 0)
      pos.pb(num);
    else
      neg.pb(abs(num));
    both.pb(abs(num));
  }
  sort(all(both));
  if (sz(pos) + sz(neg) < k) {
    cout << 0 << ENDL;
    return 0;
  }
  if (sz(pos) == 0) {
    if (k % 2) {
      if (zeros) {
        cout << 0 << ENDL;
        return 0;
      }
      sort(all(neg));
      lli cur = 1;
      fore(i, 0, k) { cur = (cur * neg[i]) % MOD; }
      cur = (MOD - cur) % MOD;
      cout << cur << ENDL;
      return 0;
    } else {
      sort(rbegin(neg), rend(neg));
      lli cur = 1;
      fore(i, 0, k) cur = (cur * neg[i]) % MOD;
      cout << cur << ENDL;
      return 0;
    }
  }
  if (sz(pos) + sz(neg) == k) {
    if (sz(neg) % 2 and zeros) {
      cout << 0 << ENDL;
      return 0;
    }
    lli cur = 1;
    for (auto it : pos)
      cur = (cur * it) % MOD;
    for (auto it : neg)
      cur = (cur * it) % MOD;
    if (sz(neg) % 2)
      cur = (MOD - cur) % MOD;
    cout << cur << ENDL;
    return 0;
  }
  sort(rbegin(pos), rend(pos));
  sort(rbegin(neg), rend(neg));
  int bundlePos = (sz(pos) - sz(pos) % 2);
  int bundleNeg = (sz(neg) - sz(neg) % 2);
  int up = 0, un = 0;
  vi packP, packN;
  for (int i = 1; i < sz(neg); i += 2) {
    packN.pb(neg[i] * neg[i - 1]);
  }
  for (int i = 1; i < sz(pos) - ((sz(pos) % 2) - (k % 2)); i += 2) {
    packP.pb(pos[i] * pos[i - 1]);
  }
  int kk = (k - k % 2) / 2;
  int cntP = 0, cntN = 0;
  lli ans = 1;
  if (sz(packN) == 0) {
    fore(i, 0, kk) ans = (ans * packP[cntP++]) % MOD;
    if (k % 2)
      ans = (ans * pos[cntP * 2]) % MOD;
    cout << ans << ENDL;
    return 0;
  }
  if (sz(packP) == 0) {
    fore(i, 0, kk) ans = (ans * packN[i]) % MOD;
    if (k % 2)
      ans = (ans * pos[0]) % MOD;
    cout << ans << ENDL;
    return 0;
  }
  lli cur = 1;
  fore(i, 0, kk) {
    if (cntP == sz(packP)) {
      cur = (cur * (packN[cntN++] % MOD)) % MOD;
      continue;
    }
    if (cntN == sz(packN)) {
      cur = (cur * (packP[cntP++] % MOD)) % MOD;
      continue;
    }
    if (packP[cntP] > packN[cntN]) {
      cur = (cur * (packP[cntP++] % MOD)) % MOD;
    } else
      cur = (cur * (packN[cntN++] % MOD)) % MOD;
  }
  if (k % 2)
    cur = (cur * (packP[cntP * 2] % MOD)) % MOD;
  cout << cur << ENDL;
  return 0;
}