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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
inline void mul(ll &x, ll y) { x = (x * y % mod + mod) % mod; }
vector<ll> vec[2];
int n, k;
int main() {
  read(n);
  read(k);
  ll x;
  bool f = 0;
  for (int i = 1; i <= n; i++) {
    read(x);
    if (x > 0)
      vec[0].push_back(x);
    if (x < 0)
      vec[1].push_back(x);
    if (x == 0)
      f = 1;
  }
  for (int i = 0; i < 2; i++)
    sort(vec[i].begin(), vec[i].end());

  if (vec[0].size() + vec[1].size() < k) {
    puts("0");
    return 0;
  }

  if ((vec[0].empty() && k % 2 == 1)) {
    if (f) {
      puts("0");
      return 0;
    }
    reverse(vec[1].begin(), vec[1].end());

    ll ans = 1;
    for (int i = 0; i < k; i++)
      mul(ans, vec[1][i]);
    writeln(ans);
    return 0;
  }
  if (vec[0].size() + vec[1].size() == k) {
    if (vec[1].size() % 2 == 1 && f) {
      puts("0");
      return 0;
    }
    ll ans = 1;
    for (int i = 0; i < vec[0].size(); i++)
      mul(ans, vec[0][i]);
    for (int i = 0; i < vec[1].size(); i++)
      mul(ans, vec[1][i]);
    writeln(ans);
    return 0;
  }

  if (vec[1].size() & 1)
    vec[1].pop_back();
  for (int i = 0; i < vec[1].size(); i++)
    vec[1][i] = -vec[1][i];

  ll ans = 1;
  if (k & 1) {
    mul(ans, vec[0].back());
    vec[0].pop_back();
    k--;
  }
  reverse(vec[0].begin(), vec[0].end());
  if (vec[0].size() & 1)
    vec[0].pop_back();
  int posa = 0, posb = 0;

  while (k) {
    k -= 2;
    if (posa >= vec[0].size()) {
      mul(ans, vec[1][posb]);
      mul(ans, vec[1][posb + 1]);
      posb += 2;
    } else if (posb >= vec[1].size()) {
      mul(ans, vec[0][posa]);
      mul(ans, vec[0][posa + 1]);
      posa += 2;
    } else {
      ll B = vec[1][posb] * vec[1][posb + 1];
      ll A = vec[0][posa] * vec[0][posa + 1];
      if (A > B) {
        mul(ans, vec[0][posa]);
        mul(ans, vec[0][posa + 1]);
        posa += 2;
      } else {
        mul(ans, vec[1][posb]);
        mul(ans, vec[1][posb + 1]);
        posb += 2;
      }
    }
  }
  writeln(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
inline void mul(ll &x, ll y) { x = (x * y % mod + mod) % mod; }
vector<int> vec[2];
int n, k;
int main() {
  read(n);
  read(k);
  int x;
  for (int i = 1; i <= n; i++) {
    read(x);
    if (x > 0)
      vec[0].push_back(x);
    else
      vec[1].push_back(x);
  }
  for (int i = 0; i < 2; i++)
    sort(vec[i].begin(), vec[i].end());

  if (vec[0].size() + vec[1].size() < k) {
    puts("0");
    return 0;
  }

  if ((vec[0].empty() && k % 2 == 1)) {
    reverse(vec[1].begin(), vec[1].end());

    ll ans = 1;
    for (int i = 0; i < k; i++)
      mul(ans, vec[1][i]);
    writeln(ans);
    return 0;
  }
  if (vec[0].size() + vec[1].size() == k) {
    ll ans = 1;
    for (int i = 0; i < vec[0].size(); i++)
      mul(ans, vec[0][i]);
    for (int i = 0; i < vec[1].size(); i++)
      mul(ans, vec[1][i]);
    writeln(ans);
    return 0;
  }

  if (vec[1].size() & 1)
    vec[1].pop_back();
  for (int i = 0; i < vec[1].size(); i++)
    vec[1][i] = -vec[1][i];

  ll ans = 1;
  if (k & 1) {
    mul(ans, vec[0].back());
    vec[0].pop_back();
    k--;
  }
  reverse(vec[0].begin(), vec[0].end());
  if (vec[0].size() & 1)
    vec[0].pop_back();
  int posa = 0, posb = 0;

  while (k) {
    k -= 2;
    if (posa > vec[0].size()) {
      mul(ans, vec[1][posb]);
      mul(ans, vec[1][posb + 1]);
      posb += 2;
    } else if (posb > vec[1].size()) {
      mul(ans, vec[0][posa]);
      mul(ans, vec[0][posa + 1]);
      posa += 2;
    } else {
      ll B = vec[1][posb] * vec[1][posb + 1];
      ll A = vec[0][posa] * vec[0][posa + 1];
      if (A > B) {
        mul(ans, vec[0][posa]);
        mul(ans, vec[0][posa + 1]);
        posa += 2;
      } else {
        mul(ans, vec[1][posb]);
        mul(ans, vec[1][posb + 1]);
        posb += 2;
      }
    }
  }
  writeln(ans);
  return 0;
}