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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
vector<ll> a, b, c;
bool cmp(ll a, ll b) { return a > b; }

int main() {
  int n, k;
  ll x, ans = 1;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    if (x > 0)
      a.push_back(x);
    if (x < 0)
      c.push_back(x);
    if (x == 0)
      b.push_back(x);
  }
  sort(a.begin(), a.end(), cmp);
  sort(c.begin(), c.end());
  if (k == n) {
    if (b.size() != 0) {
      ans = 0;
      cout << ans << endl;
      return 0;
    } else {
      for (int i = 0; i < a.size(); ++i) {
        ans *= a[i], ans %= mod;
      }
      for (int i = 0; i < c.size(); ++i) {
        ans *= c[i], ans %= mod;
      }
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }
  } else {
    if (a.size() + c.size() < k) {
      ans = 0;
      cout << ans << endl;
      return 0;
    }
    if (a.size() == 0) {
      if (c.size() >= k) {
        if (k & 1) {
          if (b.size() == 0) {
            for (int i = 1; i <= k; ++i) {
              ans *= c[c.size() - i], ans += mod, ans %= mod;
            }
            cout << ans << endl;
            return 0;
          } else {
            cout << 0 << endl;
            return 0;
          }
        } else {
          for (int i = 0; i < k; ++i) {
            ans *= c[i], ans %= mod;
          }
          cout << ans << endl;
          return 0;
        }
      }
    } else {
      int al = 0, cl = 0;
      ll ans = 1;
      if (k & 1) {
        ans *= a[al++];
        k--;
        while (k > 1) {
          if (cl + 2 > c.size() || a[al] * a[al + 1] > c[cl] * c[cl + 1])
            ans *= a[al] % mod * a[al + 1] % mod, ans %= mod, al += 2;
          else
            ans *= c[cl] % mod * c[cl + 1] % mod, ans += mod, ans %= mod,
                cl += 2;
          k -= 2;
        }
      } else {
        while (k) {
          if (cl + 2 > c.size() || a[al] * a[al + 1] > c[cl] * c[cl + 1])
            ans *= a[al] % mod * a[al + 1] % mod, ans %= mod, al += 2;
          else
            ans *= c[cl] % mod * c[cl + 1] % mod, ans += mod, ans %= mod,
                cl += 2;
          k -= 2;
        }
      }
      cout << ans << endl;
      return 0;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
vector<ll> a, b, c;
bool cmp(ll a, ll b) { return a > b; }

int main() {
  int n, k;
  ll x, ans = 1;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    if (x > 0)
      a.push_back(x);
    if (x < 0)
      c.push_back(x);
    if (x == 0)
      b.push_back(x);
  }
  sort(a.begin(), a.end(), cmp);
  sort(c.begin(), c.end());
  if (k == n) {
    if (b.size() != 0) {
      ans = 0;
      cout << ans << endl;
      return 0;
    } else {
      for (int i = 0; i < a.size(); ++i) {
        ans *= a[i], ans %= mod;
      }
      for (int i = 0; i < c.size(); ++i) {
        ans *= c[i], ans %= mod;
      }
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }
  } else {
    if (a.size() + c.size() < k) {
      ans = 0;
      cout << ans << endl;
      return 0;
    }
    if (a.size() == 0) {
      if (c.size() >= k) {
        if (k & 1) {
          if (b.size() == 0) {
            for (int i = 1; i <= k; ++i) {
              ans *= c[c.size() - i], ans += mod, ans %= mod;
            }
            cout << ans << endl;
            return 0;
          } else {
            cout << 0 << endl;
            return 0;
          }
        } else {
          for (int i = 0; i < k; ++i) {
            ans *= c[i], ans %= mod;
          }
          cout << ans << endl;
          return 0;
        }
      }
    } else {
      int al = 0, cl = 0;
      ll ans = 1;
      if (k & 1) {
        ans *= a[al++];
        k--;
        while (k > 1) {
          if (cl + 2 > c.size() || a[al] * a[al + 1] > c[cl] * c[cl + 1])
            ans *= a[al] % mod * a[al + 1] % mod, ans %= mod, al += 2;
          else
            ans *= c[cl] % mod * c[cl + 1] % mod, ans += mod, ans %= mod,
                cl += 2;
          k -= 2;
        }
        ans *= a[cl];
      } else {
        while (k) {
          if (cl + 2 > c.size() || a[al] * a[al + 1] > c[cl] * c[cl + 1])
            ans *= a[al] % mod * a[al + 1] % mod, ans %= mod, al += 2;
          else
            ans *= c[cl] % mod * c[cl + 1] % mod, ans += mod, ans %= mod,
                cl += 2;
          k -= 2;
        }
      }
      cout << ans << endl;
      return 0;
    }
  }
}