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

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
#undef LOCAL
#ifdef LOCAL
template <class T, class S>
ostream &operator<<(ostream &str, const pair<T, S> &p) {
  return (str << "{" << p.fi << ", " << p.se << "}");
}
template <template <class...> class Container, class... T>
ostream &operator<<(ostream &str, const Container<T...> &vec) {
  str << '[';
  for (auto it = vec.begin(); it != vec.end(); it++) {
    if (it != vec.begin())
      str << ", ";
    str << *it;
  }
  str << ']';
  return str;
}
#define var(x) "[" << #x << ": " << x << "] "
struct dbg {
  dbg() { cout << "  "; }
  ~dbg() { cout << endl; }
  template <class T> dbg &operator<<(T oth) {
    cout << oth;
    return *this;
  }
};
#else
#define var(x) 0
struct dbg {
  template <class T> dbg &operator<<(T oth) { return *this; }
};
#endif
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main(int argc, char **argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  auto solve = [&]() {
    int n, k;
    cin >> n >> k;
    vector<ll> pos, neg;
    for (int i = 0; i < n; i++) {
      ll d;
      cin >> d;
      if (d >= 0)
        pos.pb(d);
      else
        neg.pb(-d);
    }
    sort(neg.rbegin(), neg.rend());
    sort(pos.rbegin(), pos.rend());
    const ll mod = (ll)(1e9 + 7);

    if (n == k) {
      ll m1 = ll(1e9 + 6);
      ll ans = 1;
      for (auto g : pos)
        ans = (ans * g) % mod;
      for (auto g : neg)
        ans = (((ans * g) % mod) * m1) % mod;
      cout << ans << '\\n';
      return;
    }

    if (pos.empty()) {
      ll ans = 1;
      if (k & 1) {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[int(neg.size()) - 1 - i]) % mod;
        if (ans)
          ans = mod - ans;
      } else {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[i]) % mod;
      }
      cout << ans << '\\n';
    } else {
      int pos_taken = min(k, (int)pos.size());
      int neg_taken = k - pos_taken;
      if ((neg_taken & 1) && pos_taken != pos.size())
        --neg_taken, ++pos_taken;
      if ((neg_taken & 1) && neg_taken < (int)neg.size() &&
          pos_taken == pos.size())
        ++neg_taken, --pos_taken;
      dbg() << var(pos_taken) << var(neg_taken);
      while (pos_taken >= 2 && neg_taken + 1 < (int)neg.size()) {
        ll pos_give = pos[pos_taken - 1] * pos[pos_taken - 2];
        dbg() << var(neg) << var(neg_taken);
        ll neg_give = neg[neg_taken] * neg[neg_taken + 1];
        if (neg_give < pos_give)
          break;
        pos_taken -= 2, neg_taken += 2;
      }
      ll ans = 1;

      for (int i = 0; i < pos_taken; i++)
        ans = (ans * pos[i]) % mod;
      for (int i = 0; i < neg_taken; i++)
        ans = (ans * neg[i]) % mod;
      cout << ans << '\\n';
    }
  };
  int t;
  t = 1;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
#ifdef LOCAL
template <class T, class S>
ostream &operator<<(ostream &str, const pair<T, S> &p) {
  return (str << "{" << p.fi << ", " << p.se << "}");
}
template <template <class...> class Container, class... T>
ostream &operator<<(ostream &str, const Container<T...> &vec) {
  str << '[';
  for (auto it = vec.begin(); it != vec.end(); it++) {
    if (it != vec.begin())
      str << ", ";
    str << *it;
  }
  str << ']';
  return str;
}
#define var(x) "[" << #x << ": " << x << "] "
struct dbg {
  dbg() { cout << "  "; }
  ~dbg() { cout << endl; }
  template <class T> dbg &operator<<(T oth) {
    cout << oth;
    return *this;
  }
};
#else
#define var(x) 0
struct dbg {
  template <class T> dbg &operator<<(T oth) { return *this; }
};
#endif
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main(int argc, char **argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  auto solve = [&]() {
    int n, k;
    cin >> n >> k;
    vector<ll> pos, neg;
    for (int i = 0; i < n; i++) {
      ll d;
      cin >> d;
      if (d >= 0)
        pos.pb(d);
      else
        neg.pb(-d);
    }
    sort(neg.rbegin(), neg.rend());
    sort(pos.rbegin(), pos.rend());
    const ll mod = (ll)(1e9 + 7);

    if (n == k) {
      ll m1 = ll(1e9 + 6);
      ll ans = 1;
      for (auto g : pos)
        ans = (ans * g) % mod;
      for (auto g : neg)
        ans = (((ans * g) % mod) * m1) % mod;
      cout << ans << '\\n';
      return;
    }

    if (pos.empty()) {
      ll ans = 1;
      if (k & 1) {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[int(neg.size()) - 1 - i]) % mod;
        if (ans)
          ans = mod - ans;
      } else {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[i]) % mod;
      }
      cout << ans << '\\n';
    } else {
      int pos_taken = min(k, (int)pos.size()), neg_taken = k - pos.size();
      if ((neg_taken & 1) && pos_taken != pos.size())
        --neg_taken, ++pos_taken;

      dbg() << var(pos_taken) << var(neg_taken);
      while (pos_taken >= 2 && neg_taken + 1 < (int)neg.size()) {
        ll pos_give = pos[pos_taken - 1] * pos[pos_taken - 2];
        dbg() << var(neg) << var(neg_taken);
        ll neg_give = neg[neg_taken] * neg[neg_taken + 1];
        if (neg_give < pos_give)
          break;
        pos_taken -= 2, neg_taken += 2;
      }
      ll ans = 1;

      for (int i = 0; i < pos_taken; i++)
        ans = (ans * pos[i]) % mod;
      for (int i = 0; i < neg_taken; i++)
        ans = (ans * neg[i]) % mod;
      cout << ans << '\\n';
    }
  };
  int t;
  t = 1;
  while (t--)
    solve();
  return 0;
}