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

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> posi, nega;
  int z = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      posi.push_back(x);
    else if (x == 0)
      ++z;
    else
      nega.push_back(abs(x));
  }
  int posiNum = posi.size();
  int negaNum = nega.size();
  ll MOD = 1e9 + 7;
  // 負数になる場合
  if (!z && ((posiNum == 0 && k % 2 == 1) || (n == k && negaNum % 2 == 1))) {
    ll res = 1;
    sort(all(posi));
    sort(all(nega));
    for (ll x : posi) {
      res *= x;
      res %= MOD;
      --k;
    }
    rep(i, k) {
      res *= nega[i];
      res %= MOD;
    }
    cout << MOD - res << endl;
  }
  // 正数になる場合
  else if (posiNum + (negaNum / 2) * 2 >= k) {
    if (posiNum == 0 && k % 2 == 1) {
      cout << 0 << endl;
      return 0;
    }
    sort(all(posi), greater<ll>());
    sort(all(nega), greater<ll>());
    int usedNegaNum = 0;
    int usedPosiNum = k;
    for (; usedNegaNum + 2 <= negaNum && usedPosiNum - 2 >= 0;
         usedNegaNum += 2, usedPosiNum -= 2) {
      if (usedPosiNum > posiNum)
        continue;
      ll nextNega = nega[usedNegaNum] * nega[usedNegaNum + 1];
      ll nextPosi = posi[usedPosiNum - 1] * posi[usedPosiNum - 2];
      if (nextPosi >= nextNega)
        break;
    }
    ll res = 1;
    rep(i, usedPosiNum) {
      res *= posi[i];
      res %= MOD;
    }
    rep(i, usedNegaNum) {
      res *= nega[i];
      res %= MOD;
    }
    cout << res << endl;
  } else
    cout << 0 << endl;
}

Error: 0

Fixed submission:

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> posi, nega;
  int z = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      posi.push_back(x);
    else if (x == 0)
      ++z;
    else
      nega.push_back(abs(x));
  }
  int posiNum = posi.size();
  int negaNum = nega.size();
  ll MOD = 1e9 + 7;
  // 負数になる場合
  if (!z && ((posiNum == 0 && k % 2 == 1) || (n == k && negaNum % 2 == 1))) {
    ll res = 1;
    sort(all(posi));
    sort(all(nega));
    for (ll x : posi) {
      res *= x;
      res %= MOD;
      --k;
    }
    rep(i, k) {
      res *= nega[i];
      res %= MOD;
    }
    cout << MOD - res << endl;
  }
  // 正数になる場合
  else if (posiNum + (negaNum / 2) * 2 >= k) {
    sort(all(posi), greater<ll>());
    sort(all(nega), greater<ll>());
    int usedNegaNum = 0;
    int usedPosiNum = k;
    for (; usedNegaNum + 2 <= negaNum && usedPosiNum - 2 >= 0;
         usedNegaNum += 2, usedPosiNum -= 2) {
      if (usedPosiNum > posiNum)
        continue;
      ll nextNega = nega[usedNegaNum] * nega[usedNegaNum + 1];
      ll nextPosi = posi[usedPosiNum - 1] * posi[usedPosiNum - 2];
      if (nextPosi >= nextNega)
        break;
    }
    ll res = 1;
    rep(i, usedPosiNum) {
      res *= posi[i];
      res %= MOD;
    }
    rep(i, usedNegaNum) {
      res *= nega[i];
      res %= MOD;
    }
    cout << res << endl;
  } else
    cout << 0 << endl;
}