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

#include <algorithm> // std::unique
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream> // std::ostringstream
#include <string>
#include <unordered_map>
#include <vector>

#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;

using ll = long long;
constexpr auto PrimeBigNum10_9_7 = 1000000007;
const long long INF = 1LL << 60;

#define rep(i, a, b) for (ll i = a; i <= b; ++i)
#define repb(i, a, b) for (ll i = a; i >= b; --i)

#define OUTPUT_DEBUG_LOG

template <typename T> void DisplayAnswer(const T &v) { cout << v << endl; }
template <typename T> void DisplayAnswers(const vector<T> &vs) {
  for (const auto &v : vs) {
    cout << v << endl;
  }
}

template <typename T> void DisplayAnswerOneLine(const vector<T> &vs) {
  for (const auto &v : vs) {
    cout << v << ' ';
  }
  cout << endl;
}

std::vector<std::string> split(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

// 素数判定
template <typename T> bool IsPrimeNumber(T value) {

  for (int i = 2; i * i <= value; ++i) {
    if ((value % i) == 0) {
      return true;
    }
  }
  return false;
}

// 桁数判定
template <typename T> T GetFigureLength(T value) {
  T figure = 1;
  while (value >= 10) {
    value /= 10;
    ++figure;
  }
  return figure;
}

// 素因数分解結果
template <typename T> map<T, T> GetPrimeFactorize(T value) {

  map<T, T> res;

  for (T i = 2; i * i <= value; ++i) {
    if ((value % i) != 0)
      continue;

    T count = 0;
    while ((value % i) == 0) {
      value /= i;
      ++count;
    }

    res[i] = count;
  }

  if (value != 1) {
    res[value] = 1;
  }
  return res;
}

void DebugPrint(const vector<ll> &vec) {
  cout << "{";
  for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
    cout << *itr << ",";
  }
  cout << "}" << endl;
}

// valueが2のx乗以下である計算
ll calc2x(ll value) {
  ll n = 0;
  while (value > 0) {
    value /= 2;
    ++n;
  }
  return n;
}

struct Vec2 {
  double x;
  double y;

  void rotate(double rad) {
    double s = sin(rad);
    double c = cos(rad);
    double tmpX = this->x * c - this->y * s;
    double tmpY = this->x * s + this->y * c;
    this->x = tmpX;
    this->y = tmpY;
  }
};

void FuncA() {
  ll m;
  cin >> m;

  while (m > 0) {
    m -= 1000;
  }
  DisplayAnswer(-m);
}

void FuncB() {
  unordered_map<string, int> map;

  map["AC"] = 0;
  map["WA"] = 0;
  map["TLE"] = 0;
  map["RE"] = 0;

  ll n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    map[str]++;
  }

  cout << "AC x " << map["AC"] << endl;
  cout << "WA x " << map["WA"] << endl;
  cout << "TLE x " << map["TLE"] << endl;
  cout << "RE x " << map["RE"] << endl;
}
namespace C {
ll map[6][6] = {};
int calcSum(bitset<6> bh, bitset<6> bw) {
  ll sum = 0;
  for (ll i = 0; i < 6; ++i) {
    if (bh[i] == 0)
      continue;
    for (ll j = 0; j < 6; ++j) {
      if (bw[j]) {
        sum += map[i][j];
      }
    }
  }
  return sum;
}

int CalcFuncC() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll mapHs[6] = {};

  ll black = 0;
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      char c;
      cin >> c;
      map[i][j] = (c == '#') ? 1 : 0;
      black += map[i][j];
      mapHs[i] += map[i][j];
    }
  }
  ll hMax = 1 << H;
  ll wMax = 1 << W;

  ll answer = 0;
  for (ll i = 0; i < hMax; ++i) {
    bitset<6> bh = i;
    ll n = 0;
    for (ll k = 0; k < 6; ++k) {
      if (bh[k]) {
        n += mapHs[k];
      }
    }
    if (n < K)
      continue;

    for (ll j = 0; j < wMax; ++j) {
      ll sum = calcSum(bh, j);
      if (sum == K) {
        bitset<6> bw = j;
        answer++;
      }
    }
  }

  return answer;
}
} // namespace C

void FuncC() { DisplayAnswer(C::CalcFuncC()); }

void FuncD() {
  ll N;
  cin >> N;
  vector<ll> players(N);
  for (ll i = 0; i < N; ++i) {
    cin >> players[i];
  }
  sort(players.rbegin(), players.rend());

  std::list<ll> list;
  list.push_back(players[0]);
  ll sum = players[0];
  for (ll i = 1; i < N / 2; ++i) {
    sum += players[i] * 2;
  }

  DisplayAnswer(sum);
}

