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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var) accumulate(all(var), 0)
#define f_sumll(var) accumulate(all(var), 0LL)
#define chmin(v1, v2) v1 = min(v1, v2)
#define chmax(v1, v2) v1 = max(v1, v2)
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
void prtok(bool ok) { prt(ok ? "Yes" : "No"); }
//----------------------------------------------------------------

int main(void) {
  int n, k;
  cin >> n >> k;
  arrll(a, n);

  vecll minus;
  vecll plus;
  vecll zero;
  rep(i, n) {
    if (a[i] > 0)
      plus.pb(a[i]);
    if (a[i] < 0)
      minus.pb(a[i]);
    if (a[i] == 0)
      zero.pb(0);
  }
  int np = plus.size();
  int nm = minus.size();
  int nz = zero.size();
  sort(all(plus), greater<ll>());
  sort(all(minus), greater<ll>());

  ll ans = 0;
  if (np + nm < k) {
    ans = 0;
  } else if (np + nm == k && nm % 2 == 0) {
    ans = 1;
    rep(i, np) {
      ans *= plus[i];
      ans %= MOD;
    }
    rep(i, nm) {
      ans *= minus[nm - i - 1];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (np + nm == k && nm % 2 == 1 && nz > 0) {
    ans = 0;
  } else if (np + nm == k && nm % 2 == 1 && nz == 0) {
    ans = 1;
    rep(i, np) {
      ans *= plus[i];
      ans %= MOD;
    }
    rep(i, nm) {
      ans *= minus[i];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (k % 2 == 1 && np == 0 && nz > 0) { // k<np+nm;
    ans = 0;
  } else if (k % 2 == 1 && np == 0 && nz == 0) {
    ans = 1;
    rep(i, k) {
      ans *= minus[i];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (k % 2 == 1 && np > 0) {
    sort(all(minus));
    ans = plus[0];
    int idp = 1;
    int idm = 0;
    int kp = k - 1;
    while (kp > 0) {
      ll vsp = 0;
      ll vsm = 0;
      if (idp + 1 < np)
        vsp = plus[idp] * plus[idp + 1];
      if (idm + 1 < nm)
        vsm = minus[idm] * minus[idm + 1];
      if (vsp >= vsm) {
        vsp %= MOD;
        ans *= vsp;
        ans %= MOD;
        idp += 2;
      } else {
        vsm %= MOD;
        ans *= vsm;
        ans %= MOD;
        idm += 2;
      }
      kp -= 2;
    }
  } else {
    sort(all(minus));
    ans = 1;
    int idp = 0;
    int idm = 0;
    int kp = k;
    while (kp > 0) {
      ll vsp = 0;
      ll vsm = 0;
      if (idp + 1 < np)
        vsp = plus[idp] * plus[idp + 1];
      if (idm + 1 < nm)
        vsm = minus[idm] * minus[idm + 1];
      if (vsp >= vsm) {
        vsp %= MOD;
        ans *= vsp;
        ans %= MOD;
        idp += 2;
      } else {
        vsm %= MOD;
        ans *= vsm;
        ans %= MOD;
        idm += 2;
      }
      kp -= 2;
    }
  }

  prt(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var) accumulate(all(var), 0)
#define f_sumll(var) accumulate(all(var), 0LL)
#define chmin(v1, v2) v1 = min(v1, v2)
#define chmax(v1, v2) v1 = max(v1, v2)
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
void prtok(bool ok) { prt(ok ? "Yes" : "No"); }
//----------------------------------------------------------------

int main(void) {
  int n, k;
  cin >> n >> k;
  arrll(a, n);

  vecll minus;
  vecll plus;
  vecll zero;
  rep(i, n) {
    if (a[i] > 0)
      plus.pb(a[i]);
    if (a[i] < 0)
      minus.pb(a[i]);
    if (a[i] == 0)
      zero.pb(0);
  }
  int np = plus.size();
  int nm = minus.size();
  int nz = zero.size();
  sort(all(plus), greater<ll>());
  sort(all(minus), greater<ll>());

  ll ans = 0;
  if (np + nm < k) {
    ans = 0;
  } else if (np + nm == k && nm % 2 == 0) {
    ans = 1;
    rep(i, np) {
      ans *= plus[i];
      ans %= MOD;
    }
    rep(i, nm) {
      ans *= minus[nm - i - 1];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (np + nm == k && nm % 2 == 1 && nz > 0) {
    ans = 0;
  } else if (np + nm == k && nm % 2 == 1 && nz == 0) {
    ans = 1;
    rep(i, np) {
      ans *= plus[i];
      ans %= MOD;
    }
    rep(i, nm) {
      ans *= minus[i];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (k % 2 == 1 && np == 0 && nz > 0) { // k<np+nm;
    ans = 0;
  } else if (k % 2 == 1 && np == 0) {
    ans = 1;
    rep(i, k) {
      ans *= minus[i];
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
  } else if (k % 2 == 1 && np > 0) {
    sort(all(minus));
    ans = plus[0];
    int idp = 1;
    int idm = 0;
    int kp = k - 1;
    while (kp > 0) {
      ll vsp = 0;
      ll vsm = 0;
      if (idp + 1 < np)
        vsp = plus[idp] * plus[idp + 1];
      if (idm + 1 < nm)
        vsm = minus[n - idm - 1] * minus[n - idm - 2];
      if (vsp >= vsm) {
        vsp %= MOD;
        ans *= vsp;
        ans %= MOD;
        idp += 2;
      } else {
        vsm %= MOD;
        ans *= vsm;
        ans %= MOD;
        idm += 2;
      }
      kp -= 2;
    }
  } else {
    sort(all(minus));
    ans = 1;
    int idp = 0;
    int idm = 0;
    int kp = k;
    while (kp > 0) {
      ll vsp = 0;
      ll vsm = 0;
      if (idp + 1 < np)
        vsp = plus[idp] * plus[idp + 1];
      if (idm + 1 < nm)
        vsm = minus[idm] * minus[idm + 1];
      if (vsp >= vsm) {
        vsp %= MOD;
        ans *= vsp;
        ans %= MOD;
        idp += 2;
      } else {
        vsm %= MOD;
        ans *= vsm;
        ans %= MOD;
        idm += 2;
      }
      kp -= 2;
    }
  }

  prt(ans);
}