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

#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

typedef long long int ll;

#define MOD (ll)(1e9 + 7)
#define endl "\\n"
#define pb push_back
#define TEST_CASES                                                             \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define forn(i, a, n) for (int i = a; i < n; ++i)
#define f first
#define s second
#define all(x) x.begin(), x.end()

ll powD(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
    y = y >> 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n), pos, neg;
  forn(i, 0, n) {
    int x;
    cin >> x;
    a[i] = x;
    if (x >= 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  sort(all(a));
  sort(all(pos));
  sort(all(neg));
  if (pos.size() == 0 || k == n) {
    if (k % 2) {
      ll maxm = 1;
      forn(i, 0, k) {
        maxm *= a[n - i - 1];
        maxm = (maxm % MOD + MOD) % MOD;
      }
      cout << maxm << endl;
    } else {
      ll maxm = 1;
      forn(i, 0, k) {
        maxm *= a[i];
        maxm = (maxm % MOD + MOD) % MOD;
      }
      cout << maxm << endl;
    }
    return 0;
  } else if (neg.size() == 0) {
    ll maxm = 1;
    forn(i, 0, k) {
      maxm *= a[n - i - 1];
      maxm = (maxm % MOD + MOD) % MOD;
    }
    cout << maxm << endl;
    return 0;
  }
  stack<ll> poss, negs, ansp, ansn;
  forn(i, 0, neg.size()) { negs.push(neg[neg.size() - i - 1]); }
  forn(i, 0, pos.size()) { poss.push(pos[i]); }
  forn(i, 0, k) {
    if (poss.empty()) {
      ansn.push(negs.top());
      negs.pop();
    } else if (negs.empty()) {
      ansp.push(poss.top());
      poss.pop();
    } else if (abs(poss.top()) > abs(negs.top())) {
      ansp.push(poss.top());
      poss.pop();
    } else {
      ansn.push(negs.top());
      negs.pop();
    }
  }

  if (ansn.size() % 2) {
    if (ansp.empty()) {
      ansn.pop();
      ansp.push(poss.top());
      poss.pop();
    } else {
      ll candip = -1, candin = -1;
      if (!poss.empty()) {
        candip = poss.top() * ansp.top();
      }
      if (!negs.empty()) {
        candin = negs.top() * ansn.top();
      }
      if (candip > candin) {
        ansp.push(poss.top());
        ansn.pop();
      } else {
        ansn.push(negs.top());
        ansp.pop();
      }
    }
  }
  ll maxm = 1;
  while (!ansn.empty()) {
    maxm *= ansn.top();
    maxm = (maxm % MOD + MOD) % MOD;
    ansn.pop();
  }
  while (!ansp.empty()) {
    maxm *= ansp.top();
    maxm = (maxm % MOD + MOD) % MOD;
    ansp.pop();
  }
  cout << maxm << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

typedef long long int ll;

#define MOD (ll)(1e9 + 7)
#define endl "\\n"
#define pb push_back
#define TEST_CASES                                                             \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define forn(i, a, n) for (int i = a; i < n; ++i)
#define f first
#define s second
#define all(x) x.begin(), x.end()

ll powD(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
    y = y >> 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n), pos, neg;
  forn(i, 0, n) {
    int x;
    cin >> x;
    a[i] = x;
    if (x >= 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  sort(all(a));
  sort(all(pos));
  sort(all(neg));
  if (pos.size() == 0 || k == n) {
    if (k % 2) {
      ll maxm = 1;
      forn(i, 0, k) {
        maxm *= a[n - i - 1];
        maxm = (maxm % MOD + MOD) % MOD;
      }
      cout << maxm << endl;
    } else {
      ll maxm = 1;
      forn(i, 0, k) {
        maxm *= a[i];
        maxm = (maxm % MOD + MOD) % MOD;
      }
      cout << maxm << endl;
    }
  } else if (neg.size() == 0) {
    ll maxm = 1;
    forn(i, 0, k) {
      maxm *= a[n - i - 1];
      maxm = (maxm % MOD + MOD) % MOD;
    }
    cout << maxm << endl;
  }
  stack<ll> poss, negs, ansp, ansn;
  forn(i, 0, neg.size()) { negs.push(neg[neg.size() - i - 1]); }
  forn(i, 0, pos.size()) { poss.push(pos[i]); }
  forn(i, 0, k) {
    if (poss.empty()) {
      ansn.push(negs.top());
      negs.pop();
    } else if (negs.empty()) {
      ansp.push(poss.top());
      poss.pop();
    } else if (abs(poss.top()) > abs(negs.top())) {
      ansp.push(poss.top());
      poss.pop();
    } else {
      ansn.push(negs.top());
      negs.pop();
    }
  }

  if (ansn.size() % 2) {
    if (ansp.empty()) {
      ansn.pop();
      ansp.push(poss.top());
      poss.pop();
    } else {
      ll candip = -1, candin = -1;
      if (!poss.empty()) {
        candip = poss.top() * ansp.top();
      }
      if (!negs.empty()) {
        candin = negs.top() * ansn.top();
      }
      if (candip > candin) {
        ansp.push(poss.top());
        ansn.pop();
      } else {
        ansn.push(negs.top());
        ansp.pop();
      }
    }
  }
  ll maxm = 1;
  while (!ansn.empty()) {
    maxm *= ansn.top();
    maxm = (maxm % MOD + MOD) % MOD;
    ansn.pop();
  }
  while (!ansp.empty()) {
    maxm *= ansp.top();
    maxm = (maxm % MOD + MOD) % MOD;
    ansp.pop();
  }
  cout << maxm << endl;
}