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;

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define MOD (ull)(double)(1e9 + 7)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vs vector<string>
#define pull pair<ull, ull>
#define F first
#define S second
#define m_p make_pair
#define debug(x) cout << #x << " " << x << endl
#define printArr(a, start, end)                                                \\
  for (int i = start; i < end; ++i)                                            \\
    cout << a[i] << " ";
#define readArr(a, start, end)                                                 \\
  for (int i = start; i < end; ++i)                                            \\
    cin >> a[i];
#define read2D(mat, n, m)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> mat[i][j];

const long double PI = 3.141592653589793238462643383279502884197169399;

// Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1))

void solve() {
  int n, k;
  cin >> n >> k;

  vll a(n);
  int ne = 0;
  for (ll &x : a) {
    cin >> x;
    if (x < 0)
      ne++;
  }

  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; ++i) {
      ans *= a[i];
      if (ans < 0)
        ans = MOD - (abs(ans) % MOD);
      else
        ans %= MOD;
    }

    cout << ans;
    return;
  }

  sort(all(a), [](ll x, ll y) -> bool { return abs(x) > abs(y); });

  if (ne == n) {
    ll ans = 1;
    if (k & 1) {
      for (int i = n - 1; i > n - k - 1; --i) {
        ans *= a[i];
        ans = MOD - (abs(ans) % MOD);
      }
    } else {
      for (int i = 0; i < k; ++i) {
        ans *= a[i];
        if (ans < 0)
          ans = MOD - (abs(ans) % MOD);
        else
          ans %= MOD;
      }
    }

    cout << ans;
    return;
  }

  int cnt = 0;
  vb take(n, 0);
  for (int i = 0; i < k; ++i) {
    if (a[i] < 0)
      cnt++;
    take[i] = 1;
  }

  if (cnt & 1) {
    int posLeft = -1, negLeft = -1;
    for (int i = k; i < n; ++i) {
      if (a[i] < 0) {
        negLeft = i;
        break;
      }
    }
    for (int i = k; i < n; ++i) {
      if (a[i] >= 0) {
        posLeft = i;
        break;
      }
    }

    int leastPos = -1, leastNeg = -1;
    for (int i = k - 1; i >= 0; --i) {
      if (a[i] < 0) {
        leastNeg = i;
        break;
      }
    }
    for (int i = k - 1; i >= 0; --i) {
      if (a[i] >= 0) {
        leastPos = i;
        break;
      }
    }
    if (posLeft == -1 || leastPos == -1 || negLeft == -1) {
      if (posLeft == -1) {
        take[leastPos] = 0;
        take[negLeft] = 1;
      } else {
        if (a[posLeft] != 0 || negLeft == -1) {
          take[leastNeg] = 0;
          take[posLeft] = 1;
        } else {
          if (leastPos != -1) {
            take[negLeft] = -1;
            take[leastPos] = 0;
          } else {
            take[leastNeg] = 0;
            take[posLeft] = 1;
          }
        }
      }
    } else {
      if (a[posLeft] * a[leastPos] > a[negLeft] * a[leastNeg]) {
        take[posLeft] = 1;
        take[leastNeg] = 0;
      } else {
        take[negLeft] = 1;
        take[leastPos] = 0;
      }
    }
  }

  ll ans = 1;
  for (int i = 0; i < n; ++i) {
    if (take[i]) {
      ans *= a[i];
      if (ans < 0)
        ans = MOD - (abs(ans) % MOD);
      else
        ans %= MOD;
    }
  }

  cout << ans;
}

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

  int tt = 1;
  // cin >> tt;

  while (tt--) {
    solve();
  }
}


Error: WA

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;

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define MOD (ull)(double)(1e9 + 7)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vs vector<string>
#define pull pair<ull, ull>
#define F first
#define S second
#define m_p make_pair
#define debug(x) cout << #x << " " << x << endl
#define printArr(a, start, end)                                                \\
  for (int i = start; i < end; ++i)                                            \\
    cout << a[i] << " ";
#define readArr(a, start, end)                                                 \\
  for (int i = start; i < end; ++i)                                            \\
    cin >> a[i];
#define read2D(mat, n, m)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> mat[i][j];

const long double PI = 3.141592653589793238462643383279502884197169399;

// Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1))

void solve() {
  int n, k;
  cin >> n >> k;

  vll a(n);
  int ne = 0;
  for (ll &x : a) {
    cin >> x;
    if (x < 0)
      ne++;
  }

  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; ++i) {
      ans *= a[i];
      if (ans < 0)
        ans = MOD - (abs(ans) % MOD);
      else
        ans %= MOD;
    }

    cout << ans;
    return;
  }

  sort(all(a), [](ll x, ll y) -> bool { return abs(x) > abs(y); });

  if (ne == n) {
    ll ans = 1;
    for (int i = n - 1; i > n - k - 1; --i) {
      ans *= a[i];
      ans = MOD - (abs(ans) % MOD);
    }

    cout << ans;
    return;
  }

  int cnt = 0;
  vb take(n, 0);
  for (int i = 0; i < k; ++i) {
    if (a[i] < 0)
      cnt++;
    take[i] = 1;
  }

  if (cnt & 1) {
    int posLeft = -1, negLeft = -1;
    for (int i = k; i < n; ++i) {
      if (a[i] < 0) {
        negLeft = i;
        break;
      }
    }
    for (int i = k; i < n; ++i) {
      if (a[i] > 0) {
        posLeft = i;
        break;
      }
    }

    int leastPos = -1, leastNeg = -1;
    for (int i = k - 1; i >= 0; --i) {
      if (a[i] < 0) {
        leastNeg = i;
        break;
      }
    }
    for (int i = k - 1; i >= 0; --i) {
      if (a[i] > 0) {
        leastPos = i;
        break;
      }
    }
    if (posLeft == -1 || leastPos == -1 || negLeft == -1) {
      if (posLeft == -1) {
        take[leastPos] = 0;
        take[negLeft] = 1;
      } else {
        take[leastNeg] = 0;
        take[posLeft] = 1;
      }
    } else {
      if (a[posLeft] * a[leastPos] > a[negLeft] * a[leastNeg]) {
        take[posLeft] = 1;
        take[leastNeg] = 0;
      } else {
        take[negLeft] = 1;
        take[leastPos] = 0;
      }
    }
  }

  ll ans = 1;
  for (int i = 0; i < n; ++i) {
    if (take[i]) {
      ans *= abs(a[i]);
      ans += MOD;
      ans %= MOD;
    }
  }

  cout << ans;
}

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

  int tt = 1;
  // cin >> tt;

  while (tt--) {
    solve();
  }
}