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

#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \\
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << (first) << ", " << (last) << ")" << endl;                            \\
  for (int _i = (first); _i < (last); ++_i) {                                  \\
    cout << #a << "[" << _i << "] = " << (a)[_i] << endl;                      \\
  }
#define PRINTP(p)                                                              \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #p << " = (" \\
       << (p.first) << ", " << (p.second) << ")" << endl;
#define PRINTI(a, i)                                                           \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl;
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#define PRINTP(p)
#define PRINTI(a, i)
#endif

#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 pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

template <class T> using vec2 = vector<vector<T>>;

template <class T> using vec3 = vector<vector<vector<T>>>;

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

template <class T, T M> class modulo {
public:
  T data;

  modulo(T data = 0) {
    this->data = data % M;
    if (this->data < 0) {
      this->data += M;
    }
  }

  modulo operator+(const modulo &x) const { return modulo(data + x.data); }

  modulo operator-(const modulo &x) const {
    return modulo(data + (M - x.data));
  }

  modulo operator*(const modulo &x) const { return modulo(data * x.data); }

  modulo operator/(const modulo &x) const {
    return modulo(data * x.inv().data);
  }

  modulo operator+=(const modulo &x) { return data = (data + x.data) % M; }

  modulo operator-=(const modulo &x) {
    return data = (data + (M - x.data)) % M;
  }

  modulo operator*=(const modulo &x) { return data = (data * x.data) % M; }

  modulo operator/=(const modulo &x) {
    return data = (data * x.inv().data) % M;
  }

  modulo inv() const { return _pow(M - 2); }

  template <class Integer> modulo operator^(Integer n) const {
    if (n >= 0) {
      return _pow(n);
    } else {
      return _pow(M - 1 - (-n) % (M - 1));
    }
  }

  friend std::ostream &operator<<(std::ostream &os, const modulo &x) {
    os << x.data;
    return os;
  }

private:
  template <class NonNegativeInteger> modulo _pow(NonNegativeInteger n) const {
    modulo result(1);
    modulo x(*this);
    while (n) {
      if (n & 1) {
        result *= x;
      }
      x *= x;
      n >>= 1;
    }
    return result;
  }
};

template <class T, T M> struct combinatorics {

  using Mod = modulo<T, M>;

  T N;
  std::vector<Mod> factorial;
  std::vector<Mod> inv_factorial;

  combinatorics(T N) : N(N), factorial(N + 1), inv_factorial(N + 1) {
    factorial[0] = 1;
    for (T i = 1; i <= N; ++i) {
      factorial[i] = factorial[i - 1] * i;
    }
    inv_factorial[N] = factorial[N].inv();
    for (T i = N; i >= 1; --i) {
      inv_factorial[i - 1] = inv_factorial[i] * i;
    }
  }

  Mod P(T n, T k) {
    if (0 <= k && k <= n) {
      return factorial[n] * inv_factorial[k];
    } else {
      return 0;
    }
  }

  Mod C(T n, T k) {
    if (0 <= k && k <= n) {
      return factorial[n] * inv_factorial[k] * inv_factorial[n - k];
    } else {
      return 0;
    }
  }

  Mod H(T n, T k) {
    if (n == 0 && k == 0) {
      return 1;
    } else {
      return C(n + k - 1, k);
    }
  }
};

using Mod = modulo<ll, MOD>;

