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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (intl i = 0, nn = (intl)(n); i < nn; ++i)
#define Rep1(i, n) for (intl i = 1, nn = (intl)(n) + 1; i < nn; ++i)
#define vec vector
#define fi first
#define se second
#define eb emplace_back
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
#define Input(a)                                                               \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
  }
#define Input2(a, b)                                                           \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
    cin >> (b).at(ii);                                                         \\
  }
#define mp make_pair
#define mt make_tuple
using intl = int64_t;
using vi = vec<intl>;
using vb = vec<bool>;
using vvi = vec<vi>;
using pi = pair<intl, intl>;
using vpi = vec<pi>;
using si = set<intl>;
using vsi = vec<si>;
using mii = map<intl, intl>;
using vmii = vec<mii>;
vec<intl> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<intl> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr intl INF = 9223372036854775807;
constexpr intl MOD = 1000000007;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  intl N, K;
  cin >> N >> K;
  vi pos, neg;
  pos.reserve(N);
  neg.reserve(N);

  intl a;
  Rep(i, N) {
    cin >> a;
    if (a > 0) {
      pos.eb(a);
    } else {
      neg.eb(a);
    }
  }

  intl ans = 1;
  intl next;

  if ((N == K && neg.size() % 2 == 1) ||
      ((intl)(neg.size()) == N && K % 2 == 1)) {
    SortD(neg);
    SortA(pos);

    deque<intl> negq;
    deque<intl> posq;

    for (auto &&n : neg) {
      negq.push_back(n);
    }
    for (auto &&p : pos) {
      posq.push_back(p);
    }

    Rep(i, K) {
      if (posq.empty()) {
        ans *= negq.front();
        ans %= MOD;
        negq.pop_front();
        continue;
      } else if (negq.empty()) {
        ans *= posq.front();
        ans %= MOD;
        posq.pop_front();
        continue;
      } else if (posq.front() > abs(negq.front())) {
        ans *= posq.front();
        ans %= MOD;
        posq.pop_front();
        continue;
      } else {
        ans *= negq.front();
        ans %= MOD;
        negq.pop_front();
        continue;
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  }

  SortA(neg);
  SortD(pos);

  deque<intl> negq;
  deque<intl> posq;

  for (auto &&n : neg) {
    negq.push_back(n);
  }
  for (auto &&p : pos) {
    posq.push_back(p);
  }

  if (K % 2) {
    for (intl i = K; i > 0; i -= 2) {
      if (i == 1) {
        ans *= posq.front();
        ans %= MOD;
        break;
      } else if ((intl)(posq.size()) <= 1) {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      } else if ((intl)(negq.size()) <= 1) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else if (posq[0] * posq[1] > negq[0] * negq[1]) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  } else {
    for (intl i = K; i > 0; i -= 2) {
      if ((intl)(posq.size()) <= 1) {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      } else if ((intl)(negq.size()) <= 1) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else if (posq[0] * posq[1] > negq[0] * negq[1]) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  }
}


Error: WA

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (intl i = 0, nn = (intl)(n); i < nn; ++i)
#define Rep1(i, n) for (intl i = 1, nn = (intl)(n) + 1; i < nn; ++i)
#define vec vector
#define fi first
#define se second
#define eb emplace_back
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
#define Input(a)                                                               \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
  }
#define Input2(a, b)                                                           \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
    cin >> (b).at(ii);                                                         \\
  }
#define mp make_pair
#define mt make_tuple
using intl = int64_t;
using vi = vec<intl>;
using vb = vec<bool>;
using vvi = vec<vi>;
using pi = pair<intl, intl>;
using vpi = vec<pi>;
using si = set<intl>;
using vsi = vec<si>;
using mii = map<intl, intl>;
using vmii = vec<mii>;
vec<intl> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<intl> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr intl INF = 9223372036854775807;
constexpr intl MOD = 1000000007;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  intl N, K;
  cin >> N >> K;
  vi pos, neg;
  pos.reserve(N);
  neg.reserve(N);

  intl a;
  Rep(i, N) {
    cin >> a;
    if (a > 0) {
      pos.eb(a);
    } else {
      neg.eb(a);
    }
  }

  intl ans = 1;
  intl next;

  if ((N == K && neg.size() % 2 == 1) ||
      ((intl)(neg.size()) == N && K % 2 == 1)) {
    SortD(neg);
    SortA(pos);

    deque<intl> negq;
    deque<intl> posq;

    for (auto &&n : neg) {
      negq.push_back(n);
    }
    for (auto &&p : pos) {
      posq.push_back(p);
    }

    Rep(i, K) {
      if (posq.empty()) {
        ans *= negq.front();
        ans %= MOD;
        negq.pop_front();
        continue;
      } else if (negq.empty()) {
        ans *= posq.front();
        ans %= MOD;
        posq.pop_front();
        continue;
      } else if (posq.front() > abs(negq.front())) {
        ans *= posq.front();
        ans %= MOD;
        posq.pop_front();
        continue;
      } else {
        ans *= negq.front();
        ans %= MOD;
        negq.pop_front();
        continue;
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  }

  SortA(neg);
  SortD(pos);

  deque<intl> negq;
  deque<intl> posq;

  for (auto &&n : neg) {
    negq.push_back(n);
  }
  for (auto &&p : pos) {
    posq.push_back(p);
  }

  if (K % 2) {
    for (intl i = K; i != 0; i -= 2) {
      if (i = 1) {
        ans *= posq.front();
        ans %= MOD;
        break;
      } else if ((intl)(posq.size()) == 1) {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      } else if (negq.empty()) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else if (posq[0] * posq[1] > negq[0] * negq[1]) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  } else {
    for (intl i = K; i != 0; i -= 2) {
      if ((intl)(posq.size()) == 0) {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      } else if (negq.empty()) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else if (posq[0] * posq[1] > negq[0] * negq[1]) {
        Rep(j, 2) {
          ans *= posq.front();
          ans %= MOD;
          posq.pop_front();
          continue;
        }
      } else {
        Rep(j, 2) {
          ans *= negq.front();
          ans %= MOD;
          negq.pop_front();
          continue;
        }
      }
    }
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans;
    return 0;
  }
}