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

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

const int MOD = 1000000007;
class ModInt {
public:
  int64_t x;
  ModInt(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt a) const { return ModInt(*this) += a; }
  ModInt operator-(const ModInt a) const { return ModInt(*this) -= a; }
  ModInt operator*(const ModInt a) const { return ModInt(*this) *= a; }
  ModInt pow(int64_t n) const {
    if (n == 0)
      return 1;
    ModInt t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  ModInt inv() const { return pow(MOD - 2); }
  ModInt &operator/=(const ModInt a) { return *this *= a.inv(); }
  ModInt operator/(const ModInt a) const { return ModInt(*this) /= a; }
};
istream &operator>>(istream &is, const ModInt &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x; }

void solve(int N, int K, vector<int> A) {
  if (N == K) {
    ModInt res = 1;
    for (int i = 0; i < N; i++) {
      res *= A.at(i);
    }
    cout << res << endl;
    return;
  }

  vector<int> pos, neg;
  for (int i = 0; i < N; i++) {
    if (A.at(i) > 0)
      pos.push_back(A.at(i));
    else if (A.at(i) < 0)
      neg.push_back(-A.at(i));
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());

  int ip = 0, in = 0;
  while (ip + in < K) {
    if (ip < (int)pos.size() && in < (int)neg.size()) {
      int ap = pos.at(ip), an = neg.at(in);
      if (ap > an)
        ip++;
      else
        in++;
    } else if (ip < (int)pos.size()) {
      ip++;
    } else if (in < (int)neg.size()) {
      in++;
    } else {
      cout << 0 << endl;
      return;
    }
  }

  if (in % 2 != 0) {
    if (ip < (int)pos.size() && in < (int)neg.size()) {
      if (ip > 0) {
        int64_t mp = (int64_t)pos.at(ip - 1) * pos.at(ip);
        int64_t mn = (int64_t)neg.at(in - 1) * neg.at(in);
        if (mp > mn) {
          ip++;
          in--;
        } else {
          ip--;
          in++;
        }
      } else {
        ip++;
        in--;
      }
    } else if (ip < (int)pos.size()) {
      ip++;
      in--;
    } else if (in < (int)neg.size()) {
      if (ip > 0) {
        ip--;
        in++;
      } else {
        if ((int)neg.size() == N) {
          sort(neg.begin(), neg.end());
          ModInt res = 1;
          for (int i = 0; i < K; i++) {
            res *= -neg.at(i);
          }
          cout << res << endl;
          return;
        } else {
          cout << 0 << endl;
          return;
        }
      }
    } else {
      cout << 0 << endl;
      return;
    }
  }

  ModInt res = 1;
  for (int i = 0; i < ip; i++) {
    res *= pos.at(i);
  }
  for (int i = 0; i < in; i++) {
    res *= neg.at(i);
  }
  cout << res << endl;
}

int main() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  solve(N, K, move(A));
  return 0;
}


Error: 0

Fixed submission:

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

const int MOD = 1000000007;
class ModInt {
public:
  int64_t x;
  ModInt(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt a) const { return ModInt(*this) += a; }
  ModInt operator-(const ModInt a) const { return ModInt(*this) -= a; }
  ModInt operator*(const ModInt a) const { return ModInt(*this) *= a; }
  ModInt pow(int64_t n) const {
    if (n == 0)
      return 1;
    ModInt t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  ModInt inv() const { return pow(MOD - 2); }
  ModInt &operator/=(const ModInt a) { return *this *= a.inv(); }
  ModInt operator/(const ModInt a) const { return ModInt(*this) /= a; }
};
istream &operator>>(istream &is, const ModInt &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x; }

void solve(int N, int K, vector<int> A) {
  if (N == K) {
    ModInt res = 1;
    for (int i = 0; i < N; i++) {
      res *= A.at(i);
    }
    cout << res << endl;
    return;
  }

  vector<int> pos, neg;
  for (int i = 0; i < N; i++) {
    if (A.at(i) > 0)
      pos.push_back(A.at(i));
    else if (A.at(i) < 0)
      neg.push_back(-A.at(i));
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());

  if (pos.size() == 0) {
    if ((int)neg.size() == N) {
      sort(neg.begin(), neg.end());
      ModInt res = 1;
      for (int i = 0; i < K; i++) {
        res *= -neg.at(i);
      }
      cout << res << endl;
      return;
    } else {
      cout << 0 << endl;
      return;
    }
  }

  int ip = 0, in = 0;
  while (ip + in < K) {
    if (ip < (int)pos.size() && in < (int)neg.size()) {
      int ap = pos.at(ip), an = neg.at(in);
      if (ap > an)
        ip++;
      else
        in++;
    } else if (ip < (int)pos.size()) {
      ip++;
    } else if (in < (int)neg.size()) {
      in++;
    } else {
      cout << 0 << endl;
      return;
    }
  }

  if (in % 2 != 0) {
    if (ip < (int)pos.size() && in < (int)neg.size()) {
      if (ip > 0) {
        int64_t mp = (int64_t)pos.at(ip - 1) * pos.at(ip);
        int64_t mn = (int64_t)neg.at(in - 1) * pos.at(in);
        if (mp > mn) {
          ip++;
          in--;
        } else {
          ip--;
          in++;
        }
      } else {
        ip++;
        in--;
      }
    } else if (ip < (int)pos.size()) {
      ip++;
      in--;
    } else if (in < (int)neg.size()) {
      ip--;
      in++;
    } else {
      cout << 0 << endl;
      return;
    }
  }

  ModInt res = 1;
  for (int i = 0; i < ip; i++) {
    res *= pos.at(i);
  }
  for (int i = 0; i < in; i++) {
    res *= neg.at(i);
  }
  cout << res << endl;
}

int main() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  solve(N, K, move(A));
  return 0;
}