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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 100000
#define MAX1 10000
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

vector<ll> a[2];
int m[2];

ll mul(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  if (y < 0) {
    y += MOD;
  }
  if (x < 0) {
    x += MOD;
  }
  ll z = x * y;
  if (z >= MOD) {
    z %= MOD;
  }
  return z;
}

ll ok(int &i, int &j) {
  ll b1, b2;
  if (i > 0 and j + 1 < m[1]) {
    b1 = a[0][i] * a[0][i - 1];
    b2 = a[1][j] * a[1][j + 1];
    if (b1 > b2) {
      i -= 2;
      return b1;
    } else {
      j += 2;
      return b2;
    }
  } else if (i > 0) {
    b1 = a[0][i] * a[0][i - 1];
    i -= 2;
    return b1;
  } else if (j + 1 < m[1]) {
    b2 = a[1][j] * a[1][j + 1];
    j += 2;
    return b2;
  } else {
    b1 = a[0][i] * a[1][j];
    i--, j++;
    return b1;
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n, k, i, x, j;
  ll ans = 1;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> x;
    a[x < 0].push_back(x);
    m[x < 0]++;
  }
  sort(a[0].begin(), a[0].end());
  sort(a[1].begin(), a[1].end());
  if (k & 1) {
    if (a[0].empty()) {
      for (i = m[1] - 1; i >= m[1] - k; --i) {
        ans = mul(ans, a[1][i]);
      }
      k = 0;
    } else {
      ans = a[0].back();
      a[0].pop_back();
      k--;
      m[0]--;
    }
  }
  // k is even
  i = m[0] - 1, j = 0;
  while (k > 0) {
    ans = mul(ans, ok(i, j));
    k -= 2;
  }
  cout << ans;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 100000
#define MAX1 10000
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

vector<ll> a[2];

ll mul(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  if (y < 0) {
    y += MOD;
  }
  if (x < 0) {
    x += MOD;
  }
  ll z = x * y;
  if (z >= MOD) {
    z %= MOD;
  }
  return z;
}

ll ok(int &i, int &j) {
  if (i > 0 and j + 1 < a[1].size()) {
    ll b1 = a[0][i] * a[0][i - 1];
    ll b2 = a[1][j] * a[1][j + 1];
    if (b1 > b2) {
      i -= 2;
      return b1;
    } else {
      j += 2;
      return b2;
    }
  } else if (i > 0) {
    ll b1 = a[0][i] * a[0][i - 1];
    i -= 2;
    return b1;
  } else if (j + 1 < a[1].size()) {
    ll b2 = a[1][j] * a[1][j + 1];
    j += 2;
    return b2;
  } else {
    i--, j++;
    return a[0][i] * a[1][j];
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n, k, i, x, j;
  ll ans = 1;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> x;
    a[x < 0].push_back(x);
  }
  sort(a[0].begin(), a[0].end());
  sort(a[1].begin(), a[1].end());
  if (k & 1) {
    if (a[0].empty()) {
      for (i = a[1].size() - 1; i >= a[1].size() - k; --i) {
        ans = mul(ans, a[1][i]);
      }
      k = 0;
    } else {
      ans = a[0].back();
      a[0].pop_back();
      k--;
    }
  }
  // k is even
  i = a[0].size() - 1, j = 0;
  while (k > 0) {
    ans = mul(ans, ok(i, j));
    k -= 2;
  }
  cout << ans;

  return 0;
}