Mod solve() {

  int N, K;
  cin >> N >> K;
  vector<pii> posneg;
  REP(i, N) {
    int A;
    cin >> A;
    if (A > 0) {
      posneg.pb(pii(A, 1));
    } else if (A < 0) {
      posneg.pb(pii(-A, -1));
    }
  }
  sort(rall(posneg));

  Mod result;

  if (posneg.size() >= K) {
    result = 1;
    ll last_pos = -1;
    ll last_neg = -1;
    int n_neg = 0;
    REP(i, K) {
      auto [x, sign] = posneg[i];
      result *= x;
      if (sign == 1) {
        last_pos = x;
      } else if (sign == -1) {
        last_neg = x;
        ++n_neg;
      }
    }
    PRINT(n_neg);
    if (n_neg % 2 == 0) {
      return result;
    }
    ll first_pos = -1;
    ll first_neg = -1;
    FOR(i, K, posneg.size()) {
      auto [x, sign] = posneg[i];
      if (sign == 1) {
        if (first_pos == -1) {
          first_pos = x;
        }
      } else if (sign == -1) {
        if (first_neg == -1) {
          first_neg = x;
        }
      }
    }
    ll a = -1;
    ll b = 1;
    ll c = -1;
    ll d = 1;
    if (last_pos != -1 && first_neg != -1) {
      a = first_neg;
      b = last_pos;
    }
    if (last_neg != -1 && first_pos != -1) {
      c = first_pos;
      d = last_neg;
    }
    if (a > 0 && c > 0) {
      // if (a / b > c / d) {
      if (a * d > b * c) {
        return result * a / b;
      } else {
        return result * c / d;
      }
    } else if (a > 0) {
      return result * a / b;
    } else if (c > 0) {
      return result * c / d;
    }
  }
  int n_zero = N - posneg.size();
  if (n_zero > 0) {
    return 0;
  }
  result = -1;
  REP(i, K) {
    ll x = posneg[posneg.size() - 1 - i].first;
    result *= x;
  }
  return result;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cout << solve() << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \\
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << (first) << ", " << (last) << ")" << endl;                            \\
  for (int _i = (first); _i < (last); ++_i) {                                  \\
    cout << #a << "[" << _i << "] = " << (a)[_i] << endl;                      \\
  }
#define PRINTP(p)                                                              \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #p << " = (" \\
       << (p.first) << ", " << (p.second) << ")" << endl;
#define PRINTI(a, i)                                                           \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl;
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#define PRINTP(p)
#define PRINTI(a, i)
#endif

#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 pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

template <class T> using vec2 = vector<vector<T>>;

template <class T> using vec3 = vector<vector<vector<T>>>;

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

template <class T, T M> class modulo {
public:
  T data;

  modulo(T data = 0) : data(data % M) {}

  modulo operator+(const modulo &x) const { return modulo(data + x.data); }

  modulo operator-(const modulo &x) const {
    return modulo(data + (M - x.data));
  }

  modulo operator*(const modulo &x) const { return modulo(data * x.data); }

  modulo operator/(const modulo &x) const {
    return modulo(data * x.inv().data);
  }

  modulo operator+=(const modulo &x) { return data = (data + x.data) % M; }

  modulo operator-=(const modulo &x) {
    return data = (data + (M - x.data)) % M;
  }

  modulo operator*=(const modulo &x) { return data = (data * x.data) % M; }

  modulo operator/=(const modulo &x) {
    return data = (data * x.inv().data) % M;
  }

  modulo inv() const { return _pow(M - 2); }

  template <class Integer> modulo operator^(Integer n) const {
    if (n >= 0) {
      return _pow(n);
    } else {
      return _pow(M - 1 - (-n) % (M - 1));
    }
  }

  friend std::ostream &operator<<(std::ostream &os, const modulo &x) {
    os << x.data;
    return os;
  }

private:
  template <class NonNegativeInteger> modulo _pow(NonNegativeInteger n) const {
    modulo result(1);
    modulo x(*this);
    while (n) {
      if (n & 1) {
        result *= x;
      }
      x *= x;
      n >>= 1;
    }
    return result;
  }
};

template <class T, T M> struct combinatorics {

  using Mod = modulo<T, M>;

  T N;
  std::vector<Mod> factorial;
  std::vector<Mod> inv_factorial;

  combinatorics(T N) : N(N), factorial(N + 1), inv_factorial(N + 1) {
    factorial[0] = 1;
    for (T i = 1; i <= N; ++i) {
      factorial[i] = factorial[i - 1] * i;
    }
    inv_factorial[N] = factorial[N].inv();
    for (T i = N; i >= 1; --i) {
      inv_factorial[i - 1] = inv_factorial[i] * i;
    }
  }

  Mod P(T n, T k) {
    if (0 <= k && k <= n) {
      return factorial[n] * inv_factorial[k];
    } else {
      return 0;
    }
  }

  Mod C(T n, T k) {
    if (0 <= k && k <= n) {
      return factorial[n] * inv_factorial[k] * inv_factorial[n - k];
    } else {
      return 0;
    }
  }

  Mod H(T n, T k) {
    if (n == 0 && k == 0) {
      return 1;
    } else {
      return C(n + k - 1, k);
    }
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  using Mod = modulo<ll, MOD>;

  int N, K;
  cin >> N >> K;
  vector<int> pos, neg;
  vector<pii> posneg;
  REP(i, N) {
    int A;
    cin >> A;
    if (A > 0) {
      pos.pb(A);
      posneg.pb(pii(A, 1));
    } else if (A < 0) {
      neg.pb(-A);
      posneg.pb(pii(-A, -1));
    }
  }

  sort(rall(pos));
  sort(rall(neg));
  sort(rall(posneg));

  int n_pos = pos.size();
  int n_neg = neg.size();
  int n_zero = N - n_pos - n_neg;

  Mod result;

  bool can_make_pos = false;

  for (int i = 0; i <= min(n_neg, K); i += 2) {
    if (n_pos >= K - i) {
      can_make_pos = true;
      break;
    }
  }

  PRINT(can_make_pos);

  if (can_make_pos) {
    if (K == 1) {
      result = pos[0];
    } else {
      result = 1;

      int cnt_pos = 0;
      int cnt_neg = 0;
      REP(i, K - 2) {
        result *= posneg[i].first;
        if (posneg[i].second == 1) {
          cnt_pos++;
        } else {
          cnt_neg++;
        }
      }
      vector<ll> pp;
      vector<ll> nn;
      vector<ll> pn;
      vector<ll> np;
      FOR(i, K - 2, N) {
        ll x = posneg[i].first;
        if (posneg[i].second == 1) {
          if (pp.size() < 2) {
            pp.pb(x);
          }
          if (pn.size() == 0) {
            pn.pb(x);
          }
          if (np.size() == 1) {
            np.pb(x);
          }
        } else {
          if (nn.size() < 2) {
            nn.pb(x);
          }
          if (pn.size() == 1) {
            pn.pb(x);
          }
          if (np.size() == 0) {
            np.pb(x);
          }
        }
      }
      PRINTA(pp, 0, pp.size());
      PRINTA(nn, 0, nn.size());
      PRINTA(pn, 0, pn.size());
      PRINTA(np, 0, np.size());
      ll maxi = 0;
      if (cnt_neg % 2 == 0) {
        if (pp.size() == 2) {
          ll tmp = pp[0] * pp[1];
          amax(maxi, pp[0] * pp[1]);
        }
        if (nn.size() == 2) {
          amax(maxi, nn[0] * nn[1]);
        }
      } else {
        if (pn.size() == 2) {
          amax(maxi, pn[0] * pn[1]);
        }
        if (np.size() == 2) {
          amax(maxi, np[0] * np[1]);
        }
      }
      assert(maxi > 0);
      // assert(pospos.size() >= 2 || posneg.size() >= 2 || negpos.size() >= 2
      // || negneg.size() >= 2);
      result *= maxi;
    }
  } else {
    if (n_zero > 0) {
      result = 0;
    } else {
      PRINT("NEGATIVE ANSWER");
      result = 0;
      result -= 1;
      REP(i, K) { result *= posneg[posneg.size() - 1 - i].first; }
    }
  }

  cout << result << endl;

  return 0;
}