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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288
#define MOD 1000000007
#define INF -100000000000000
#define ordered_set                                                            \\
  tree<pair<ll int, ll int>, null_type, less<pair<ll int, ll int>>,            \\
       rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#define lld long double
using namespace __gnu_pbds;
struct Comparator {
  bool operator()(const std::pair<ll int, ll int> &a,
                  const std::pair<ll int, ll int> &b) const {
    if (a.F > b.F)
      return true;
    else
      return false;
  }
};
int main() {
  fast;
  //  #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll int n, k;
  cin >> n >> k;
  ll int a[n];
  ll int i, s1 = 0, s2 = 0, zero = 0, left;
  ll int ans = 1;
  vector<ll int> neg, pos;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      neg.PB(abs(a[i]));
    } else if (a[i] > 0)
      pos.PB(a[i]);
    else
      zero++;
  }
  if (n == k) {
    ans = 1;
    for (i = 0; i < n; i++)
      ans = (ans * a[i]) % MOD;
    cout << (ans + MOD) % MOD;
    exit(0);
  }
  sort(neg.begin(), neg.end(), greater<ll int>());
  sort(pos.begin(), pos.end(), greater<ll int>());
  if (k > (neg.size() + pos.size())) {
    cout << "0\\n";
  } else if (k == pos.size() + neg.size()) {

    if (zero == 0) {
      ans = 1;
      for (i = 0; i < n; i++)
        ans = (ans * a[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    }
    if (k % 2 == 0 && (pos.size() % 2 != 0 || neg.size() % 2 != 0)) {
      ans = 1;
      if (zero != 0)
        cout << "0";
      exit(0);
    } else if (k % 2 == 0) {
      ans = 1;
      for (i = 0; i < pos.size(); i++)
        ans = (ans * pos[i]) % MOD;
      for (i = 0; i < neg.size(); i++)
        ans = (ans * neg[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    }
    if (k % 2 != 0 && neg.size() % 2 == 0) {
      ans = 1;
      for (i = 0; i < pos.size(); i++)
        ans = (ans * pos[i]) % MOD;
      for (i = 0; i < neg.size(); i++)
        ans = (ans * neg[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    } else if (k % 2 != 0 && pos.size() % 2 == 0) {
      if (zero != 0)
        cout << "0";
      exit(0);
    }

  } else if (pos.size() == 0) {
    if (k % 2 != 0 && zero != 0)
      cout << 0 << endl;
    else if (k % 2 != 0 && zero == 0) { // cout<<"jj";
      ans = 1;
      for (i = neg.size() - 1; i >= 0; i--) {
        ans = (ans * (-1) * neg[i]) % MOD;
        k--;
        if (k == 0)
          break;
      }

      cout << (ans + MOD) % MOD << endl;

    } else {
      ans = 1;
      for (i = 0; i < k; i++)
        ans = (ans * neg[i]) % MOD;
      cout << ans << endl;
    }

  } else if (neg.size() == 0) {
    ans = 1;
    for (i = 0; i < k; i++)
      ans = (ans * pos[i]) % MOD;
    cout << ans << endl;
    exit(0);
  } else {
    s1 = 0;
    s2 = 0;
    left = k;
    if (k % 2 != 0) {
      ans = pos[0];
      s1++;
      left--;
    }

    while (left != 0) {
      if (pos.size() - (s1 + 1) < 1) {
        s2 += 2;
        left -= 2;
        continue;
      }
      if (neg.size() - (s2 + 1) < 1) {
        s1 += 2;
        left -= 2;
        continue;
      }
      ll int w = pos[s1] * pos[s1 + 1];
      ll int q = neg[s2] * neg[s2 + 1];
      if (w >= q) {
        s1 += 2;
        left -= 2;
        continue;
      } else {
        s2 += 2;
        left -= 2;
        continue;
      }
    }
    ans = 1;
    for (i = 0; i < s1; i++)
      ans = (ans * pos[i]) % MOD;
    for (i = 0; i < s2; i++)
      ans = (ans * neg[i]) % MOD;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288
#define MOD 1000000007
#define INF -100000000000000
#define ordered_set                                                            \\
  tree<pair<ll int, ll int>, null_type, less<pair<ll int, ll int>>,            \\
       rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#define lld long double
using namespace __gnu_pbds;
struct Comparator {
  bool operator()(const std::pair<ll int, ll int> &a,
                  const std::pair<ll int, ll int> &b) const {
    if (a.F > b.F)
      return true;
    else
      return false;
  }
};
int main() {
  fast;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll int n, k;
  cin >> n >> k;
  ll int a[n];
  ll int i, s1 = 0, s2 = 0, zero = 0, left;
  ll int ans = 1;
  vector<ll int> neg, pos;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      neg.PB(abs(a[i]));
    } else if (a[i] > 0)
      pos.PB(a[i]);
    else
      zero++;
  }
  if (n == k) {
    ans = 1;
    for (i = 0; i < n; i++)
      ans = (ans * a[i]) % MOD;
    cout << (ans + MOD) % MOD;
    exit(0);
  }
  sort(neg.begin(), neg.end(), greater<ll int>());
  sort(pos.begin(), pos.end(), greater<ll int>());
  if (k > (neg.size() + pos.size())) {
    cout << "0\\n";
  } else if (k == pos.size() + neg.size()) {

    if (zero == 0) {
      ans = 1;
      for (i = 0; i < n; i++)
        ans = (ans * a[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    }
    if (k % 2 == 0 && (pos.size() % 2 != 0 || neg.size() % 2 != 0)) {
      ans = 1;
      if (zero != 0)
        cout << "0";
      exit(0);
    } else if (k % 2 == 0) {
      ans = 1;
      for (i = 0; i < pos.size(); i++)
        ans = (ans * pos[i]) % MOD;
      for (i = 0; i < neg.size(); i++)
        ans = (ans * neg[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    }
    if (k % 2 != 0 && neg.size() % 2 == 0) {
      ans = 1;
      for (i = 0; i < pos.size(); i++)
        ans = (ans * pos[i]) % MOD;
      for (i = 0; i < neg.size(); i++)
        ans = (ans * neg[i]) % MOD;
      cout << (ans + MOD) % MOD << endl;
      exit(0);
    } else if (k % 2 != 0 && pos.size() % 2 == 0) {
      if (zero != 0)
        cout << "0";
      exit(0);
    }

  } else if (pos.size() == 0) { // cout<<-6%MOD;
    if (k % 2 != 0 && zero != 0)
      cout << 0 << endl;
    else if (k % 2 != 0 && zero == 0) { // cout<<"jj";
      ans = 1;
      for (i = neg.size() - 1; i >= 0; i--) {
        ans = (ans * (-1) * neg[i]) % MOD;
        k--;
        if (k == 0)
          break;
      }

      cout << (ans + MOD) % MOD << endl;

    } else {
      ans = 1;
      for (i = 0; i < k; i++)
        ans = (ans * neg[i]) % MOD;
      cout << ans << endl;
    }

  } else {
    s1 = 0;
    s2 = 0;
    left = k;
    if (k % 2 != 0) {
      ans = pos[0];
      s1++;
      left--;
    }

    while (left != 0) {
      if (pos.size() - (s1 + 1) < 2) {
        s2 += 2;
        left -= 2;
        continue;
      }
      if (neg.size() - (s2 + 1) < 2) {
        s1 += 2;
        left -= 2;
        continue;
      }
      ll int w = pos[s1] * pos[s1 + 1];
      ll int q = neg[s2] * neg[s2 + 1];
      if (w >= q) {
        s1 += 2;
        left -= 2;
        continue;
      } else {
        s2 += 2;
        left -= 2;
        continue;
      }
    }
    ans = 1;
    for (i = 0; i < s1; i++)
      ans = (ans * pos[i]) % MOD;
    for (i = 0; i < s2; i++)
      ans = (ans * neg[i]) % MOD;
    cout << ans << endl;
  }
}