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

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define TRACE(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "]";
  err(++it, args...);
}

#define MAXN ((int)2e5 + 5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<int, int>
#define mid ((l + r) / 2)

int n, k;
vector<ll> neg, pos, neg2, pick;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a > 0)
      pos.pb(a);
    else
      neg.pb(a);
  }

  if (n == k) {
    ll ans = 1;
    for (ll x : pos)
      ans = (ans * (x)) % MOD;
    for (ll x : neg)
      ans = (ans * (x)) % MOD;

    cout << (ans + MOD) % MOD << endl;
  } else if ((pos.size() == 0) and (k % 2)) {
    ll ans = 1;
    sort(neg.rbegin(), neg.rend());
    for (int i = 0; i < k; ++i)
      ans = (ans * (neg[i])) % MOD;
    cout << (ans + MOD) % MOD << endl;
  } else {
    sort(all(pos));
    sort(all(neg));
    for (int i = 0; i < (int)neg.size() - 1; i += 2)
      neg2.pb(neg[i] * neg[i + 1]);
    sort(all(neg2));

    int t = 0;
    int pt = pos.size() - 1;
    int nt = neg2.size() - 1;

    if (k % 2) {
      pick.pb(pos[pt--]);
      t++;
    }

    while (t + 2 <= k and nt >= 0 and pt > 0) {
      if (neg2[nt] > (pos[pt] * pos[pt - 1]))
        pick.pb(neg2[nt--]);
      else {
        pick.pb((pos[pt] * pos[pt - 1]));
        pt -= 2;
      }

      t += 2;
    }

    if (nt < 0) // pick all pos
    {
      while (t < k) {
        t++;
        pick.pb(pos[pt--]);
      }
    } else if (pt <= 0) {
      while (t < k) {
        t += 2;
        pick.pb(neg2[nt--]);
      }
    }

    ll ans = 1;
    for (ll x : pick)
      ans = (ans * (x % MOD)) % MOD;
    cout << ans << endl;
  }
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */

Error: 0

Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define TRACE(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "]";
  err(++it, args...);
}

#define MAXN ((int)2e5 + 5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<int, int>
#define mid ((l + r) / 2)

int n, k;
vector<ll> neg, pos, neg2, pick;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a > 0)
      pos.pb(a);
    else
      neg.pb(a);
  }

  if (n == k) {
    ll ans = 1;
    for (ll x : pos)
      ans = (ans * (x)) % MOD;
    for (ll x : neg)
      ans = (ans * (x)) % MOD;

    cout << (ans + MOD) % MOD << endl;
  } else if ((pos.size() == 0) and (k % 2)) {
    ll ans = 1;
    sort(neg.rbegin(), neg.rend());
    for (int i = 0; i < k; ++i)
      ans = (ans * (neg[i])) % MOD;
    cout << (ans + MOD) % MOD << endl;
  } else {
    sort(all(pos));
    sort(all(neg));

    for (int i = 0; i < (int)neg.size() - 1; i += 2)
      neg2.pb(neg[i] * neg[i + 1]);

    sort(all(neg2));

    int t = 0;
    int pt = pos.size() - 1;
    int nt = neg2.size() - 1;

    while (t + 2 <= k and nt >= 0 and pt > 0) {
      if (neg2[nt] > (pos[pt] * pos[pt - 1]))
        pick.pb(neg2[nt--]);
      else {
        pick.pb((pos[pt] * pos[pt - 1]));
        pt -= 2;
      }

      t += 2;
    }

    if (t < k) {
      if (pt >= 0) {
        while (t < k) {
          pick.pb(pos[pt--]);
          t++;
        }
      } else if (nt >= 0) {
        while (t + 1 < k) {
          pick.pb(neg2[nt--]);
          t += 2;
        }

        if (t < k && pt >= 0)
          pick.pb(pos[pt--]);
      }
    }

    ll ans = 1;
    for (ll x : pick)
      ans = (ans * (x % MOD)) % MOD;
    cout << ans << endl;
  }
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */