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

#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define REP(i, n) for ((i) = 0; (i) < (n); (i)++)
#define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++)
#define REV(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++)
#define foreach(it, c)                                                         \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define foreach2d(i, j, v)                                                     \\
  foreach (i, v)                                                               \\
    foreach (j, *i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define MMS(x, n) memset(x, n, sizeof(x))
#define mms(x, n, s) memset(x, n, sizeof(x) * s)
#define pb push_back
#define mp make_pair
#define NX next_permutation
#define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define CV(x, n) count(all(x), (n))
#define FIND(x, n) find(all(x), (n)) - (x).begin()
#define ACC(x) accumulate(all(x), 0)
#define PPC(x) __builtin_popcountll(x)
#define LZ(x) __builtin_clz(x)
#define TZ(x) __builtin_ctz(x)
#define mxe(x) *max_element(all(x))
#define mne(x) *min_element(all(x))
#define low(x, i) lower_bound(all(x), i)
#define upp(x, i) upper_bound(all(x), i)
#define NXPOW2(x) (1ll << ((int)log2(x) + 1))
#define PR(x) cout << #x << " = " << (x) << endl;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int OO = (int)2e9;
const double eps = 1e-9;

const int N = 2e5 + 5;
const int mod = (int)1e9 + 7;

int n, k;
ll a[N];
vector<ll> pos, neg, zer;

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
//	freopen("in.txt", "rt", stdin);
//	freopen("out.txt", "wt", stdout);
#endif
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      zer.push_back(0);
    else if (a[i] > 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(rall(pos));
  sort(all(neg));
  int ps = SZ(pos), ns = SZ(neg);
  ll res = 1;
  if (k & 1) {
    if (pos.empty()) {
      if (!zer.empty()) {
        res = 0;
      } else {
        for (int i = 0; i < k; i++) {
          res = ((res * neg[ns - 1 - i]) % mod + mod) % mod;
        }
      }
      cout << res << endl;
      return 0;
    }
    k--;
    res = pos[0];
    pos.erase(pos.begin());
    ps--;
  }
  if (ps - ps % 2 + (ns - ns % 2) < k) {
    if (!zer.empty()) {
      res = 0;
    } else {
      for (int i = 0; i < n; i++) {
        res = ((res * a[i]) % mod + mod) % mod;
      }
    }
    cout << res << endl;
    return 0;
  }
  int ii = 0, jj = 0;
  for (int i = 0; i < k; i += 2) {
    if (ii + 1 < ps && jj + 1 < ns) {
      if (pos[ii] * pos[ii + 1] > neg[jj] * neg[jj + 1]) {
        res = res * (pos[ii] * pos[ii + 1] % mod) % mod;
        ii += 2;
      } else {
        res = res * (neg[jj] * neg[jj + 1] % mod) % mod;
        jj += 2;
      }
    } else if (ii + 1 < ps) {
      res = res * (pos[ii] * pos[ii + 1] % mod) % mod;
      ii += 2;
    } else if (jj + 1 < ns) {
      res = res * (neg[jj] * neg[jj + 1] % mod) % mod;
      jj += 2;
    } else {
      assert(0);
    }
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define REP(i, n) for ((i) = 0; (i) < (n); (i)++)
#define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++)
#define REV(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++)
#define foreach(it, c)                                                         \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define foreach2d(i, j, v)                                                     \\
  foreach (i, v)                                                               \\
    foreach (j, *i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define MMS(x, n) memset(x, n, sizeof(x))
#define mms(x, n, s) memset(x, n, sizeof(x) * s)
#define pb push_back
#define mp make_pair
#define NX next_permutation
#define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define CV(x, n) count(all(x), (n))
#define FIND(x, n) find(all(x), (n)) - (x).begin()
#define ACC(x) accumulate(all(x), 0)
#define PPC(x) __builtin_popcountll(x)
#define LZ(x) __builtin_clz(x)
#define TZ(x) __builtin_ctz(x)
#define mxe(x) *max_element(all(x))
#define mne(x) *min_element(all(x))
#define low(x, i) lower_bound(all(x), i)
#define upp(x, i) upper_bound(all(x), i)
#define NXPOW2(x) (1ll << ((int)log2(x) + 1))
#define PR(x) cout << #x << " = " << (x) << endl;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int OO = (int)2e9;
const double eps = 1e-9;

const int N = 2e5 + 5;
const int mod = (int)1e9 + 7;

int n, k, m;
ll a[N];
vector<ll> pos, neg, zer;

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
//	freopen("in.txt", "rt", stdin);
//	freopen("out.txt", "wt", stdout);
#endif
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      zer.push_back(0);
    else if (a[i] > 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(rall(pos));
  sort(all(neg));
  n = SZ(pos), m = SZ(neg);
  ll res = 1;
  if (k & 1) {
    if (pos.empty()) {
      if (!zer.empty()) {
        res = 0;
      } else {
        for (int i = 0; i < k; i++) {
          res = ((res * neg[m - 1 - i]) % mod + mod) % mod;
        }
      }
      cout << res << endl;
      return 0;
    }
    k--;
    res = pos[0];
    pos.erase(pos.begin());
    n--;
  }
  if (n - n % 2 + (m - m % 2) < k) {
    if (!zer.empty()) {
      res = 0;
    } else {
      for (int i = 0; i < n; i++) {
        res = ((res * a[i]) % mod + mod) % mod;
      }
    }
    cout << res << endl;
    return 0;
  }
  int ii = 0, jj = 0;
  for (int i = 0; i < k; i += 2) {
    if (ii + 1 < n && jj + 1 < m) {
      if (pos[ii] * pos[ii + 1] > neg[jj] * neg[jj + 1]) {
        res = res * (pos[ii] * pos[ii + 1] % mod) % mod;
        ii += 2;
      } else {
        res = res * (neg[jj] * neg[jj + 1] % mod) % mod;
        jj += 2;
      }
    } else if (ii + 1 < n) {
      res = res * (pos[ii] * pos[ii + 1] % mod) % mod;
      ii += 2;
    } else if (ii + 1 < n) {
      res = res * (neg[jj] * neg[jj + 1] % mod) % mod;
      jj += 2;
    } else {
      assert(0);
    }
  }
  cout << res << endl;
  return 0;
}