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

#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cstring>

using namespace std;

using ll = long long;
using ull = unsigned long long;
ll const MM = 1000000007;
// ll const NN = 1000000007;

#define pv(val) cerr << #val << '=' << (val) << endl
#define pl cerr << '@' << __LINE__ << endl

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, b, e) for (ll i = (b); i < (e); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

template <class T> ostream &operator<<(ostream &os, vector<T> const &vec);
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr);
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p);
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t);

template <class T> ostream &operator<<(ostream &os, vector<T> const &vec) {
  if (vec.empty()) {
    os << "{}";
  } else {
    os << '{';
    for (size_t i = 0; i < vec.size() - 1; i++)
      os << vec[i] << ", ";
    os << vec.back() << '}';
  }
  return os;
}

template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr) {
  if (arr.empty()) {
    os << "{}";
  } else {
    os << '{';
    for (size_t i = 0; i < arr.size() - 1; i++)
      os << arr[i] << ", ";
    os << arr.back() << '}';
  }
  return os;
}

template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <size_t I, class... Args>
typename enable_if<sizeof...(Args) == (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
  os << get<I>(t);
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) != (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
  os << get<I>(t) << ", ";
  print_tuple<I + 1, Args...>(os, t);
}

template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t) {
  os << '(';
  print_tuple<0, Args...>(os, t);
  os << ')';

  return os;
}

struct Timer {
  using Clock = std::chrono::system_clock;
  Clock::time_point startTime;

  Timer() { start(); }
  void start() { startTime = Clock::now(); }

  long long milli() const {
    return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() -
                                                                 startTime)
        .count();
  }
  long long micro() const {
    return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() -
                                                                 startTime)
        .count();
  }
};

ll bound(function<bool(ll)> f, ll ng, ll ok) {
  while (abs(ng - ok) > 1) {
    ll mid = (ng + ok) / 2;
    if (f(mid))
      ok = mid;
    else
      ng = mid;
  }

  return ok;
}

ll order(vector<ll> const &r) {
  vector<ll> a(r.size());
  iota(all(a), 1);

  ll ord = 0;
  while (a != r) {
    next_permutation(all(a));
    ord++;
  }

  return ord;
}

ll div(ll n) {
  ll ans = 0;
  while (n > 0) {
    ans++;
    n /= 2;
  }

  return ans;
}

// value, weight
ll knapsack(vector<pair<ll, ll>> const &a, ll max_w) {
  ll max_n = a.size();
  vector<ll> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    ll v = a[i].first;
    ll w = a[i].second;
    if (w <= max_w && table[w] < v)
      table[w] = v;
    for (int j = 1; j <= max_w - w; j++) {
      if (table[j] != 0 && table[w + j] < table[j] + v)
        table[w + j] = table[j] + v;
    }
  }

  return *max_element(all(table));
}

ll llpow(ll n, ll m) {
  ll ans = 1;
  rep(i, m) {
    ans *= n;
    ans %= MM;
  }

  return ans;
}

Timer gTimer;

ll positive(vector<ll> p, vector<ll> n, ll k) {
  sort(all(p), [](ll a, ll b) { return abs(a) >= abs(b); });
  sort(all(n), [](ll a, ll b) { return abs(a) >= abs(b); });
  vector<pair<ll, ll>> a;
  for (ll b : p)
    a.push_back({b, 1});

  rep(i, n.size() / 2) a.push_back({n[2 * i] * n[2 * i + 1], 2});

  sort(rall(a));
  pv(a);
  ll ans = 1;
  rep(i, a.size()) {
    if (a[i].second <= k) {
      ans *= a[i].first;
      ans %= MM;
      k -= a[i].second;
    }
    if (k == 0)
      break;
  }
  if (k == 0) {
    return ans;
  } else {
    return -MM;
  }
}

