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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 10
#else
#define SIZE 223450
#endif

int N, Q, K;
int A[SIZE];

template <int MOD> struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt 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 ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1)
      r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
typedef ModInt<1000000007> mint;

struct Item {
  int v;
  bool operator<(const Item &that) const { return abs(v) < abs(that.v); }
};

int check() {
  int ret = -(1e18 / 2);
  REP(i, 1 << N) {
    bitset<60> b(i);
    if (b.count() == K) {
      int a = 1;
      REP(j, N) if (b[j]) { a *= A[i]; }
      chmax(ret, a);
    }
  }
  return ret;
}

void solve() {
  cin >> N >> K;
  REP(i, N) cin >> A[i];
  vector<int> minus, plus;
  REP(i, N) {
    if (A[i] < 0) {
      minus.push_back(A[i]);
    } else {
      plus.push_back(A[i]);
    }
  }
  sort(A, A + N);
  // 絶対値小さい順
  sort(ALL(minus));
  reverse(ALL(minus));
  sort(ALL(plus));

  int d = K - plus.size();
  if (d % 2) {
    // 奇数→全部マイナスだとまずい
    if (d < (int)minus.size() && plus.size() > 0) {
      // 正にできる
    } else {
      // 負になる→絶対値の小さいものを使う
      mint ans = -1;
      REP(i, N) A[i] = abs(A[i]);
      sort(A, A + N);
      REP(i, K) ans *= A[i];
      cout << ans << endl;
      return;
    }
  }
  // 絶対値の大きなものをとる
  mint ans = 1;
  while (K) {
    if (K % 2 && plus.size()) {
      ans *= plus.back();
      debug("* %lld -> ans: %lld\\n", plus.back(), ans);
      plus.pop_back();
      K--;
    } else if (K >= 2 && minus.size() >= 2) {
      auto it = minus.end();
      int mlast = (*--it);
      int mlast2 = (*--it);
      if (plus.size() <= 1) {
        ans *= mlast;
        ans *= mlast2;
        minus.pop_back();
        minus.pop_back();
        K -= 2;
        continue;
      }
      auto itp = plus.end();
      int plast = (*--itp);
      int plast2 = (*--itp);
      if (mlast * mlast2 > plast * plast2) {
        ans *= mlast;
        ans *= mlast2;
        debug("%lld * %lld -> ans: %lld\\n", mlast, mlast2, ans);
        minus.pop_back();
        minus.pop_back();
      } else {
        ans *= plast;
        ans *= plast2;
        debug("%lld * %lld -> ans: %lld\\n", plast, plast2, ans);
        plus.pop_back();
        plus.pop_back();
      }
      K -= 2;
    } else {
      ans *= plus.back();
      debug("* %lld -> ans: %lld\\n", plus.back(), ans);
      plus.pop_back();
      K--;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 10
#else
#define SIZE 223450
#endif

int N, Q, K;
int A[SIZE];

template <int MOD> struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt 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 ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1)
      r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
typedef ModInt<1000000007> mint;

struct Item {
  int v;
  bool operator<(const Item &that) const { return abs(v) < abs(that.v); }
};

void solve() {
  cin >> N >> K;
  REP(i, N) cin >> A[i];
  vector<int> minus, plus;
  REP(i, N) {
    if (A[i] < 0) {
      minus.push_back(A[i]);
    } else {
      plus.push_back(A[i]);
    }
  }
  sort(A, A + N);
  sort(ALL(minus));
  sort(ALL(plus));

  int d = K - plus.size();
  if (d % 2) {
    // 奇数→全部マイナスだとまずい
    if (d < minus.size() && plus.size() > 0) {
      // 正にできる
    } else {
      // 負になる→絶対値の小さいものを使う
      mint ans = -1;
      REP(i, N) A[i] = abs(A[i]);
      sort(A, A + N);
      REP(i, K) ans *= A[i];
      cout << ans << endl;
      return;
    }
  }
  // 絶対値の大きなものをとる
  mint ans = 1;
  while (K) {
    if (K >= 2 && minus.size() >= 2) {
      auto it = minus.end();
      int mlast = (*--it);
      int mlast2 = (*--it);
      if (plus.size() <= 1) {
        ans *= mlast;
        ans *= mlast2;
        minus.pop_back();
        minus.pop_back();
        continue;
      }
      auto itp = plus.end();
      int plast = (*--itp);
      int plast2 = (*--itp);
      if (mlast * mlast2 > plast * plast2) {
        ans *= mlast;
        ans *= mlast2;
        debug("%lld * %lld -> ans: %lld\\n", mlast, mlast2, ans);
        minus.pop_back();
        minus.pop_back();
      } else {
        ans *= plast;
        ans *= plast2;
        debug("%lld * %lld -> ans: %lld\\n", plast, plast2, ans);
        plus.pop_back();
        plus.pop_back();
      }
      K -= 2;
    } else {
      ans *= plus.back();
      debug("* %lld -> ans: %lld\\n", plus.back(), ans);
      plus.pop_back();
      K--;
    }
  }
  cout << ans << endl;
}