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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vp;
typedef set<ll> si;
typedef map<string, ll> msi;
#define REP(i, a, b) for (ll i = ll(a); i <= ll(b); i++)
#define DEP(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
#define TR(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
ll mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int T;
  T = 1;
  while (T--) {
    ll n, i, j, k, l, m;
    cin >> n >> k;
    string s;
    vector<ll> v, pos, neg;

    REP(i, 0, n - 1) {
      cin >> j;
      v.pb(j);
      if (j >= 0)
        pos.pb(j);
      else {
        neg.pb(j);
      }
    }
    sort(v.begin(), v.end());
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
    stack<ll> s1, s2;
    REP(i, 0, pos.size() - 1) s1.push(pos[i]);
    DEP(i, neg.size() - 1, 0) s2.push(neg[i]);
    ll cnt = 0;
    vector<ll> resp, resn;
    ll p1 = -1, n1 = 1;
    while (!s1.empty() && !s2.empty() && k--) {
      ll x = s1.top(), y = s2.top();
      if (abs(x) >= abs(y)) {
        resp.pb(x);
        s1.pop();
        p1 = x;
      } else {
        cnt++;
        resn.pb(y);
        s2.pop();
        n1 = y;
      }
    }
    while (k > 0 && !s1.empty()) {
      resp.pb(s1.top());
      p1 = s1.top();
      s1.pop();
      k--;
    }

    while (k > 0 && !s2.empty()) {
      resn.pb(s2.top());
      n1 = s2.top();
      s2.pop();
      k--;
      cnt++;
    }
    if (cnt % 2 != 0) {
      if (s2.empty() && !s1.empty() && n1 < 0) {
        resn.pop_back();
        resp.pb(s1.top());
      } else if (s1.empty() && !s2.empty() && p1 >= 0) {
        resp.pop_back();
        resn.pb(s2.top());
      } else if (!s1.empty() && !s2.empty()) {
        if ((p1 == -1) || (p1 * s1.top() >= n1 * s2.top())) {
          resn.pop_back();
          resp.pb(s1.top());
        } else {
          resp.pop_back();
          resn.pb(s2.top());
        }
      } else {
        resn.clear();
        DEP(i, neg.size() - 1, 0) {
          if (cnt <= 0)
            break;
          resn.pb(neg[i]);
          cnt--;
        }
      }
    }
    ll ans = 1;
    REP(i, 0, resp.size() - 1) { ans = (ans * ((resp[i] + mod) % mod)) % mod; }

    REP(i, 0, resn.size() - 1) { ans = (ans * ((resn[i] + mod) % mod)) % mod; }
    cout << ans;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vp;
typedef set<ll> si;
typedef map<string, ll> msi;
#define REP(i, a, b) for (ll i = ll(a); i <= ll(b); i++)
#define DEP(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
#define TR(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
ll mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int T;
  T = 1;
  while (T--) {
    ll n, i, j, k, l, m;
    cin >> n >> k;
    string s;
    vector<ll> v, pos, neg;

    REP(i, 0, n - 1) {
      cin >> j;
      v.pb(j);
      if (j >= 0)
        pos.pb(j);
      else {
        neg.pb(j);
      }
    }
    sort(v.begin(), v.end());
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
    stack<ll> s1, s2;
    REP(i, 0, pos.size() - 1) s1.push(pos[i]);
    DEP(i, neg.size() - 1, 0) s2.push(neg[i]);
    ll cnt = 0;
    vector<ll> resp, resn;
    ll p1 = -1, n1 = 1;
    while (!s1.empty() && !s2.empty() && k--) {
      ll x = s1.top(), y = s2.top();
      if (abs(x) >= abs(y)) {
        resp.pb(x);
        s1.pop();
        p1 = x;
      } else {
        cnt++;
        resn.pb(y);
        s2.pop();
        n1 = y;
      }
    }
    while (k > 0 && !s1.empty()) {
      resp.pb(s1.top());
      p1 = s1.top();
      s1.pop();
      k--;
    }

    while (k > 0 && !s2.empty()) {
      resn.pb(s2.top());
      n1 = s2.top();
      s2.pop();
      k--;
      cnt++;
    }
    sort(resp.begin(), resp.end());
    sort(resn.begin(), resn.end());
    if (cnt % 2 != 0) {
      if (s2.empty() && !s1.empty() && n1 < 0) {
        resn.erase(lower_bound(resn.begin(), resn.end(), n1));
        resp.pb(s1.top());
      } else if (s1.empty() && !s2.empty() && p1 >= 0) {
        resp.erase(lower_bound(resp.begin(), resp.end(), p1));
        resn.pb(s2.top());
      } else if (!s1.empty() && !s2.empty()) {
        if (p1 * s1.top() >= n1 * s2.top()) {
          resn.erase(lower_bound(resn.begin(), resn.end(), n1));
          resp.pb(s1.top());
        } else {
          resp.erase(lower_bound(resp.begin(), resp.end(), p1));
          resn.pb(s2.top());
        }
      } else {
        resn.clear();
        DEP(i, n - 1, 0) {
          if (cnt <= 0)
            break;
          resn.pb(neg[i]);
          cnt--;
        }
      }
    }
    ll ans = 1;
    REP(i, 0, resp.size() - 1) { ans = (ans * ((resp[i] + mod) % mod)) % mod; }

    REP(i, 0, resn.size() - 1) { ans = (ans * ((resn[i] + mod) % mod)) % mod; }
    cout << ans;
  }

  return 0;
}