ll negative(vector<ll> p, vector<ll> n, ll k) {
  if (n.empty())
    return -MM;
  sort(all(p), [](ll a, ll b) { return abs(a) <= abs(b); });
  sort(all(n), [](ll a, ll b) { return abs(a) <= abs(b); });

  vector<pair<ll, ll>> a;
  for (ll b : p)
    a.push_back({b, 1});

  ll ans = n[0];
  n.erase(n.begin());
  rep(i, n.size() / 2) a.push_back({n[2 * i] * n[2 * i + 1], 2});

  sort(rall(a));
  rep(i, a.size()) {
    if (a[i].second <= k) {
      ans *= a[i].first;
      ans %= MM;
      k -= a[i].second;
    }
    if (k == 0)
      break;
  }
  if (k == 0) {
    return -ans;
  } else {
    return -MM;
  }
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int main(void) {
  ll N, K;
  cin >> N >> K;

  vector<ll> p, n;
  ll zero = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a <= 0)
      n.push_back(a);
    else
      p.push_back(a);
  }

  if (N == K) {
    ll ans = 1;
    for (ll v : p) {
      ans *= v;
      ans %= MM;
    }
    for (ll v : n) {
      ans *= -v;
      ans %= MM;
    }

    if (n.size() % 2 == 1)
      ans = (-ans + MM) % MM;

    cout << ans << endl;

    return 0;
  }

  if (p.empty() && K % 2 == 1) {
    sort(rall(n));
    ll ans = 1;
    rep(i, K) {
      ans *= -n[i];
      ans %= MM;
    }
    cout << (-ans + MM) % MM << endl;

    return 0;
  }

  sort(all(p));
  sort(rall(n));

  vector<ll> up, un;

  ll ans = 1;
  rep(i, K) {
    ll const pp = p.empty() ? -MM : p.back();
    ll const nn = n.empty() ? -MM : -n.back();
    if (pp > nn) {
      up.push_back(pp);
      ans *= pp;
      ans %= MM;
      p.pop_back();
    } else {
      un.push_back(-nn);
      ans *= nn;
      ans %= MM;
      n.pop_back();
    }
  }
  pv(ans);

  if (un.size() % 2 == 1) {
    ll a = -1, b = -1, c = -1, d = -1;
    if (!p.empty()) {
      a = p.back();
      b = -un.back();
    }
    if (!up.empty() && !n.empty()) {
      c = -n.back();
      d = up.back();
    }
    if (a != -1 && c != -1)
      if (a * d < c * b)
        a = c, b = d;
    if (a == -1)
      a = c, b = d;
    ans *= a;
    ans %= MM;
    ans *= modinv(b, MM);
    ans %= MM;
  }

  cout << ans << endl;

  return 0;
}


Error: 0
ans=12


Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cstring>

using namespace std;

using ll = long long;
using ull = unsigned long long;
ll const MM = 1000000007;
// ll const NN = 1000000007;

#define pv(val) cerr << #val << '=' << (val) << endl
#define pl cerr << '@' << __LINE__ << endl

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, b, e) for (ll i = (b); i < (e); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

template <class T> ostream &operator<<(ostream &os, vector<T> const &vec);
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr);
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p);
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t);

template <class T> ostream &operator<<(ostream &os, vector<T> const &vec) {
  if (vec.empty()) {
    os << "{}";
  } else {
    os << '{';
    for (size_t i = 0; i < vec.size() - 1; i++)
      os << vec[i] << ", ";
    os << vec.back() << '}';
  }
  return os;
}

template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr) {
  if (arr.empty()) {
    os << "{}";
  } else {
    os << '{';
    for (size_t i = 0; i < arr.size() - 1; i++)
      os << arr[i] << ", ";
    os << arr.back() << '}';
  }
  return os;
}

template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <size_t I, class... Args>
typename enable_if<sizeof...(Args) == (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
  os << get<I>(t);
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) != (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
  os << get<I>(t) << ", ";
  print_tuple<I + 1, Args...>(os, t);
}

template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t) {
  os << '(';
  print_tuple<0, Args...>(os, t);
  os << ')';

  return os;
}

struct Timer {
  using Clock = std::chrono::system_clock;
  Clock::time_point startTime;

  Timer() { start(); }
  void start() { startTime = Clock::now(); }

  long long milli() const {
    return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() -
                                                                 startTime)
        .count();
  }
  long long micro() const {
    return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() -
                                                                 startTime)
        .count();
  }
};

ll bound(function<bool(ll)> f, ll ng, ll ok) {
  while (abs(ng - ok) > 1) {
    ll mid = (ng + ok) / 2;
    if (f(mid))
      ok = mid;
    else
      ng = mid;
  }

  return ok;
}

ll order(vector<ll> const &r) {
  vector<ll> a(r.size());
  iota(all(a), 1);

  ll ord = 0;
  while (a != r) {
    next_permutation(all(a));
    ord++;
  }

  return ord;
}

