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


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

ostream &operator<<(ostream &os, const mint a) {
  os << a.x;

  return os;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N, K;
  cin >> N >> K;

  vll neg, pos;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a >= 0)
      pos.pb(a);
    else
      neg.pb(a);
  }

  if (N == K) {
    mint ans = 1;
    REP(i, pos.size()) ans *= pos[i];
    REP(i, neg.size()) ans *= neg[i];

    print(ans);
    return 0;
  }

  if (pos.size() == 0) {
    mint ans = 1;

    if (K % 2 == 0) {
      sort(ALL(neg));
      REP(i, K) ans *= neg[i];
    } else {
      sort(ALL(neg), greater<>());
      REP(i, K) ans *= neg[i];
    }

    print(ans);
    return 0;
  }

  sort(ALL(pos), greater<>());
  sort(ALL(neg));

  int ip = 0, in = 0;

  mint ans = 1;
  if (K % 2 == 1) {
    ans *= pos[ip++];
    K--;
  }

  while (K > 0) {
    ll p1 = 0;
    if (ip + 1 < pos.size())
      p1 = pos[ip] * pos[ip + 1];

    ll p2 = 0;
    if (in + 1 < neg.size())
      p2 = neg[in] * neg[in + 1];

    if (p1 >= p2) {
      ans *= p1;
      ip += 2;
    } else {
      ans *= p2;
      in += 2;
    }

    K -= 2;
  }

  print(mint(ans).x);
}


Error: 0

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

ostream &operator<<(ostream &os, const mint a) {
  os << a.x;

  return os;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N, K;
  cin >> N >> K;

  vll neg, pos;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a >= 0)
      pos.pb(a);
    else
      neg.pb(a);
  }

  if (N == K) {
    mint ans = 1;
    REP(i, pos.size()) ans *= pos[i];
    REP(i, neg.size()) ans *= pos[i];

    print(ans);
    return 0;
  }

  if (pos.size() == 0) {
    mint ans = 1;
    sort(ALL(neg), greater<>());

    REP(i, K) ans *= neg[i];
    print(ans);
    return 0;
  }

  sort(ALL(pos), greater<>());
  sort(ALL(neg));

  int ip = 0, in = 0;

  mint ans = 1;
  if (K % 2 == 1) {
    ans *= pos[ip++];
    K--;
  }

  while (K > 0) {
    ll p1 = 1;
    if (ip + 1 < pos.size())
      p1 = pos[ip] * pos[ip + 1];

    ll p2 = 1;
    if (in + 1 < neg.size())
      p2 = neg[in] * neg[in + 1];

    if (p1 >= p2) {
      ans *= p1;
      ip += 2;
    } else {
      ans *= p2;
      in += 2;
    }

    K -= 2;
  }

  print(mint(ans).x);
}