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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

template <int mod> struct Modint {
  int x;
  Modint() : x(0) {}
  Modint(int64_t y) : x((y % mod + mod) % mod) {}

  Modint &operator+=(const Modint &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  Modint &operator-=(const Modint &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  Modint &operator*=(const Modint &p) {
    x = (1LL * x * p.x) % mod;
    return *this;
  }

  Modint &operator/=(const Modint &p) {
    *this *= p.inverse();
    return *this;
  }

  Modint operator-() const { return Modint(-x); }
  Modint operator+(const Modint &p) const { return Modint(*this) += p; }
  Modint operator-(const Modint &p) const { return Modint(*this) -= p; }
  Modint operator*(const Modint &p) const { return Modint(*this) *= p; }
  Modint operator/(const Modint &p) const { return Modint(*this) /= p; }

  bool operator==(const Modint &p) const { return x == p.x; }
  bool operator!=(const Modint &p) const { return x != p.x; }

  Modint inverse() const { // 非再帰拡張ユークリッド
    int a = x, b = mod, u = 1, v = 0;
    while (b > 0) {
      int t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return Modint(u);
  }

  Modint pow(int64_t n) const { // 繰り返し二乗法
    Modint ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Modint &p) { return os << p.x; }
};

using modint = Modint<mod>;

using P = pair<ll, ll>;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  modint ans = 1;
  if (n == k || k == 1) {
    sort(rall(a));
    rep(i, k) { ans *= a[i]; }
    cout << ans;
    return;
  }
  vector<P> b(n);
  rep(i, n) { b[i] = {abs(a[i]), a[i] < 0}; }
  sort(rall(b));
  int cnt = 0;
  rep(i, k) {
    if (b[i].second == 1)
      ++cnt;
  }
  if (cnt % 2 == 0) {
    rep(i, k) { ans *= b[i].first; }
    cout << ans;
    return;
  }
  int l = -1, r = -1, s = -1, t = -1;
  for (int i = k - 1; i >= 0; --i) {
    if (b[i].second == 1) {
      l = i;
      break;
    }
  }
  REP(i, k, n) {
    if (b[i].second == 0) {
      r = i;
      break;
    }
  }
  for (int i = k - 1; i >= 0; --i) {
    if (b[i].second == 0) {
      s = i;
      break;
    }
  }
  REP(i, k, n) {
    if (b[i].second == 1) {
      t = i;
      break;
    }
  }
  if (l != -1 && r != -1 && s != -1 && t != -1) {
    if (b[l].first * b[t].first <= b[r].first * b[s].first) {
      ans *= b[r].first;
      rep(i, k) {
        if (l == i)
          continue;
        ans *= b[i].first;
      }
    } else {
      ans *= b[t].first;
      rep(i, k) {
        if (s == i)
          continue;
        ans *= b[i].first;
      }
    }
    cout << ans;
    return;
  } else if (l != -1 && r != -1) {
    ans *= b[r].first;
    rep(i, k) {
      if (l == i)
        continue;
      ans *= b[i].first;
    }
    cout << ans;
    return;
  } else if (s != -1 && t != -1) {
    ans *= b[t].first;
    rep(i, k) {
      if (s == i)
        continue;
      ans *= b[i].first;
    }
    cout << ans;
    return;
  } else {
    sort(all(b));
    rep(i, k) ans *= b[i].first;
    ans = -ans;
    cout << ans;
    return;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

template <int mod> struct Modint {
  int x;
  Modint() : x(0) {}
  Modint(int64_t y) : x((y % mod + mod) % mod) {}

  Modint &operator+=(const Modint &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  Modint &operator-=(const Modint &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  Modint &operator*=(const Modint &p) {
    x = (1LL * x * p.x) % mod;
    return *this;
  }

  Modint &operator/=(const Modint &p) {
    *this *= p.inverse();
    return *this;
  }

  Modint operator-() const { return Modint(-x); }
  Modint operator+(const Modint &p) const { return Modint(*this) += p; }
  Modint operator-(const Modint &p) const { return Modint(*this) -= p; }
  Modint operator*(const Modint &p) const { return Modint(*this) *= p; }
  Modint operator/(const Modint &p) const { return Modint(*this) /= p; }

  bool operator==(const Modint &p) const { return x == p.x; }
  bool operator!=(const Modint &p) const { return x != p.x; }

  Modint inverse() const { // 非再帰拡張ユークリッド
    int a = x, b = mod, u = 1, v = 0;
    while (b > 0) {
      int t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return Modint(u);
  }

  Modint pow(int64_t n) const { // 繰り返し二乗法
    Modint ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Modint &p) { return os << p.x; }
};

using modint = Modint<mod>;

using P = pair<ll, ll>;
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<P> b(n);
  rep(i, n) cin >> a[i], b[i] = {abs(a[i]), a[i] < 0};
  sort(rall(b));
  rep(i, k) {
    if (b[i].first == 0) {
      cout << 0;
      return;
    }
  }
  int x;
  rep(i, n) {
    if (b[i].second == 1)
      ++x;
  }
  modint ans = 1;
  int cnt = 0;
  rep(i, k) {
    if (b[i].second == 1)
      ++cnt;
  }
  if (cnt % 2 == 0) {
    rep(i, k) ans *= b[i].first;
  } else {
    ll mul = -INF;
    if (b[k - 1].second == 1) {
      REP(i, k, n) {
        if (b[i].second == 0) {
          mul = b[i].first;
          break;
        }
      }
    } else {
      REP(i, k, n) {
        if (b[i].second == 1) {
          mul = b[i].first;
          break;
        }
      }
    }
    if (mul != -INF) {
      ans *= mul;
      bool flag = true;
      for (int i = k - 1; i >= 0; --i) {
        if (flag && b[i].second == 1) {
          flag = false;
          continue;
        }
        ans *= b[i].first;
      }
    } else {
      assert(x % 2 == 1);
      sort(all(b));
      rep(i, k) { ans *= b[i].first; }
      ans = -ans;
    }
  }

  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}