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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
ll in() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  return x * f;
}
vector<ll> a, b;
ll c[N], n, k;
int main() {
  n = in(), k = in();
  for (int i = 1, x; i <= n; i++) {
    c[i] = x = in();
    if (x > 0)
      a.push_back(x);
    else
      b.push_back(-x);
  }
  if (n == k) {
    ll ans = 1;
    for (int i = 1; i <= n; i++)
      ans = (ans * c[i] % mod + mod) % mod;
    cout << (ans % mod + mod) % mod << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  ll ans = 1;
  int p1 = a.size() - 1, p2 = b.size() - 1;
  if (k % 2) {
    if (a.size())
      ans = a[p1--];
    else {
      for (int i = 0; i < k; i++)
        ans = (ans * (-b[i]) % mod + mod) % mod;
      cout << (ans % mod + mod) % mod << endl;
      return 0;
    }
  }
  for (int i = 0; i < k / 2; i++) {
    ll left = 0, right = 0;
    if (p1 >= 1)
      left = 1ll * a[p1] * a[p1 - 1];
    if (p2 >= 1)
      right = 1ll * b[p2] * b[p2 - 1];
    if (left > right)
      left %= mod, ans = (ans * left % mod), p1 -= 2;
    else
      right %= mod, ans = (ans * right % mod) % mod, p2 -= 2;
  }
  cout << (ans % mod + mod) % mod << endl;
  return 0;
}
/*
 */


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
ll in() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  return x * f;
}
vector<ll> a, b;
ll c[N], n, k;
int main() {
  n = in(), k = in();
  for (int i = 1, x; i <= n; i++) {
    c[i] = x = in();
    if (x > 0)
      a.push_back(x);
    else
      b.push_back(-x);
  }
  if (n == k) {
    ll ans = 1;
    for (int i = 1; i <= n; i++)
      ans = (ans * c[i]) % mod;
    cout << ans << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  ll ans = 1;
  int p1 = a.size() - 1, p2 = b.size() - 1;
  if (k % 2) {
    if (a.size())
      ans = a[p1--];
    else {
      for (int i = 0; i < k; i++)
        ans = (ans * (-b[i]) % mod + mod) % mod;
      cout << (ans % mod + mod) % mod << endl;
      return 0;
    }
  }
  for (int i = 0; i < k / 2; i++) {
    ll left = 0, right = 0;
    if (p1)
      left = a[p1] * a[p1 - 1];
    if (p2)
      right = b[p2] * b[p2 - 1];
    if (left > right)
      ans = (ans * left % mod), p1 -= 2;
    else
      ans = (ans * right) % mod, p2 -= 2;
  }
  cout << ans << endl;
  return 0;
}
/*
 */