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 __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int find_mod(int x) {
  if (x >= 0)
    return x % MOD;
  return (MOD + x % MOD) % MOD;
}

int32_t main() {
  // IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n, k;
    cin >> n >> k;
    vi vp;
    vi vn;
    int flag = 0;
    int isp = 0;
    rep(i, 1, n) {
      int x;
      cin >> x;
      if (x == 0)
        isp++;
      if (x > 0)
        flag = 1;
      if (x < 0)
        vn.pb(abs(x));
      else
        vp.pb(x);
    }
    if (k == n) {
      int ans = 1;
      for (int x : vp) {
        ans = ans * x;
        ans = find_mod(ans);
      }
      for (int x : vn) {
        ans = ans * (-1 * x);
        ans = find_mod(ans);
      }
      cout << ans << endl;
      return 0;
    }
    if (flag == 0) {
      if (isp > 0 && k % 2 == 1) {
        cout << "0" << endl;
        return 0;
      }
      if (isp == 0 && k % 2 == 1) {
        sort(all(vn));
        int ans = 1;
        for (int i = 0; i < k; i++) {
          ans = ans * (-1 * vn[i]);
          ans = find_mod(ans);
          // deb1(ans);
        }
        cout << ans << endl;
        return 0;
      }
      if (vn.size() >= k) {
        sort(all(vn));
        reverse(all(vn));
        int ans = 1;
        for (int i = 0; i < k; i++) {
          ans = ans * (vn[i]);
          ans = find_mod(ans);
          // deb1(ans);
        }
        cout << ans << endl;
        return 0;
      } else {
        cout << "0" << endl;
        return 0;
      }
    }
    int ans = 1;
    sort(all(vn));
    sort(all(vp));
    reverse(all(vn));
    reverse(all(vp));
    vi vv;
    int i = 0;
    int j = 0;
    if (k % 2 == 1 && i < vp.size()) {
      vv.pb(vp[i]);
      i++;
      k--;
    }
    while (k > 0 && i + 1 < vp.size() && j + 1 < vn.size()) {
      if (vp[i] * vp[i + 1] > vn[j] * vn[j + 1]) {
        vv.pb(vp[i]);
        vv.pb(vp[i + 1]);
        i += 2;
        k -= 2;
      } else {
        vv.pb(vn[j]);
        vv.pb(vn[j + 1]);
        j += 2;
        k -= 2;
      }
    }
    while (k > 1 && j + 1 < vn.size()) {
      vv.pb(vn[j]);
      vv.pb(vn[j + 1]);
      j++;
      j++;
      k--;
      k--;
    }
    while (k > 0 && i < vp.size()) {
      vv.pb(vp[i]);
      i++;
      k--;
    }

    // trace(vv);
    for (int x : vv) {
      ans = (ans * x) % 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>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int find_mod(int x) {
  if (x >= 0)
    return x % MOD;
  return (MOD + x % MOD) % MOD;
}

int32_t main() {
  // IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n, k;
    cin >> n >> k;
    vi vp;
    vi vn;
    int flag = 0;
    rep(i, 1, n) {
      int x;
      cin >> x;
      if (x > 0)
        flag = 1;
      if (x < 0)
        vn.pb(abs(x));
      else
        vp.pb(x);
    }
    if (k == n) {
      int ans = 1;
      for (int x : vp) {
        ans = ans * x;
        ans = find_mod(ans);
      }
      for (int x : vn) {
        ans = ans * (-1 * x);
        ans = find_mod(ans);
      }
      cout << ans << endl;
      return 0;
    }
    if (flag == 0) {
      int ans = 1;
      vi vnn;
      for (int x : vn) {
        vnn.pb(-1 * x);
      }
      sort(all(vnn));
      reverse(all(vnn));
      for (int i = 0; i < k; i++) {
        ans = ans * (vnn[i]);
        ans = find_mod(ans);
      }
      cout << ans << endl;
      return 0;
    }
    int ans = 1;
    sort(all(vn));
    sort(all(vp));
    reverse(all(vn));
    reverse(all(vp));
    vi vv;
    int i = 0;
    int j = 0;
    if (k % 2 == 1 && i < vp.size()) {
      vv.pb(vp[i]);
      i++;
      k--;
    }
    while (k > 0 && i + 1 < vp.size() && j + 1 < vn.size()) {
      if (vp[i] * vp[i + 1] > vn[j] * vn[j + 1]) {
        vv.pb(vp[i]);
        vv.pb(vp[i + 1]);
        i += 2;
        k -= 2;
      } else {
        vv.pb(vn[j]);
        vv.pb(vn[j + 1]);
        j += 2;
        k -= 2;
      }
    }
    while (k > 0 && i < vp.size()) {
      vv.pb(vp[i]);
      i++;
      k--;
    }
    while (k > 0 && j < vn.size()) {
      vv.pb(vn[j]);
      j++;
      k--;
    }
    // trace(vv);
    for (int x : vv) {
      ans = (ans * x) % MOD;
    }
    cout << ans << endl;
  }
}