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

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
// #define MULTIPLE_TEST_CASE
void solution() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, 0, n, 1) cin >> v[i];
  sort(all(v));
  int ans = 1;
  if (v[0] >= 0) {
    for (int i = 0; i < k; i++)
      ans = mod(ans * v[n - 1 - i]);
    cout << ans << endl;
    return;
  }
  if (v[n - 1] <= 0) {
    for (int i = 0; i < k; i++) {
      ans = mod(ans * v[(k % 2 ? n - 1 - i : i)]);
    }
    cout << ans << endl;
    return;
  }

  int l = 0, r = n - 1;
  if (k % 2) {
    ans = mod(ans * v[r]);
    r--;
    k--;
  }

  while (k > 0) {
    int ll = v[l] * v[l + 1];
    int rr = v[r] * v[r - 1];

    if (ll >= rr) {
      ans = mod(ans * mod(ll));
      l += 2;
    } else {
      ans = mod(ans * (mod(rr)));
      r -= 2;
    }
    k -= 2;
  }

  cout << ans << endl;
}

int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
// #define MULTIPLE_TEST_CASE
void solution() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  int zeros = 0;
  rep(i, 0, n, 1) {
    int x;
    cin >> x;
    if (x == 0)
      zeros++;
    else
      v.pb(x);
  }
  sort(all(v), greater<int>());
  if (v.size() < k) {
    cout << 0 << endl;
    return;
  }
  multiset<int> neg, pos;
  for (int x : v) {
    if (x < 0)
      neg.insert(x);
    else if (x > 0)
      pos.insert(x);
  }

  // entope de negativo esse troço
  multiset<int> ans;
  while (neg.size() > 0 && ans.size() < k) {
    ans.insert(*neg.begin());
    neg.erase(neg.begin());
  }

  if (ans.size() % 2 != 0 && pos.size() == 0) {
    if (zeros > 0) {
      cout << 0 << endl;
      return;
    }

    vector<int> arr;
    for (int x : ans)
      arr.pb(x);
    for (int x : neg)
      arr.pb(x);
    sort(all(arr), greater<int>());
    int tot = 1;
    for (int i = 0; i < k; i++)
      tot = mod(tot * arr[i]);
    cout << tot << endl;
  } else {
    multiset<int> anspos;
    // tira o negativozin
    bool bo = ans.size() % 2;
    while (ans.size() + anspos.size() < k) {
      if (pos.size() == 0)
        break;
      anspos.insert(*pos.rbegin());
      pos.erase(pos.find(*pos.rbegin()));
    }
    while (ans.size() >= 2 && pos.size() >= 2 + bo) {
      int x1 = *ans.rbegin();
      ans.erase(ans.find(*ans.rbegin()));
      int x2 = *ans.rbegin();
      ans.erase(ans.find(*ans.rbegin()));

      int y1 = *pos.rbegin();
      pos.erase(pos.find(*pos.rbegin()));
      int y2 = *pos.rbegin();
      pos.erase(pos.find(*pos.rbegin()));

      if (y1 * y2 >= x1 * x2) {
        anspos.insert(y1);
        anspos.insert(y2);
      } else {
        ans.insert(x1);
        ans.insert(x2);
        break;
      }
    }

    if (bo) {
      ans.erase(ans.find(*ans.rbegin()));
      anspos.insert(*pos.rbegin());
      pos.erase(pos.find(*pos.rbegin()));
    }

    if (anspos.size() + ans.size() < k) {
      cout << 0 << endl;
      return;
    }

    int tot = 1;
    for (int x : ans) {
      tot = mod(tot * x);
    }
    for (int x : anspos) {
      tot = mod(tot * x);
    }
    cout << tot << endl;
  }
}

int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}