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

#include <bits/stdc++.h> // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;              // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i)  // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r)                                                          \\
  for (int i = (int)(l); i <= (int)(r);                                        \\
       ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)   // reverse 1 based
#define max(a, b) (a > b) ? a : b                         // max of 2 elements
#define min(a, b) (a < b) ? a : b                         // min of 2 elements

// short hand for usual tokens
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mii map<int, int>
#define pqb priority_queue<int>                   // Max Heap
#define pqs priority_queue<int, vi, greater<int>> // Min Heap
#define setbits(x)                                                             \\
  __builtin_popcountll(                                                        \\
      x) // returns the number of set bits in the binary representation of x
#define zrobits(x)                                                             \\
  __builtin_ctzll(x) // returns the number of zeroes before the first set bit in
                     // the binary representation of x
#define mod 1000000007                            // 1e9 + 7
#define inf 1e18                                  // pow(10, 18)
#define mk(arr, n, type) type *arr = new type[n]; // generates a dynamic array
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--) // for taking test cases
mt19937 rng(chrono::steady_clock::now()
                .time_since_epoch()
                .count()); // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end()  // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto
// alleviates the pain.
#define tr(c, i)                                                               \\
  for (__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in
// log(n) for set and map
#define present(c, x) ((c).find(x) != (c).end())

// find version works for all containers. This is present in std namespace.
#define cpresent(c, x) (find(all(c), x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds; // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  ll n, k;
  cin >> n;
  cin >> k;
  vll arr(n), ans;
  ll numNeg = 0;
  for0(i, n) {
    cin >> arr[i];
    if (arr[i] < 0)
      numNeg++;
  }

  sort(all(arr));
  if (numNeg == n) {
    if (k % 2) {
      ll answer = 1LL;
      for (ll i = n - 1; i >= (n - k); i--)
        answer = (answer * arr[i]) % mod;

      cout << (mod + answer) % mod;
      return 0;
    } else {
      ll answer = 1LL;
      for (ll i = 0; i < k; i++)
        answer = (answer * arr[i]) % mod;

      cout << (mod + answer) % mod;
      return 0;
    }
  }

  ll i = 0, j = n - 1;
  while (i <= j && k) {
    if (arr[j] >= 0) {
      ll prod1 = arr[j] * arr[j - 1];
      ll prod2 = arr[i] * arr[i + 1];
      if (prod1 > prod2 || k == 1) {
        ans.pb(arr[j--]);
        k--;
      } else {
        ans.pb(arr[i++]);
        ans.pb(arr[i++]);
        k -= 2;
      }

    } else {
      if (k == 1) {
        ans.pb(arr[j--]);
        k--;
      } else {
        ans.pb(arr[i++]);
        ans.pb(arr[i++]);
        k -= 2;
      }
    }
  }

  ll answer = 1LL;
  for (ll i : ans)
    answer = (answer * i) % mod;

  if (answer < 0)
    answer += mod;

  cout << answer;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h> // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds;              // Only for g++ compiler
using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i)  // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r)                                                          \\
  for (int i = (int)(l); i <= (int)(r);                                        \\
       ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)   // reverse 1 based
#define max(a, b) (a > b) ? a : b                         // max of 2 elements
#define min(a, b) (a < b) ? a : b                         // min of 2 elements

// short hand for usual tokens
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mii map<int, int>
#define pqb priority_queue<int>                   // Max Heap
#define pqs priority_queue<int, vi, greater<int>> // Min Heap
#define setbits(x)                                                             \\
  __builtin_popcountll(                                                        \\
      x) // returns the number of set bits in the binary representation of x
#define zrobits(x)                                                             \\
  __builtin_ctzll(x) // returns the number of zeroes before the first set bit in
                     // the binary representation of x
#define mod 1000000007                            // 1e9 + 7
#define inf 1e18                                  // pow(10, 18)
#define mk(arr, n, type) type *arr = new type[n]; // generates a dynamic array
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--) // for taking test cases
mt19937 rng(chrono::steady_clock::now()
                .time_since_epoch()
                .count()); // used with shuffle

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end()  // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto
// alleviates the pain.
#define tr(c, i)                                                               \\
  for (__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in
// log(n) for set and map
#define present(c, x) ((c).find(x) != (c).end())

// find version works for all containers. This is present in std namespace.
#define cpresent(c, x) (find(all(c), x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds; // Used to implement policy-based data structure

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n, k, x;
  cin >> n;
  cin >> k;
  vi neg, pos, ans;
  for0(i, n) {
    cin >> x;
    if (x < 0)
      neg.pb(x);
    else
      pos.pb(x);
  }

  sort(all(neg));
  sort(all(pos), greater<int>());
  int n1 = neg.size(), n2 = pos.size();
  int i = 0, j = 0;
  while (k) {
    if ((i + 1) < n1 && (j + 1) < n2) {
      ll prod1 = 1LL * neg[i] * neg[i + 1];
      ll prod2 = 1LL * pos[j] * pos[j + 1];
      if (prod2 > prod1 || k == 1) {
        ans.pb(pos[j++]);
        k--;
      } else {
        ans.pb(neg[i++]);
        ans.pb(neg[i++]);
        k -= 2;
      }
    }

    if (j >= n2 && k) {
      ans.pb(neg[i++]);
      k--;
    }

    if (i >= n1 && k) {
      ans.pb(pos[j++]);
      k--;
    }

    if (j == n2 - 1 && k) {
      if ((i + 1) < n1) {
        ll prod = 1LL * neg[i] * neg[i + 1];
        if (pos[j] > prod || k == 1) {
          ans.pb(pos[j++]);
          k--;
        } else {
          ans.pb(neg[i++]);
          ans.pb(neg[i++]);
          k -= 2;
        }
      } else {
        ans.pb(pos[j++]);
        k--;
      }
    }

    if (i == n1 - 1 && k) {
      ans.pb(pos[j++]);
      k--;
    }
  }

  ll answer = 1LL;
  for (int i : ans)
    answer = (answer * i) % mod;

  if (answer < 0)
    answer += mod;

  cout << answer;

  return 0;
}