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

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, ans, a[ohara], b, c, d, tmp, m, h, w, x, y, sum, k, q;
P p[ohara];
ll sei = -1, fu = -1;
ll corner;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  rep(i, n) cin >> a[i];
  rep(i, n) p[i] = m_p(abs(a[i]), i);
  ans = 1;
  sort(p, p + n);
  reverse(p, p + n);
  rep(i, k) {
    ans *= (a[p[i].se]);
    if (a[p[i].se] >= 0)
      sei = i;
    else
      fu = i;
    ans %= MOD;
  }
  if (ans >= 0)
    Cout(ans);
  else {
    ll h1, h2, h3, h4;
    ans = 1;
    ll ans2 = 1;
    if (sei != -1) {
      h2 = abs(a[p[sei].se]);
      cnt = 0;
      rep(i, n) {
        if (i == sei)
          continue;
        if (cnt < k - 1) {
          ans *= (a[p[i].se]);
          cnt++;
        } else if (cnt == k - 1) {
          if (a[p[i].se] >= 0)
            continue;
          ans *= (a[p[i].se]);
          h1 = abs(a[p[i].se]);
          ans %= MOD;
          cnt++;
          break;
        }
        ans %= MOD;
      }
    }
    ll cnt2 = 0;
    if (fu != -1) {
      h4 = abs(a[p[fu].se]);
      rep(i, n) {
        if (i == fu)
          continue;
        if (cnt2 < k - 1) {
          ans2 *= (a[p[i].se]);
          cnt2++;
        } else if (cnt2 == k - 1) {
          if (a[p[i].se] < 0)
            continue;
          ans2 *= (a[p[i].se]);
          h3 = abs(a[p[i].se]);
          ans2 %= MOD;
          cnt2++;
          break;
        }
        ans2 %= MOD;
      }
    }
    if (cnt2 == k || cnt == k) {
      if (sei == -1) {
        Cout(ans2);
      } else {
        if (h2 == 0) {
          if (h1 == 0) {
            Cout(ans2);
          } else
            Cout(ans);
        } else if (h1 * h4 >= h3 * h2) {
          Cout(ans);
        } else {
          Cout(ans2);
        }
      }
    } else {
      ans = 1;
      rrrep(i, n - 1, n - k) {
        ans *= (a[p[i].se]);
        ans %= MOD;
      }
      ans += MOD;
      ans %= MOD;
      Cout(ans);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, ans, a[ohara], b, c, d, tmp, m, h, w, x, y, sum, k, q;
P p[ohara];
ll sei = -1, fu = -1;
ll corner;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  rep(i, n) cin >> a[i];
  rep(i, n) p[i] = m_p(abs(a[i]), i);
  ans = 1;
  sort(p, p + n);
  reverse(p, p + n);
  rep(i, k) {
    ans *= (a[p[i].se]);
    if (a[p[i].se] >= 0)
      sei = i;
    else
      fu = i;
    ans %= MOD;
  }
  if (ans >= 0)
    Cout(ans);
  else {
    ll h1, h2, h3, h4;
    ans = 1;
    ll ans2 = 1;
    if (sei != -1) {
      h2 = abs(a[p[sei].se]);
      cnt = 0;
      rep(i, n) {
        if (i == sei)
          continue;
        if (cnt < k - 1) {
          ans *= (a[p[i].se]);
          cnt++;
        } else if (cnt == k - 1) {
          if (a[p[i].se] >= 0)
            continue;
          ans *= (a[p[i].se]);
          h1 = abs(a[p[i].se]);
          ans %= MOD;
          cnt++;
          break;
        }
        ans %= MOD;
      }
    }
    ll cnt2 = 0;
    if (fu != -1) {
      h4 = abs(a[p[fu].se]);
      rep(i, n) {
        if (i == fu)
          continue;
        if (cnt2 < k - 1) {
          ans2 *= (a[p[i].se]);
          cnt2++;
        } else if (cnt2 == k - 1) {
          if (a[p[i].se] < 0)
            continue;
          ans2 *= (a[p[i].se]);
          h3 = abs(a[p[i].se]);
          ans2 %= MOD;
          cnt2++;
          break;
        }
        ans2 %= MOD;
      }
    }
    if (cnt2 == k || cnt == k) {
      if (sei == -1) {
        Cout(ans2);
      } else {
        if (h2 == 0) {
          if (h1 == 0) {
            Cout(ans2);
          } else
            Cout(ans);
        } else if (h1 * h4 >= h3 * h2) {
          return -1;
          Cout(ans);
        } else {
          Cout(ans2);
        }
      }
    } else {
      ans = 1;
      rrrep(i, n - 1, n - k) {
        ans *= (a[p[i].se]);
        ans %= MOD;
      }
      ans += MOD;
      ans %= MOD;
      Cout(ans);
    }
  }
  return 0;
}