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

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 5e5 + 5;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, k;
vll pos, neg;

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0)
      pos.pb(x);
    else
      neg.pb(-x);
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());
  ll ans = 1;
  if (pos.size() == 0) {
    if (k & 1)
      reverse(neg.begin(), neg.end());
    for (int i = 0; i < k; i++)
      ans = (ans * neg[i]) % mod;
    if (k & 1)
      ans = (mod - ans);
    deb1(ans % mod) return;
  }
  int i = 0, j = 0;
  if (k & 1)
    ans = pos[0], i++, k--;
  for (int kk = 0; kk < k; kk += 2) {
    if (i + 1 == pos.size() and j + 1 == neg.size()) {
      ans = ans * pos[i] % mod * neg[j] % mod, i++, j++;
      ans = mod - ans;
    } else if (j + 1 >= neg.size())
      ans = ans * pos[i] % mod * pos[i + 1] % mod, i += 2;
    else if (i + 1 >= pos.size())
      ans = ans * neg[j] % mod * neg[j + 1] % mod, j += 2;
    else {
      ll x = pos[i] * pos[i + 1], y = neg[j] * neg[j + 1];
      if (x > y)
        ans = ans * (x % mod) % mod, i += 2;
      else
        ans = ans * (y % mod) % mod, j += 2;
    }
  }
  deb1(ans % mod)
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}

Error: WA

Fixed submission:

#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const ll MOD = 1e+9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll N = 2e+5 + 7;

vll adj[N];
ll visit[N] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, k;
ll a[N];
vll v1, v2, v;
//<<-----Implement Functions Here---->>//
ll mult(ll a, ll b) { return (a * b) % MOD; }
ll add(ll a, ll b) { return (a + b) % MOD; }
ll sub(ll a, ll b) { return (a - b + MOD) % MOD; }
bool compare(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a <= b)
    return 1;
  return 0;
}
//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> k;
  ll f = 0;
  ll ans = 1;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    v.pb(a[i]);
    if (a[i] >= 0)
      v1.pb(-a[i]);
    else
      v2.pb(a[i]);
  }

  sort(all(v1));
  sort(all(v2));
  sort(all(v), compare);
  reverse(all(v1));
  ll len1 = v1.size();
  ll len2 = v2.size();
  ll p1 = 0;
  ll p2 = 0;
  ll count1 = 0;
  if ((k & 1 && len1 == 0) || (k == n && (len2 & 1)))
    f = 1;
  if (f) {
    for (ll i = 0; i < k; i++)
      ans = mult(ans, v[i]);
    ans = MOD - ans;
    cout << (ans) % MOD;
    return;
  }
  ll flag = 0;
  while (1) {
    ll ele1 = 1e+18, ele2 = 1e+18, ele3 = 1e+18, ele4 = 1e+18;
    if (count1 == k)
      break;
    if (p1 < len1)
      ele1 = v1[p1];
    else {
      flag = 1;
      break;
    }
    if (p1 < len1 - 1)
      ele2 = v1[p1 + 1];
    else if ((k - count1) % 2 == 0) {
      flag = 1;
      break;
    }
    if (p2 < len2 - 1 && k - count1 >= 2) {
      ele3 = v2[p2];
      ele4 = v2[p2 + 1];
    } else {
      flag = 2;
      break;
    }
    if (ele2 == 1e+18) {
      ans = mult(ans, ele1);
      p1++;
      count1++;
    } else if (ele1 * ele2 < ele4 * ele3) {
      ans = mult(ans, ele3 * ele4);
      p2 += 2;
      count1 += 2;
    } else {
      ans = mult(ans, ele1);
      p1++;
      count1++;
    }
  }

  if (count1 != k) {
    ll d = k - count1;
    if (flag == 1) {
      for (ll i = p2; i < p2 + d; i++)
        ans = mult(ans, v2[i]);
    } else {
      for (ll i = p1; i < p1 + d; i++)
        ans = mult(ans, v1[i]);
    }
  }
  cout << ans;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }
}