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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

// mod
int mod = 1e9 + 7;

struct mint {
  unsigned x;

  mint() : x(0) {}

  mint(signed sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  mint(signed long long sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  int get() const { return (int)x; }

  mint &operator+=(mint that) {
    if ((x += that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(mint that) {
    if ((x += mod - that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(mint that) {
    x = (unsigned long long)x * that.x % mod;
    return *this;
  }

  mint &operator/=(mint that) { return *this *= that.inverse(); }

  mint operator+(mint that) const { return mint(*this) += that; }

  mint operator-(mint that) const { return mint(*this) -= that; }

  mint operator*(mint that) const { return mint(*this) *= that; }

  mint operator/(mint that) const { return mint(*this) /= that; }

  mint inverse() const {
    long long a = x, b = mod, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return mint(u);
  }

  bool operator==(mint that) const { return x == that.x; }

  bool operator!=(mint that) const { return x != that.x; }

  mint operator-() const {
    mint t;
    t.x = x == 0 ? 0 : mod - x;
    return t;
  }
};

class EMultiplication4 {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    REP(i, N) cin >> A[i];

    vector<int> plus, minus;
    int zero = 0;
    REP(i, N) {
      if (A[i] > 0)
        plus.push_back(A[i]);
      if (A[i] < 0)
        minus.push_back(-A[i]);
    }
    sort(plus.rbegin(), plus.rend());
    sort(minus.rbegin(), minus.rend());
    mint ans = 1;
    if (plus.size() + minus.size() / 2 * 2 >= K &&
        (K % 2 == 0 || plus.size() > 0)) {
      int p = 0;
      if (K & 1) {
        ans = plus[p++];
      }
      vector<int> v;
      for (; p + 1 < plus.size(); p += 2)
        v.push_back(plus[p] * plus[p + 1]);
      for (int m = 0; m + 1 < minus.size(); m += 2)
        v.push_back(minus[m] * minus[m + 1]);
      sort(v.rbegin(), v.rend());
      REP(i, K / 2) ans *= v[i];
    } else {
      REP(i, N) A[i] = abs(A[i]);
      sort(A.begin(), A.end());
      REP(i, K) ans *= A[i];
      ans *= -1;
    }
    cout << ans.get() << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  EMultiplication4 solver;
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

// mod
int mod = 1e9 + 7;

struct mint {
  unsigned x;

  mint() : x(0) {}

  mint(signed sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  mint(signed long long sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  int get() const { return (int)x; }

  mint &operator+=(mint that) {
    if ((x += that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(mint that) {
    if ((x += mod - that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(mint that) {
    x = (unsigned long long)x * that.x % mod;
    return *this;
  }

  mint &operator/=(mint that) { return *this *= that.inverse(); }

  mint operator+(mint that) const { return mint(*this) += that; }

  mint operator-(mint that) const { return mint(*this) -= that; }

  mint operator*(mint that) const { return mint(*this) *= that; }

  mint operator/(mint that) const { return mint(*this) /= that; }

  mint inverse() const {
    long long a = x, b = mod, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return mint(u);
  }

  bool operator==(mint that) const { return x == that.x; }

  bool operator!=(mint that) const { return x != that.x; }

  mint operator-() const {
    mint t;
    t.x = x == 0 ? 0 : mod - x;
    return t;
  }
};

class EMultiplication4 {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    REP(i, N) cin >> A[i];

    vector<int> plus, minus;
    int zero = 0;
    REP(i, N) {
      if (A[i] > 0)
        plus.push_back(A[i]);
      if (A[i] < 0)
        minus.push_back(A[i]);
      if (A[i] == 0)
        zero++;
    }

    if (minus.size() == N && K % 2 == 1) {
      sort(minus.rbegin(), minus.rend());
      mint res = 1;
      REP(i, K) { res *= minus[i]; }
      cout << res.get() << endl;
      return;
    }
    if (plus.size() + minus.size() < K) {
      cout << 0 << endl;
      return;
    }
    if (N == K) {
      mint res = 1;
      REP(i, K) { res *= A[i]; }
      cout << res.get() << endl;
      return;
    }

    sort(plus.rbegin(), plus.rend());
    sort(minus.begin(), minus.end());
    vector<int> res;
    int p = 0, m = 0;
    if (K & 1) {
      res.push_back(plus[p++]);
      K--;
    }
    while (res.size() < K) {
      if (p + 1 < plus.size() && m + 1 < minus.size()) {
        if (plus[p] * plus[p + 1] > minus[m] * minus[m + 1]) {
          res.push_back(plus[p]);
          res.push_back(plus[p + 1]);
          p += 2;
        } else {
          res.push_back(minus[m]);
          res.push_back(minus[m + 1]);
          m += 2;
        }
      } else if (m + 1 < minus.size()) {
        res.push_back(minus[m]);
        res.push_back(minus[m + 1]);
        m += 2;
      } else if (p < plus.size()) {
        res.push_back(plus[p++]);
      } else {
        res.push_back(0);
      }
    }

    mint ans = 1;
    for (auto n : res)
      ans *= n;
    cout << ans.get() << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  EMultiplication4 solver;
  solver.solve(in, out);
  return 0;
}