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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, k;
  cin >> n >> k;

  vi am, ap;
  rep(i, n) {
    int num;
    cin >> num;
    if (num < 0)
      am.push_back(-num);
    else
      ap.push_back(num);
  }

  sort(all(ap));
  sort(all(am));

  ll ans = 1;

  if (n == k) {
    int l = ap.size();
    rep(i, l) ans = (ans * ap[i]) % MOD;
    l = am.size();
    rep(i, l) ans = (ans * am[i]) % MOD;
    if (l % 2)
      ans = (-ans + MOD) % MOD;

    cout << ans << "\\n";
    return 0;
  }

  if ((0 == ap.size()) && k % 2) {
    rep(i, k) ans = (ans * am[i]) % MOD;
    ans = (-ans + MOD) % MOD;

    cout << ans << "\\n";
    return 0;
  }

  int ip = ap.size() - 1, im = am.size() - 1;

  rep(i, k) {
    if ((k - 1 - i) % 2 == 0 || im < 1) {
      cerr << "a\\n";
      ans = (ans * ap[ip]) % MOD;
      ip--;
    } else if (ip < 1) {
      cerr << "b\\n";
      ans = (ans * am[im]) % MOD;
      ans = (ans * am[im - 1]) % MOD;
      im -= 2;
      i++;
    } else {
      if (((ll)ap[ip]) * ap[ip - 1] > ((ll)am[im]) * am[im - 1]) {
        cerr << "c\\n";
        ans = (ans * ap[ip]) % MOD;
        ans = (ans * ap[ip - 1]) % MOD;
        ip -= 2;
        i++;
      } else {
        cerr << "d\\n";
        ans = (ans * am[im]) % MOD;
        ans = (ans * am[im - 1]) % MOD;
        im -= 2;
        i++;
      }
    }
    cerr << "i=" << i << ",ans=" << ans << "\\n";
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0
d
i=1,ans=12


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, k;
  cin >> n >> k;

  vi am, ap;
  rep(i, n) {
    int num;
    cin >> num;
    if (num < 0)
      am.push_back(-num);
    else
      ap.push_back(num);
  }

  sort(all(ap));
  sort(all(am));

  ll ans = 1;

  if (n == k) {
    int l = ap.size();
    rep(i, l) ans = (ans * ap[i]) % MOD;
    l = am.size();
    rep(i, l) ans = (ans * am[i]) % MOD;
    if (l % 2)
      ans = (-ans + MOD) % MOD;

    cout << ans << "\\n";
    return 0;
  }

  if ((0 == ap.size()) && k % 2) {
    rep(i, k) ans = (ans * am[i]) % MOD;
    ans = (-ans + MOD) % MOD;

    cout << ans << "\\n";
    return 0;
  }

  int ip = ap.size() - 1, im = am.size() - 1;

  rep(i, k) {
    if ((k - 1 - i) % 2 == 0 || im < 1) {
      cerr << "a\\n";
      ans = (ans * ap[ip]) % MOD;
      ip--;
    } else if (ip == 0) {
      cerr << "b\\n";
      ans = (ans * am[im]) % MOD;
      ans = (ans * am[im - 1]) % MOD;
      im -= 2;
      i++;
    } else {
      if (((ll)ap[ip]) * ap[ip - 1] > ((ll)am[im]) * am[im - 1]) {
        cerr << "c\\n";
        ans = (ans * ap[ip]) % MOD;
        ip--;
      } else {
        cerr << "d\\n";
        ans = (ans * am[im]) % MOD;
        ans = (ans * am[im - 1]) % MOD;
        im -= 2;
        i++;
      }
    }
    cerr << "i=" << i << ",ans=" << ans << "\\n";
  }

  cout << ans << "\\n";

  return 0;
}