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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...)                                                             \\
  cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__)    \\
       << endl;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\\b\\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) { oss << "\\b\\b \\b"; }

template <typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template <typename... T> string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1)
    oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1)
    oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int N, K;
  vector<int> neg, pos;

  scanf("%d%d", &N, &K);

  for (int i = 0; i < N; i++) {
    int A;
    scanf("%d", &A);
    if (A < 0)
      neg.push_back(A);
    else
      pos.push_back(A);
  }

  sort(neg.begin(), neg.end());
  sort(pos.rbegin(), pos.rend());

  if (pos.size() == 0) {
    ll ans = 1;
    if (K % 2 == 1) {
      reverse(neg.begin(), neg.end());
    }
    for (int i = 0; i < K; i++)
      ans = ans * (mod + neg[i]) % mod;

    cout << ans << endl;
    return 0;
  }

  ll ans = 1;
  int s = 0, t = 0;

  if (K % 2) {
    ans = pos[s++];
    K--;
  }

  for (int i = 0; i < K / 2; i++) {
    ll A = s + 1 < pos.size() ? (ll)pos[s] * pos[s + 1] : 0;
    ll B = t + 1 < neg.size() ? (ll)neg[t] * neg[t + 1] : 0;

    if (s + 1 == pos.size() && t + 1 == neg.size()) {
      ans = ans * pos[s] % mod * (mod + neg[t]) % mod;
    } else if (A > B) {
      ans = ans * (A % mod) % mod;
      s += 2;
    } else {
      ans = ans * (B % mod) % mod;
      t += 2;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...)                                                             \\
  cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__)    \\
       << endl;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\\b\\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) { oss << "\\b\\b \\b"; }

template <typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template <typename... T> string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1)
    oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1)
    oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int N, K;
  vector<int> neg, pos;

  scanf("%d%d", &N, &K);

  for (int i = 0; i < N; i++) {
    int A;
    scanf("%d", &A);
    if (A < 0)
      neg.push_back(A);
    else
      pos.push_back(A);
  }

  sort(neg.begin(), neg.end());
  sort(pos.rbegin(), pos.rend());

  if (pos.size() == 0) {
    ll ans = 1;
    if (K % 2 == 1) {
      reverse(neg.begin(), neg.end());
    }
    for (int i = 0; i < K; i++)
      ans = ans * (mod + neg[i]) % mod;

    assert(false);
    cout << ans << endl;
    return 0;
  }

  ll ans = 1;
  int s = 0, t = 0;

  if (K % 2) {
    ans = pos[s++];
    K--;
  }

  for (int i = 0; i < K / 2; i++) {
    ll A = s + 1 < pos.size() ? (ll)pos[s] * pos[s + 1] : 0;
    ll B = t + 1 < neg.size() ? (ll)neg[t] * neg[t + 1] : 0;

    if (A > B) {
      ans = ans * (A % mod) % mod;
      s += 2;
    } else {
      ans = ans * (B % mod) % mod;
      t += 2;
    }
  }

  cout << ans << endl;

  return 0;
}