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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll powmod(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}
ll n, k, z, ma;
bool ze, p;
int main() {
  cin >> n >> k;
  vector<ll> s(n), t1, t2, v1, v2;
  REP(i, n) {
    cin >> s[i];
    if (s[i] == 0)
      z++;
    else if (s[i] < 0)
      t2.push_back(s[i] * -1);
    else
      t1.push_back(s[i]);
  }
  sort(ALL(t1));
  sort(ALL(t2));
  ll t2s = t2.size(), t1s = t1.size();
  if (z > 0)
    ze = true;
  v1.push_back(0);
  v2.push_back(0);
  // 正判定
  REP(i, k + 1) {
    // cout << i << " " << k - i << " " << t1s << " " << t2s << endl;
    if (i % 2 == 1)
      continue;
    if (t1s < k - i)
      continue;
    if (t2s < i)
      continue;
    p = true;
    ma = max(i, ma);
  }
  // 正が作れない
  if (!p) {
    if (ze) {
      cout << 0 << endl;
      return 0;
    }
    vector<ll> l;
    REP(i, t1s) { l.push_back(t1[i]); }
    REP(i, t2s) { l.push_back(t2[i]); }
    sort(ALL(l));
    ll ans = 1;
    REP(i, k) {
      ans *= l[i];
      ans %= MOD;
    }
    cout << (MOD * 2 - ans) % MOD << endl;
    return 0;
  }
  vector<P> l;
  REP(i, t1s) { l.push_back(P(t1[i], 1)); }
  REP(i, t2s) { l.push_back(P(t2[i], -1)); }
  sort(ALL(l), greater<P>());
  ll ans = 1, cnt = 0, m1 = -1, p1 = -1, m2 = -1, p2 = -1;
  REP(i, k) {
    ans *= l[i].first;
    ans %= MOD;
    if (l[i].second == -1)
      cnt++;
    if (l[i].second == -1)
      m1 = l[i].first;
    if (l[i].second == 1)
      p1 = l[i].first;
  }
  if (cnt % 2 == 0) {
    cout << ans << endl;
    return 0;
  }
  FOR(i, k, n) {
    if (m2 == -1 and l[i].second == -1)
      m2 = l[i].first;
    if (p2 == -1 and l[i].second == 1)
      p2 = l[i].first;
  }
  if (m1 == -1 or p2 == -1) {
    ans = ans * m2 % MOD * powmod(p1, MOD - 2) % MOD;
  } else if (m2 == -1 or p1 == -1) {
    ans = ans * p2 % MOD * powmod(m1, MOD - 2) % MOD;
  } else if (p1 * p2 >= m1 * m2) {
    ans = ans * p2 % MOD * powmod(m1, MOD - 2) % MOD;
  } else {
    ans = ans * m2 % MOD * powmod(p1, MOD - 2) % MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll powmod(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}
ll n, k, z, ma;
bool ze, p;
int main() {
  cin >> n >> k;
  vector<ll> s(n), t1, t2, v1, v2;
  REP(i, n) {
    cin >> s[i];
    if (s[i] == 0)
      z++;
    else if (s[i] < 0)
      t2.push_back(s[i] * -1);
    else
      t1.push_back(s[i]);
  }
  sort(ALL(t1));
  sort(ALL(t2));
  ll t2s = t2.size(), t1s = t1.size();
  if (ze > 0)
    ze = true;
  v1.push_back(0);
  v2.push_back(0);
  // 正判定
  REP(i, k + 1) {
    // cout << i << " " << k - i << " " << t1s << " " << t2s << endl;
    if (i % 2 == 1)
      continue;
    if (t1s < k - i)
      continue;
    if (t2s < i)
      continue;
    p = true;
    ma = max(i, ma);
  }
  // 正が作れない
  if (!p) {
    if (ze) {
      cout << 0 << endl;
      return 0;
    }
    vector<ll> l;
    REP(i, t1s) { l.push_back(t1[i]); }
    REP(i, t2s) { l.push_back(t2[i]); }
    sort(ALL(l));
    ll ans = 1;
    REP(i, k) {
      ans *= l[i];
      ans %= MOD;
    }
    cout << (MOD * 2 - ans) % MOD << endl;
    return 0;
  }
  vector<P> l;
  REP(i, t1s) { l.push_back(P(t1[i], 1)); }
  REP(i, t2s) { l.push_back(P(t2[i], -1)); }
  sort(ALL(l), greater<P>());
  ll ans = 1, cnt = 0, m1 = -1, p1 = -1, m2 = -1, p2 = -1;
  REP(i, k) {
    ans *= l[i].first;
    ans %= MOD;
    if (l[i].second == -1)
      cnt++;
    if (l[i].second == -1)
      m1 = l[i].first;
    if (l[i].second == 1)
      p1 = l[i].first;
  }
  if (cnt % 2 == 0) {
    cout << ans << endl;
    return 0;
  }
  FOR(i, k, n) {
    if (m2 == -1 and l[i].second == -1)
      m2 = l[i].first;
    if (p2 == -1 and l[i].second == 1)
      p2 = l[i].first;
  }
  if (m1 == -1 or p2 == -1) {
    ans = ans * m2 % MOD * powmod(p1, MOD - 2) % MOD;
  } else if (m2 == -1 or p1 == -1) {
    ans = ans * p2 % MOD * powmod(m1, MOD - 2) % MOD;
  } else if (p1 * p2 >= m1 * m2) {
    ans = ans * p2 % MOD * powmod(m1, MOD - 2) % MOD;
  } else {
    ans = ans * m2 % MOD * powmod(p1, MOD - 2) % MOD;
  }
  cout << ans << endl;
}