ll calcFuncE() {
  ll N, K;
  cin >> N >> K;
  vector<ll> vecPositive;
  vector<ll> vecNegative;
  for (ll i = 0; i < N; ++i) {
    ll n;
    cin >> n;

    if (n > 0) {
      vecPositive.push_back(n);
    } else {
      vecNegative.push_back(n);
    }
  }
  if (vecPositive.size() + vecNegative.size() < K)
    return 0;

  sort(vecPositive.rbegin(), vecPositive.rend());
  sort(vecNegative.begin(), vecNegative.end());

  if (N == K) {
    ll ans = 1;
    for (ll i = 0; i < vecPositive.size(); ++i) {
      ans *= vecPositive[i];
      ans %= PrimeBigNum10_9_7;
    }
    for (ll i = 0; i < vecNegative.size(); ++i) {
      ans *= vecNegative[i];
      ans %= PrimeBigNum10_9_7;
    }
    return ans;
  } else if (vecPositive.size() == 0) {
#if 1
    if (vecNegative.size() < N && (K % 2)) {
      return 0;
    }
    if (K % 2)
      reverse(vecNegative.begin(), vecNegative.end());
    ll ans = 1;
    rep(i, 0, K - 1) ans = (ans * vecNegative[i]) % PrimeBigNum10_9_7;
    return ans;

#else
    if (vecNegative.size() < K)
      return 0;
    if ((K % 2) == 1) {

      sort(vecNegative.rbegin(), vecNegative.rend());

      ll ans = 1;
      for (ll i = 0; i < K; ++i) {
        ans *= vecNegative[i];
        ans %= PrimeBigNum10_9_7;
      }
      return ans;
    } else {
      ll ans = 1;
      for (ll i = 0; i < K; ++i) {
        ans *= vecNegative[i];
        ans %= PrimeBigNum10_9_7;
      }
      return ans;
    }
#endif
  } else {
    ll ans = 1;
    ll indexP = 0;
    ll indexN = 0;
    for (ll count = 0; count < K;) {
      bool addP = false;
      if (count + 1 == K) {
        addP = true;
      } else if (indexP == vecPositive.size() - 1 && ((K - count) % 2) == 0) {
        addP = false;
      } else if (indexP == vecPositive.size()) {
        addP = false;
      } else if (indexN == vecNegative.size()) {
        addP = true;
      } else {
        ll p = vecPositive[indexP] * vecPositive[indexP + 1];
        ll n = vecNegative[indexN] * vecNegative[indexN + 1];
        addP = p > n;
      }

      if (addP) {
        ans *= vecPositive[indexP];
        indexP++;
        count++;
      } else {
        ans *= vecNegative[indexN];
        ans %= PrimeBigNum10_9_7;
        ans *= vecNegative[indexN + 1];
        indexN += 2;
        count += 2;
      }

      ans %= PrimeBigNum10_9_7;
    }
    return ans;
  }
}

void FuncE() {

  auto ans = calcFuncE() % PrimeBigNum10_9_7;

  DisplayAnswer((ans + PrimeBigNum10_9_7) % PrimeBigNum10_9_7);
}
int main(void) {

  FuncE();

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm> // std::unique
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream> // std::ostringstream
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, a, b) for (LL i = a; i <= b; ++i)
#define repb(i, a, b) for (LL i = a; i >= b; --i)

#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;

using ll = long long;
constexpr auto PrimeBigNum10_9_7 = 1000000007;
const long long INF = 1LL << 60;

#define OUTPUT_DEBUG_LOG

template <typename T> void DisplayAnswer(const T &v) { cout << v << endl; }
template <typename T> void DisplayAnswers(const vector<T> &vs) {
  for (const auto &v : vs) {
    cout << v << endl;
  }
}

template <typename T> void DisplayAnswerOneLine(const vector<T> &vs) {
  for (const auto &v : vs) {
    cout << v << ' ';
  }
  cout << endl;
}

std::vector<std::string> split(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

// 素数判定
template <typename T> bool IsPrimeNumber(T value) {

  for (int i = 2; i * i <= value; ++i) {
    if ((value % i) == 0) {
      return true;
    }
  }
  return false;
}

// 桁数判定
template <typename T> T GetFigureLength(T value) {
  T figure = 1;
  while (value >= 10) {
    value /= 10;
    ++figure;
  }
  return figure;
}

// 素因数分解結果
template <typename T> map<T, T> GetPrimeFactorize(T value) {

  map<T, T> res;

  for (T i = 2; i * i <= value; ++i) {
    if ((value % i) != 0)
      continue;

    T count = 0;
    while ((value % i) == 0) {
      value /= i;
      ++count;
    }

    res[i] = count;
  }

  if (value != 1) {
    res[value] = 1;
  }
  return res;
}

void DebugPrint(const vector<ll> &vec) {
  cout << "{";
  for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
    cout << *itr << ",";
  }
  cout << "}" << endl;
}

// valueが2のx乗以下である計算
ll calc2x(ll value) {
  ll n = 0;
  while (value > 0) {
    value /= 2;
    ++n;
  }
  return n;
}

struct Vec2 {
  double x;
  double y;

  void rotate(double rad) {
    double s = sin(rad);
    double c = cos(rad);
    double tmpX = this->x * c - this->y * s;
    double tmpY = this->x * s + this->y * c;
    this->x = tmpX;
    this->y = tmpY;
  }
};

void FuncA() {
  ll m;
  cin >> m;

  while (m > 0) {
    m -= 1000;
  }
  DisplayAnswer(-m);
}

