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>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
using less_set = tree<int, null_type, less<int>, rb_tree_tag,
                      tree_order_statistics_node_update>;
using great_set = tree<int, null_type, greater<int>, rb_tree_tag,
                       tree_order_statistics_node_update>;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int N = 2e3 + 69;
#define ALL(x) begin(x), end(x)
#define sz(x) (ll) x.size()
const int OO = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, k, tmp, x;
ll arr[200069];
vector<ll> ans;
deque<ll> pos;
deque<ll> neg;
bool z = 0;
int main() {
  FIO ll sum = 1LL;
  cin >> n >> k;
  tmp = k;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x < 0) {
      neg.push_back(x);
    } else if (x > 0) {
      pos.push_back(x);
    } else
      z = 1;
    ans.push_back(x);
  }
  if (n == k) {
    for (ll g : ans) {
      sum = ((sum % mod) * (g % mod)) % mod;
    }
    cout << (sum % mod + mod) % mod << "\\n";
    return 0;
  }
  ans.clear();
  sort(ALL(pos), greater<ll>());
  sort(ALL(neg));
  ll mnpos = 99999999999;
  for (int o = 0; o < k; o++) {
    if (sz(pos) && sz(neg) >= 2) {
      if (pos[0] * pos[1] >= neg[0] * neg[1]) {
        ans.push_back(pos[0]);
        mnpos = min(pos[0], mnpos);
        pos.pop_front();
        continue;
      } else if (o < k - 1) {
        ans.push_back(neg[0]);
        ans.push_back(neg[1]);
        neg.pop_front();
        neg.pop_front();
        o++;
        continue;
      }
    }
    if (sz(pos)) {
      ans.push_back(pos[0]);
      mnpos = min(pos[0], mnpos);
      pos.pop_front();
      continue;
    }
    if (sz(neg) >= 2 && o < k - 1) {
      // cout << o << "\\n";
      ans.push_back(neg[0]);
      ans.push_back(neg[1]);
      neg.pop_front();
      neg.pop_front();
      o++;
      continue;
    }
  }
  // cout << sz(ans) << "\\n";
  if (sz(ans) == tmp) {
    // cout << "here1\\n";
    for (ll g : ans) {
      sum = ((sum % mod) * (g % mod)) % mod;
    }
    cout << (sum % mod + mod) % mod << "\\n";
    return 0;
  }
  if (tmp - sz(ans) > sz(neg)) {
    cout << 0 << "\\n";
    return 0;
  }
  if (sz(neg) == 1) {
    if (z)
      cout << 0 << "\\n";
    else {
      // cout << "here2\\n";
      ans.push_back(neg[0]);
      for (ll g : ans) {
        sum = ((sum % mod) * (g % mod)) % mod;
      }
      cout << (sum % mod + mod) % mod << "\\n";
    }

    return 0;
  }
  // cout << sz(neg) << "\\n";
  bool good = 0;
  for (int i = 0; i < sz(ans); i++) {
    if (ans[i] == mnpos) {
      ans[i] = 1;
      good = 1;
      break;
    }
  }
  if (good) {
    ans.push_back(neg[0]);
    ans.push_back(neg[1]);
  } else if (z) {
    cout << 0 << "\\n";
    return 0;
  } else {
    for (int i = 0; i < sz(neg); i++) {
      if (i < sz(neg) - 1)
        ans[i] = 1;
      ans.push_back(neg[i]);
    }
  }
  for (ll g : ans) {
    // cout << g << " " ;
    sum = ((sum % mod) * (g % mod)) % mod;
  }
  // cout << "\\n";
  // cout << sum << "\\n";
  cout << (sum % mod + mod) % mod << "\\n";
  return 0;
}


Error: WA

Fixed submission:

// In the name of God

#include <bits/stdc++.h>

using namespace std;

constexpr int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N, k;
  cin >> N >> k;

  vector<long long> pos, neg;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x > 0) {
      pos.push_back(x);
    } else if (x < 0) {
      neg.push_back(-x);
    }
  }

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  reverse(pos.begin(), pos.end());
  reverse(neg.begin(), neg.end());

  int idx1 = 0, idx2 = 0;

  int n = pos.size();
  int m = neg.size();

  if (n + m < k) {
    cout << 0;
    exit(0);
  }

  int go = n & 1 ? n - 1 : n;

  if (go < k && (k - go) % 2 == 1) {

    if (n + m != N) {
      cout << 0;
    } else {
      vector<long long> v;
      for (long long x : pos)
        v.push_back(x);
      for (long long x : neg)
        v.push_back(x);
      sort(v.begin(), v.end());

      long long ans = 1;

      for (int i = 0; i < k; i++) {
        ans = (ans * v[i]) % mod;
      }

      cout << (mod - ans) % mod;
    }
    exit(0);
  }

  while (idx1 + idx2 < k) {
    long long p1 = idx1 < n ? pos[idx1] : 0;
    long long p2 = idx2 < m ? neg[idx2] : 0;
    if (p1 >= p2) {
      idx1++;
    } else
      idx2++;
  }

  if (idx2 & 1) {
    if (idx2 == m) {
      // can not take more negatives
      // only option is to delete one negative, and take one more positive
      assert(idx1 != n);
      idx2--;
      idx1++;
    } else if (idx1 == n) {
      // can not take more positives
      // only option is to delete one positive and take one more negative
      assert(idx2 != m);
      idx1--;
      idx2++;
    } else {
      // i can do either of them
      pair<long long, long long> op1 = {neg[idx2 - 1], pos[idx1]};
      pair<long long, long long> op2 = {pos[idx1 - 1], neg[idx2]};
      if (op1.second * op2.first > op2.second * op1.first) {
        idx2--;
        idx1++;
      } else {
        idx1--;
        idx2++;
      }
    }
  }

  long long ans = 1;

  for (int i = 0; i < idx1; i++) {
    ans = (ans * pos[i]) % mod;
  }

  for (int i = 0; i < idx2; i++) {
    ans = (ans * neg[i]) % mod;
  }

  cout << ans;

  return 0;
}