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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18;

/*--------------------------------------------------------------------*/

signed main() {
  int n, k;
  cin >> n >> k;
  vi po, ne;
  int zero = 0, pos, neg;
  rep(i, n) {
    int a;
    cin >> a;
    if (a >= 0)
      po.pb(a);
    else
      ne.pb(a);
  }
  pos = po.size();
  neg = ne.size();

  sort(all(po));
  sort(all(ne));
  reverse(all(po));

  int pro;

  if (n == k) {
    pro = 1;
    rep(i, pos) {
      pro *= po[i];
      pro %= MOD;
    }
    rep(i, neg) {
      pro *= ne[i];
      pro = (pro + MOD * MOD) % MOD;
    }
    cout << pro << endl;
    return 0;
  }

  if (pos == 0 && k % 2 == 1) {
    pro = 1;
    rep(i, k) {
      pro *= ne[neg - i - 1];
      pro = (pro + MOD * MOD) % MOD;
    }
    cout << pro << endl;
    return 0;
  }

  int ip = 0, in = 0;
  pro = 1;
  while (in + ip < k) {

    if (k - ip - in >= 2 && in + 1 < neg && ip + 1 < pos &&
        ne[in] * ne[in + 1] >= po[ip] * po[ip + 1]) {
      pro *= ne[in];
      pro = (pro + MOD * MOD) % MOD;
      pro *= ne[in + 1];
      pro = (pro + MOD * MOD) % MOD;
      in += 2;
    } else if (k - ip - in >= 2 && in + 1 < neg && ip + 1 >= pos) {
      pro *= ne[in];
      pro = (pro + MOD * MOD) % MOD;
      pro *= ne[in + 1];
      pro = (pro + MOD * MOD) % MOD;
      in += 2;

    } else if (ip < pos) {
      pro *= po[ip];
      pro %= MOD;
      ip++;
    } else {
      assert(0);
    }
  }
  cout << pro << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18;

/*--------------------------------------------------------------------*/

signed main() {
  int n, k;
  cin >> n >> k;
  vi po, ne;
  int zero = 0, pos, neg;
  rep(i, n) {
    int a;
    cin >> a;
    if (a >= 0)
      po.pb(a);
    else
      ne.pb(a);
  }
  pos = po.size();
  neg = ne.size();

  sort(all(po));
  sort(all(ne));
  reverse(all(po));

  int pro;

  if (n == k) {
    pro = 1;
    rep(i, pos) {
      pro *= po[i];
      pro %= MOD;
    }
    rep(i, neg) {
      pro *= ne[i];
      pro = (pro + MOD * MOD) % MOD;
    }
    cout << pro << endl;
    return 0;
  }

  if (pos == 0 && k % 2 == 1) {
    pro = 1;
    rep(i, k) {
      pro *= ne[neg - i - 1];
      pro = (pro + MOD * MOD) % MOD;
    }
    cout << pro << endl;
    return 0;
  }

  int ip = 0, in = 0;
  pro = 1;
  while (in + ip < k) {

    if (k - ip - in >= 2 && in + 1 < neg && ip + 1 < pos &&
        ne[in] * ne[in + 1] >= po[ip] * po[ip + 1]) {
      pro *= ne[in];
      pro = (pro + MOD * MOD) % MOD;
      pro *= ne[in + 1];
      pro = (pro + MOD * MOD) % MOD;
      in += 2;
    } else if (k - ip - in >= 2 && in + 1 < neg && ip == pos) {
      pro *= ne[in];
      pro = (pro + MOD * MOD) % MOD;
      pro *= ne[in + 1];
      pro = (pro + MOD * MOD) % MOD;
      in += 2;

    } else if (ip < pos) {
      pro *= po[ip];
      pro %= MOD;
      ip++;
    } else {
      assert(0);
    }
  }
  cout << pro << endl;
  return 0;
}