void FuncB() {
  unordered_map<string, int> map;

  map["AC"] = 0;
  map["WA"] = 0;
  map["TLE"] = 0;
  map["RE"] = 0;

  ll n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    map[str]++;
  }

  cout << "AC x " << map["AC"] << endl;
  cout << "WA x " << map["WA"] << endl;
  cout << "TLE x " << map["TLE"] << endl;
  cout << "RE x " << map["RE"] << endl;
}
namespace C {
ll map[6][6] = {};
int calcSum(bitset<6> bh, bitset<6> bw) {
  ll sum = 0;
  for (ll i = 0; i < 6; ++i) {
    if (bh[i] == 0)
      continue;
    for (ll j = 0; j < 6; ++j) {
      if (bw[j]) {
        sum += map[i][j];
      }
    }
  }
  return sum;
}

int CalcFuncC() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll mapHs[6] = {};

  ll black = 0;
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      char c;
      cin >> c;
      map[i][j] = (c == '#') ? 1 : 0;
      black += map[i][j];
      mapHs[i] += map[i][j];
    }
  }
  ll hMax = 1 << H;
  ll wMax = 1 << W;

  ll answer = 0;
  for (ll i = 0; i < hMax; ++i) {
    bitset<6> bh = i;
    ll n = 0;
    for (ll k = 0; k < 6; ++k) {
      if (bh[k]) {
        n += mapHs[k];
      }
    }
    if (n < K)
      continue;

    for (ll j = 0; j < wMax; ++j) {
      ll sum = calcSum(bh, j);
      if (sum == K) {
        bitset<6> bw = j;
        answer++;
      }
    }
  }

  return answer;
}
} // namespace C

void FuncC() { DisplayAnswer(C::CalcFuncC()); }

void FuncD() {
  ll N;
  cin >> N;
  vector<ll> players(N);
  for (ll i = 0; i < N; ++i) {
    cin >> players[i];
  }
  sort(players.rbegin(), players.rend());

  std::list<ll> list;
  list.push_back(players[0]);
  ll sum = players[0];
  for (ll i = 1; i < N / 2; ++i) {
    sum += players[i] * 2;
  }

  DisplayAnswer(sum);
}

ll calcFuncE() {
  ll N, K;
  cin >> N >> K;
  vector<ll> vecPositive;
  vector<ll> vecNegative;
  while (N--) {
    ll n;
    cin >> n;

    if (n > 0) {
      vecPositive.push_back(n);
    } else {
      vecNegative.push_back(n);
    }
  }
  if (vecPositive.size() + vecNegative.size() < K)
    return 0;

  sort(vecPositive.rbegin(), vecPositive.rend());
  sort(vecNegative.begin(), vecNegative.end());

  if (N == K) {
    ll ans = 1;
    for (ll i = 0; i < vecPositive.size(); ++i) {
      ans *= vecPositive[i];
      ans %= PrimeBigNum10_9_7;
    }
    for (ll i = 0; i < vecNegative.size(); ++i) {
      ans *= vecNegative[i];
      ans %= PrimeBigNum10_9_7;
    }
    return ans;
  } else if (vecPositive.size() == 0) {

    if (vecNegative.size() < K)
      return 0;
    if ((K % 2) == 1) {

      sort(vecNegative.rbegin(), vecNegative.rend());

      ll ans = 1;
      for (ll i = 0; i < K; ++i) {
        ans *= vecNegative[i];
        ans %= PrimeBigNum10_9_7;
      }
      return ans;
    } else {
      ll ans = 1;
      for (ll i = 0; i < K; ++i) {
        ans *= vecNegative[i];
        ans %= PrimeBigNum10_9_7;
      }
      return ans;
    }
  } else {
    ll ans = 1;
    ll indexP = 0;
    ll indexN = 0;
    for (ll count = 0; count < K;) {
      bool addP = false;
      if (count + 1 == K) {
        addP = true;
      } else if (indexP == vecPositive.size() - 1 && ((K - count) % 2) == 0) {

        ans *= vecPositive[indexN];
        ans %= PrimeBigNum10_9_7;
        indexN++;
        count++;
        continue;
      } else if (indexP == vecPositive.size()) {
        addP = false;
      } else if (indexN == vecNegative.size()) {
        addP = true;
      } else {
        ll p = vecPositive[indexP] * vecPositive[indexP + 1];
        ll n = vecNegative[indexN] * vecNegative[indexN + 1];
        addP = p > n;
      }

      if (addP) {
        ans *= vecPositive[indexP];
        indexP++;
        count++;
      } else {
        ans *= vecNegative[indexN];
        ans %= PrimeBigNum10_9_7;
        ans *= vecNegative[indexN + 1];
        indexN += 2;
        count += 2;
      }

      ans %= PrimeBigNum10_9_7;
    }
    return ans;
  }
}

void FuncE() {

  auto ans = calcFuncE() % PrimeBigNum10_9_7;

  DisplayAnswer((ans + PrimeBigNum10_9_7) % PrimeBigNum10_9_7);
}
int main(void) {

  FuncE();

  return 0;
}