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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj)                                                              \\
  {                                                                            \\
    for (auto x : obj)                                                         \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool comp_abs(int a, int b) { return abs(a) > abs(b); }

int main() {
  ll N, K, nax = -inf;
  cin >> N >> K;
  vector<ll> A(N);
  ll ans = 1;
  rep(i, N) {
    cin >> A[i];
    nax = max(nax, A[i]);
  }
  sort(all(A), comp_abs);

  if (nax < 0 && K % 2 == 1) {
    sort(all(A), greater<ll>());
    rep(i, K) {
      ans *= A[i];
      ans %= MOD;
    }
    if (ans < 0)
      ans += MOD;
    cout << ans << endl;
    return 0;
  }

  ll cnin = -inf, cnax = -inf;
  rep(i, K) {
    if (A[i] >= 0) {
      if (cnax == -inf) {
        cnax = A[i];
      } else {
        ans *= cnax;
        ans %= MOD;
        cnax = A[i];
      }
    } else {
      if (cnin == -inf) {
        cnin = A[i];
      } else {
        ans *= cnin;
        ans %= MOD;
        cnin = A[i];
      }
    }
  }
  // cout<<cnin<<' '<<cnax<<endl;
  ll t_ans = ans;
  if (cnin != -inf) {
    t_ans *= cnin;
    t_ans %= MOD;
  }
  if (cnax != -inf) {
    t_ans *= cnax;
    t_ans %= MOD;
  }
  // cout<<t_ans<<endl;

  if (t_ans < 0 && cnin != -inf && N != K) {
    if (N == K + 1) {
      ll t = A[K];
      ans *= t;
      ans %= MOD;
      if (t < 0) {
        ans *= cnin;
        ans %= MOD;
      } else {
        ans *= cnax;
        ans %= MOD;
      }
    } else {
      ll nxt_nin = -inf, nxt_nax = -inf;
      for (ll i = K; i < N; ++i) {
        if (nxt_nin == -inf && A[i] < 0)
          nxt_nin = A[i];
        if (nxt_nax == -inf && A[i] >= 0)
          nxt_nax = A[i];
      }
      if (cnax == -inf && nxt_nax != -inf) {
        ans *= nxt_nax;
        ans %= MOD;
      } else if (nxt_nax == -inf) {
        ans *= nxt_nin;
        ans %= MOD;
        ans *= cnin;
        ans %= MOD;
      } else if (nxt_nin == -inf) {
        ans *= nxt_nax;
        ans %= MOD;
        ans *= cnax;
        ans %= MOD;
      } else {
        ans *= (max(nxt_nin * cnin, nxt_nax * cnax) % MOD);
        ans %= MOD;
      }
    }
  } else {
    ans = t_ans;
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj)                                                              \\
  {                                                                            \\
    for (auto x : obj)                                                         \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll N, K, a;
  cin >> N >> K;
  ll ans = 1;
  vector<ll> plus;
  vector<ll> minus;
  rep(i, N) {
    cin >> a;
    if (a < 0)
      minus.push_back(a);
    else
      plus.push_back(a);
  }
  sort(all(plus), greater<ll>());
  sort(all(minus), greater<ll>());

  // all num is minus ^ K is odd
  if (plus.size() == 0 && K % 2 == 1) {
    rep(i, K) {
      ans *= minus[i];
      ans %= MOD;
    }
    if (ans < 0)
      ans += MOD;
    cout << ans << endl;
    return 0;
  }

  // else
  if (K % 2 == 1) {
    ans = plus[0];
    plus.erase(plus.begin());
    --K;
  }

  vector<ll> exd;
  for (ll i = 0; i < plus.size(); i += 2) {
    if (i + 1 >= plus.size())
      exd.push_back(plus[i]);
    else
      exd.push_back(plus[i] * plus[i + 1]);
  }
  for (ll i = 0; i < minus.size(); i += 2) {
    if (i + 1 >= minus.size())
      exd.push_back(minus[i]);
    else
      exd.push_back(minus[i] * minus[i + 1]);
  }
  sort(all(exd), greater<ll>());

  // show(exd);
  for (ll i = 0; K >= 2; i += 2, K -= 2) {
    ans *= (exd[i] % MOD);
    ans %= MOD;
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}