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

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

#define F first
#define S second
#define nl '\\n'
#define gcd __gcd
#define nset __builtin_popcount
#define pb push_back
#define pf push_front
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define nrep(i, a, b) for (int i = int(a); i >= int(b); i--)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvl = vector<vl>;
using ii = pair<int, int>;
using vii = vector<ii>;
using vs = vector<string>;
using msi = map<string, int>;
using iss = istringstream;

ll mod(ll x, ll M) {
  ll res = x % M;
  return (res >= 0 ? res : res + M);
}

int main() {
  FASTIO;
  const ll M = 1e9 + 7;

  int n, k;
  cin >> n >> k;
  vl a(n);
  int pcnt = 0;

  rep(i, 0, n - 1) {
    cin >> a[i];
    if (a[i] >= 0)
      pcnt++;
  }

  ll ans = 1;
  if (k == n) {
    rep(i, 0, n - 1) ans = mod(ans * a[i], M);
    return cout << ans << nl, 0;
  }

  sort(all(a));

  if (pcnt == 0) {
    if (k & 1)
      nrep(i, n - 1, n - k) ans = mod(ans * a[i], M);
    else
      rep(i, 0, k - 1) ans = mod(ans * a[i], M);
    return cout << ans << nl, 0;
  }

  int leave = -1;
  if (k & 1) {
    nrep(i, n - 1, 0) {
      if (a[i] >= 0) {
        leave = i;
        ans = a[i];
        k--;
        break;
      }
    }
  }

  stack<ll> ps, ns;
  rep(i, 0, n - 1) if (a[i] >= 0 && i != leave) ps.push(a[i]);
  nrep(i, n - 1, 0) if (a[i] < 0) ns.push(a[i]);

  while (k > 0) {
    if (ps.size() > 1 && ns.size() > 1) {
      ll px = ps.top();
      ps.pop();
      ll py = ps.top();
      ps.pop();

      ll nx = ns.top();
      ns.pop();
      ll ny = ns.top();
      ns.pop();

      if (px * py > nx * ny) {
        ans = mod(ans * mod(px * py, M), M);
        ns.push(ny);
        ns.push(nx);
      } else {
        ans = mod(ans * mod(nx * ny, M), M);
        ps.push(py);
        ps.push(px);
      }
    } else if (ps.size() > 1) {
      ll px = ps.top();
      ps.pop();
      ll py = ps.top();
      ps.pop();
      ans = mod(ans * mod(px * py, M), M);
    } else if (ns.size() > 1) {
      ll nx = ns.top();
      ns.pop();
      ll ny = ns.top();
      ns.pop();
      ans = mod(ans * mod(nx * ny, M), M);
    } else
      break;
    k -= 2;
  }

  cout << ans << nl;
  return 0;
}


Error: 0

Fixed submission:

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

#define F first
#define S second
#define nl '\\n'
#define gcd __gcd
#define nset __builtin_popcount
#define pb push_back
#define pf push_front
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define nrep(i, a, b) for (int i = int(a); i >= int(b); i--)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvl = vector<vl>;
using ii = pair<int, int>;
using vii = vector<ii>;
using vs = vector<string>;
using msi = map<string, int>;
using iss = istringstream;

ll mod(ll x, ll M) {
  ll res = x % M;
  return (res >= 0 ? res : res + M);
}

int main() {
  FASTIO;
  const ll M = 1e9 + 7;

  int n, k;
  int korg = k;
  cin >> n >> k;
  vl a(n);
  int pcnt = 0;

  rep(i, 0, n - 1) {
    cin >> a[i];
    if (a[i] >= 0)
      pcnt++;
  }

  sort(all(a));

  ll ans = 1;
  if (pcnt == 0) {
    if (k & 1)
      nrep(i, n - 1, n - k) ans = mod(ans * a[i], M);
    else
      rep(i, 0, k - 1) ans = mod(ans * a[i], M);
    return cout << ans << nl, 0;
  }

  int leave = -1;
  if (k & 1) {
    nrep(i, n - 1, 0) {
      if (a[i] >= 0) {
        leave = i;
        ans = a[i];
        k--;
        break;
      }
    }
  }

  stack<ll> ps, ns;
  rep(i, 0, n - 1) if (a[i] >= 0 && i != leave) ps.push(a[i]);
  nrep(i, n - 1, 0) if (a[i] < 0) ns.push(a[i]);

  while (k > 0) {
    if (ps.size() > 1 && ns.size() > 1) {
      ll px = ps.top();
      ps.pop();
      ll py = ps.top();
      ps.pop();

      ll nx = ns.top();
      ns.pop();
      ll ny = ns.top();
      ns.pop();

      if (px * py > nx * ny) {
        ans = mod(ans * mod(px * py, M), M);
        ns.push(ny);
        ns.push(nx);
      } else {
        ans = mod(ans * mod(nx * ny, M), M);
        ps.push(py);
        ps.push(px);
      }
    } else if (ps.size() > 1) {
      ll px = ps.top();
      ps.pop();
      ll py = ps.top();
      ps.pop();
      ans = mod(ans * mod(px * py, M), M);
    } else if (ns.size() > 1) {
      ll nx = ns.top();
      ns.pop();
      ll ny = ns.top();
      ns.pop();
      ans = mod(ans * mod(nx * ny, M), M);
    } else {
      ans = 1;
      sort(all(a), [](ll x, ll y) {
        if (abs(x) < abs(y))
          return true;
        return false;
      });
      rep(i, 0, korg - 1) ans = mod(ans * a[i], M);
      break;
    }
    k -= 2;
  }

  cout << ans << nl;
  return 0;
}