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

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mx 1000002
mt19937 myrand(time(0));

int main() {
  ios::sync_with_stdio(0);
  cin.tie(), cout.tie();
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  if (a[n - 1] == 0 && k & 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 1;
  if (a[n - 1] < 0 && k & 1) {
    for (int i = n - 1; i >= (n - k); i--) {
      (ans *= a[i]) %= mod;
      (ans += mod) %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  int i = 0, j = n - 1;
  if (k & 1) {
    (ans *= a[j]) %= mod;
    j--, k--;
  }
  for (int l = 0; l < (k / 2); l++) {
    ll tmp1 = a[i] * a[i + 1];
    ll tmp2 = a[j] * a[j - 1];
    if (tmp1 >= tmp2) {
      tmp1 %= mod;
      (ans *= tmp1) %= mod;
      (ans += mod) %= mod;
      i += 2;
    } else {
      tmp2 %= mod;
      (ans *= tmp2) %= mod;
      (ans += mod) %= mod;
      j -= 2;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mx 1000002
mt19937 myrand(time(0));

int main() {
  ios::sync_with_stdio(0);
  cin.tie(), cout.tie();
  ll n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  ll val = 0, zero = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x == 0)
      zero = 1;
    else if (x > 0)
      pos.pb(x);
    else
      neg.pb(x);
    val = max(val, x);
  }
  if (k == 1) {
    cout << val << endl;
    return 0;
  }
  if (k == n) {
    if (zero) {
      cout << 0 << endl;
      return 0;
    }
    ll ans = 1;
    for (auto &x : pos)
      ans = (ans * x) % mod;
    for (auto &x : neg) {
      ans = (ans * x) % mod;
      ans = (ans + mod) % mod;
    }
    cout << ans << endl;
    return 0;
  }
  sort(pos.rbegin(), pos.rend());
  sort(all(neg));
  int i = 0, j = 0;
  ll ans = 1, rem = k;
  while (rem > 1 && (i + 1 < (int)neg.size() || j + 1 < (int)pos.size())) {
    ll tmp1 = -1, tmp2 = -1;
    if (i + 1 < (int)pos.size())
      tmp1 = neg[i] * neg[i + 1];
    if (j + 1 < (int)pos.size())
      tmp2 = pos[j] * pos[j + 1];
    if (tmp1 != -1 && tmp2 != -1) {
      if (tmp1 >= tmp2)
        ans = (ans * tmp1) % mod, i += 2;
      else
        ans = (ans * tmp2) % mod, j += 2;
    } else if (tmp1 != -1) {
      ans = (ans * tmp1) % mod;
      j += 2;
    } else if (tmp2 != -1) {
      ans = (ans * tmp2) % mod;
      i += 2;
    }
    rem -= 2;
  }
  if (rem && j >= (int)pos.size()) {
    if (zero) {
      cout << 0 << endl;
      return 0;
    }
    ans = 1;
    for (int l = (int)neg.size() - 1; l >= 0; l--) {
      debug() << imie(neg[l]);
      if (k == 0)
        break;
      ans = (ans * neg[l]) % mod;
      ans = (ans + mod) % mod;
      k--;
    }
    cout << ans << endl;
    return 0;
  }
  for (; j < (int)pos.size(); j++) {
    if (rem == 0)
      break;
    ans = (ans * pos[j]) % mod;
    rem--;
  }
  if (rem)
    cout << 0 << endl;
  else
    cout << ans << endl;
  return 0;
}