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

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007;

int main(void) {
  bool has_zero = false;
  int N, K;
  cin >> N >> K;
  vector<int> ap, am;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a > 0)
      ap.push_back(a);
    else if (a < 0)
      am.push_back(a);
    else
      has_zero = true;
  }

  sort(ap.begin(), ap.end(), greater<int>());
  sort(am.begin(), am.end());

  long long ans = 1;
  int psize = ap.size();
  int msize = am.size();
  int pidx = 0;
  int midx = 0;

  if (K > psize + msize) {
    assert(has_zero);
    cout << 0 << endl;
    return 0;
  } else if (K == psize + msize) {
    if (msize % 2 == 1) {
      if (has_zero) {
        cout << 0 << endl;
        return 0;
      }
    }
    for (int i = 0; i < psize; i++) {
      ans *= ap.at(i);
      ans %= MOD;
    }
    for (int i = 0; i < msize; i++) {
      ans *= am.at(i);
      if (ans < 0)
        ans = (ans % MOD + MOD) % MOD;
      else
        ans %= MOD;
    }

    cout << ans << endl;
    return 0;
  }

  /* 負の数しかない場合、特別扱いする */
  if (ap.empty()) {
    if (K % 2 == 1) {
      if (has_zero) {
        cout << 0 << endl;
        return 0;
      }
      midx = msize - 1;
      while (K > 0) {
        ans *= am.at(midx);
        if (ans < 0)
          ans = (ans % MOD + MOD) % MOD;
        else
          ans %= MOD;
        midx--;
        K--;
      }
    } else {
      while (K > 0) {
        ans *= ((long long)am.at(midx) * am.at(midx + 1)) % MOD;
        ans %= MOD;
        midx += 2;
        K -= 2;
      }
    }
    cout << ans << endl;
    return 0;
  }

  if (K % 2 != 0) {
    ans = ap.at(0);
    pidx = 1;
    K--;
  }

  while (K > 0) {
    long long p;
    long long m;

    if (pidx >= psize - 1) {
      assert(midx < msize - 1);
      ans *= ((long long)am.at(midx) * am.at(midx + 1)) % MOD;
      ans %= MOD;
      midx += 2;
      goto NLOOP;
    }
    if (midx >= msize - 1) {
      assert(pidx < psize - 1);
      ans *= ((long long)ap.at(pidx) * ap.at(pidx + 1)) % MOD;
      ans %= MOD;
      pidx += 2;
      goto NLOOP;
    }

    p = (long long)ap.at(pidx) * ap.at(pidx + 1);
    m = (long long)am.at(midx) * am.at(midx + 1);

    if (p > m) {
      ans *= p % MOD;
      ans %= MOD;
      pidx += 2;
    } else {
      ans *= m % MOD;
      ans %= MOD;
      midx += 2;
    }
  NLOOP:
    K -= 2;
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007;

int main(void) {
  bool has_zero = false;
  int N, K;
  cin >> N >> K;
  vector<int> ap, am;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a > 0)
      ap.push_back(a);
    else if (a < 0)
      am.push_back(a);
    else
      has_zero = true;
  }

  sort(ap.begin(), ap.end(), greater<int>());
  sort(am.begin(), am.end());

  long long ans = 1;
  int psize = ap.size();
  int msize = am.size();
  int pidx = 0;
  int midx = 0;

  /* 負の数しかない場合、特別扱いする */
  if (ap.empty()) {
    if (K % 2 == 1) {
      if (has_zero) {
        cout << 0 << endl;
        return 0;
      }
      midx = msize - 1;
      while (K > 0) {
        ans *= am.at(midx);
        if (ans < 0)
          ans = (ans % MOD + MOD) % MOD;
        else
          ans %= MOD;
        midx--;
        K--;
      }
    } else {
      while (K > 0) {
        ans *= ((long long)am.at(midx) * am.at(midx + 1)) % MOD;
        ans %= MOD;
        midx += 2;
        K -= 2;
      }
    }
    cout << ans << endl;
    return 0;
  }

  if (K > psize + msize) {
    assert(has_zero);
    cout << 0 << endl;
    return 0;
  } else if (K == psize + msize) {
    if (msize % 2 == 1) {
      if (has_zero) {
        cout << 0 << endl;
        return 0;
      }
    }
    for (int i = 0; i < psize; i++) {
      ans *= ap.at(i);
      ans %= MOD;
    }
    for (int i = 0; i < msize; i++) {
      ans *= am.at(i);
      if (ans < 0)
        ans = (ans % MOD + MOD) % MOD;
      else
        ans %= MOD;
    }

    cout << ans << endl;
    return 0;
  }

  if (K % 2 != 0) {
    ans = ap.at(0);
    pidx = 1;
    K--;
  }

  while (K > 0) {
    long long p;
    long long m;

    if (pidx >= psize - 1) {
      assert(midx < msize - 1);
      ans *= ((long long)am.at(midx) * am.at(midx + 1)) % MOD;
      ans %= MOD;
      midx += 2;
      goto NLOOP;
    }
    if (midx >= msize - 1) {
      assert(pidx < psize - 1);
      ans *= ((long long)ap.at(pidx) * ap.at(pidx + 1)) % MOD;
      ans %= MOD;
      pidx += 2;
      goto NLOOP;
    }

    p = (long long)ap.at(pidx) * ap.at(pidx + 1);
    m = (long long)am.at(midx) * am.at(midx + 1);

    if (p > m) {
      ans *= p % MOD;
      ans %= MOD;
      pidx += 2;
    } else {
      ans *= m % MOD;
      ans %= MOD;
      midx += 2;
    }
  NLOOP:
    K -= 2;
  }

  cout << ans << endl;
  return 0;
}