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

#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const double Pii = 3.14159265359;
ll n, x, k;
ll a[N], b[N];

int main() {
  FIO vector<ll> pos, neg;
  bool z = 0;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(-x);
    else
      z = 1;
  }

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  int psz = pos.size(), nsz = neg.size();
  ll ans = 1;
  ll tk = 0;
  bool ok = 1;

  if (psz + ((nsz / 2) * 2) >= k && !(psz == 0 && k % 2 == 1)) {
    int p1 = psz - 1, p2 = nsz - 1;
    if (k % 2 == 1) {
      ans *= pos[p1--];
      ans %= M;
      tk++;
    }
    while (tk < k) {
      if (p2 > 0 && p1 > 0) {
        if (pos[p1] * pos[p1 - 1] > neg[p2] * neg[p2 - 1]) {
          ans *= pos[p1--];
          ans %= M;
          ans *= pos[p1--];
          ans %= M;
          tk += 2;
        } else {
          ans *= neg[p2--];
          ans %= M;
          ans *= neg[p2--];
          ans %= M;
          tk += 2;
        }
      } else if (p1 > 0) {
        ans *= pos[p1--];
        ans %= M;
        ans *= pos[p1--];
        ans %= M;
        tk += 2;
      } else {
        ans *= neg[p2--];
        ans %= M;
        ans *= neg[p2--];
        ans %= M;
        tk += 2;
      }
    }
  } else if (z)
    ans = 0;
  else {
    int p1 = 0, p2 = 0;
    while (tk < k) {
      if (p1 < psz && p2 < nsz) {
        if (pos[p1] < -neg[p2]) {
          ans *= pos[p1++];
          ans %= M;
        } else {
          ans *= neg[p2++];
          ans %= M;
        }
      } else if (p1 < psz) {
        ans *= pos[p1++];
        ans %= M;
      } else {
        ans *= neg[p2++];
        ans %= M;
      }
      tk++;
    }
    ok = 0;
  }
  if (ok)
    cout << ans << endl;
  else
    cout << ((M - ans) + M) % M << endl;
  return 0;
}


Error: WA

Fixed submission:

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

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;

const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);

#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b)                                                          \\
  for (long long i = (long long)(a); i >= (long long)(b); i--)
#define ull unsigned long long
#define deb(x) cerr << #x << " = " << x << endl;
#define endl "\\n"
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define verify(x)                                                              \\
  if (x >= MOD)                                                                \\
    x -= MOD;

inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }

template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;

template <typename T> using maxpq = priority_queue<T>;
/*----------------------------------------------------------------------*/

bool cmp(ll a, ll b) { return abs(a) < abs(b); }

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, k;
  cin >> n >> k;
  vll arr(n);
  FOR(i, 0, n) cin >> arr[i];
  sort(all(arr), cmp);
  int cnt = 0, minneg = INT_MAX, minnegi = -1, minpos = INT_MAX, minposi = -1;
  FOR(i, n - k, n) {
    if (arr[i] == 0) {
      cout << 0;
      return 0;
    } else if (arr[i] < 0) {
      cnt = 1 - cnt;
      if (abs(minneg) > abs(arr[i])) {
        minneg = arr[i];
        minnegi = i;
      }
    } else {
      if (minpos > arr[i]) {
        minpos = arr[i];
        minposi = i;
      }
    }
  }

  ll res = 1;
  if (!cnt) {
    FOR(i, n - k, n) {
      res *= abs(arr[i]);
      res = (res + MOD) % MOD;
    }
  } else {
    int posi = -1;
    RFOR(i, n - k - 1, 0) {
      if (arr[i] >= 0) {
        posi = i;
        break;
      }
    }
    int negi = -1;
    RFOR(i, n - k - 1, 0) {
      if (arr[i] <= 0) {
        negi = i;
        break;
      }
    }

    if (posi >= 0 && minnegi >= 0 && negi >= 0 && minposi >= 0) {
      // for posi. arr[posi] / arr[minnegi] > arr[negi] / arr[minposi]
      ll p = arr[posi] * 1LL * arr[minposi];
      ll n = arr[negi] * 1LL * arr[minnegi];
      if (p > n) {
        res *= arr[posi];
        res %= MOD;
        FOR(i, n - k, n) {
          if (i == minnegi)
            continue;
          res *= abs(arr[i]);
          res %= MOD;
        }
      } else {
        res *= abs(arr[negi]);
        res %= MOD;
        FOR(i, n - k, n) {
          if (i == minposi)
            continue;
          res *= abs(arr[i]);
          res %= MOD;
        }
      }
    } else if (posi >= 0 && minnegi >= 0) {
      // take last k except minnegi. instead take posi
      res *= abs(arr[posi]);
      res %= MOD;
      FOR(i, n - k, n) {
        if (i == minnegi)
          continue;
        res *= abs(arr[i]);
        res %= MOD;
      }
    } else if (negi >= 0 && minposi >= 0) {
      // take last k except minposi. instead take negi
      res *= abs(arr[negi]);
      res %= MOD;
      FOR(i, n - k, n) {
        if (i == minposi)
          continue;
        res *= abs(arr[i]);
        res %= MOD;
      }
    } else {
      int cnt2 = 0;
      FOR(i, 0, k) {
        if (arr[i] < 0)
          cnt2 = 1 - cnt2;
        res *= abs(arr[i]);
        res = (res + MOD) % MOD;
      }
      if (cnt2)
        res *= -1;
      res = (res + MOD) % MOD;
    }
  }
  cout << res;
  return 0;
}