ll div(ll n) {
  ll ans = 0;
  while (n > 0) {
    ans++;
    n /= 2;
  }

  return ans;
}

// value, weight
ll knapsack(vector<pair<ll, ll>> const &a, ll max_w) {
  ll max_n = a.size();
  vector<ll> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    ll v = a[i].first;
    ll w = a[i].second;
    if (w <= max_w && table[w] < v)
      table[w] = v;
    for (int j = 1; j <= max_w - w; j++) {
      if (table[j] != 0 && table[w + j] < table[j] + v)
        table[w + j] = table[j] + v;
    }
  }

  return *max_element(all(table));
}

ll llpow(ll n, ll m) {
  ll ans = 1;
  rep(i, m) {
    ans *= n;
    ans %= MM;
  }

  return ans;
}

Timer gTimer;

ll positive(vector<ll> p, vector<ll> n, ll k) {
  sort(all(p), [](ll a, ll b) { return abs(a) >= abs(b); });
  sort(all(n), [](ll a, ll b) { return abs(a) >= abs(b); });
  vector<pair<ll, ll>> a;
  for (ll b : p)
    a.push_back({b, 1});

  rep(i, n.size() / 2) a.push_back({n[2 * i] * n[2 * i + 1], 2});

  sort(rall(a));
  pv(a);
  ll ans = 1;
  rep(i, a.size()) {
    if (a[i].second <= k) {
      ans *= a[i].first;
      ans %= MM;
      k -= a[i].second;
    }
    if (k == 0)
      break;
  }
  if (k == 0) {
    return ans;
  } else {
    return -MM;
  }
}

ll negative(vector<ll> p, vector<ll> n, ll k) {
  if (n.empty())
    return -MM;
  sort(all(p), [](ll a, ll b) { return abs(a) <= abs(b); });
  sort(all(n), [](ll a, ll b) { return abs(a) <= abs(b); });

  vector<pair<ll, ll>> a;
  for (ll b : p)
    a.push_back({b, 1});

  ll ans = n[0];
  n.erase(n.begin());
  rep(i, n.size() / 2) a.push_back({n[2 * i] * n[2 * i + 1], 2});

  sort(rall(a));
  rep(i, a.size()) {
    if (a[i].second <= k) {
      ans *= a[i].first;
      ans %= MM;
      k -= a[i].second;
    }
    if (k == 0)
      break;
  }
  if (k == 0) {
    return -ans;
  } else {
    return -MM;
  }
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int main(void) {
  ll N, K;
  cin >> N >> K;

  vector<ll> p, n;
  ll zero = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a < 0)
      n.push_back(a);
    else if (a == 0)
      zero++;
    else if (a > 0)
      p.push_back(a);
  }

  if (N == K) {
    ll ans = 1;
    for (ll v : p) {
      ans *= v;
      ans %= MM;
    }
    for (ll v : n) {
      ans *= -v;
      ans %= MM;
    }

    if (n.size() % 2 == 1)
      ans = -ans + MM;

    cout << ans << endl;

    return 0;
  }

  if (p.empty() && K % 2 == 1) {
    sort(rall(n));
    ll ans = 1;
    rep(i, K) {
      ans *= -n[i];
      ans %= MM;
    }
    cout << -ans + MM << endl;

    return 0;
  }

  sort(all(p));
  sort(rall(n));

  vector<ll> up, un;

  ll ans = 1;
  rep(i, K) {
    ll const pp = p.empty() ? -MM : p.back();
    ll const nn = n.empty() ? -MM : -n.back();
    if (pp > nn) {
      up.push_back(pp);
      ans *= pp;
      ans %= MM;
      p.pop_back();
    } else {
      un.push_back(-nn);
      ans *= nn;
      ans %= MM;
      n.pop_back();
    }
  }
  pv(ans);

  if (un.size() % 2 == 1) {
    ll aa, bb;

    ll a = -1, b = -1, c = -1, d = -1;
    if (!p.empty()) {
      a = p.back();
      b = -un.back();
    }
    if (!up.empty() && !n.empty()) {
      c = -n.back();
      d = up.back();
    }
    if (a != -1 && c != -1)
      if (a * d < c * b)
        a = c, b = d;
    if (a == -1)
      a = c, b = d;
    ans *= a;
    ans %= MM;
    ans *= modinv(b, MM);
    ans %= MM;
  }

  cout << ans << endl;

  return 0;
}