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

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
#define PB push_back
#define MP make_pair
const int maxn = 200009;
const int MOD = 1e9 + 7;
int main() {
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A, B, C;
  for (int i = 0; i < n; ++i) {
    int64_t t;
    cin >> t;
    if (t == 0)
      C.PB(t);
    else if (t > 0)
      A.PB(t);
    else
      B.PB(t);
  }
  int N = SZ(B) / 2 * 2;
  while (N > k)
    N -= 2;
  if (N + SZ(A) < k) {
    if (SZ(C)) {
      cout << 0;
    } else {
      vector<int64_t> D;
      int64_t res = 1;
      for (auto &i : A)
        D.PB(i);
      for (auto &i : B)
        D.PB(i);
      sort(D.begin(), D.end(),
           [](int64_t a, int64_t b) { return abs(a) < abs(b); });
      for (int i = 0; i < k; ++i)
        res = res * D[i] % MOD;
      cout << (res % MOD + MOD) % MOD << '\\n';
    }
    return 0;
  }
  vector<int64_t> D;
  for (auto &i : A)
    D.PB(i);
  for (auto &i : B)
    D.PB(i);
  sort(D.begin(), D.end(),
       [](int64_t a, int64_t b) { return abs(a) > abs(b); });
  vector<int64_t> D1, D2;
  for (int i = 0; i < k; ++i) {
    if (D[i] > 0)
      D1.PB(D[i]);
    else
      D2.PB(D[i]);
  }
  if (SZ(D2) % 2 == 0) {
    int64_t res = 1;
    for (int i = 0; i < k; ++i)
      res = res * D[i] % MOD;
    cout << (res % MOD + MOD) % MOD;
    return 0;
  }
  if (SZ(D1) == 0) {
    for (int i = k; i < SZ(D); ++i) {
      if (D[i] > 0) {
        D1.PB(D[i]);
        D2.pop_back();
        break;
      }
    }
  } else {
    int idx1 = -1, idx2 = -1;
    for (int i = k; i < SZ(D); ++i) {
      if (D[i] > 0) {
        idx1 = i;
        break;
      }
    }
    for (int i = k; i < SZ(D); ++i)
      if (D[i] < 0) {
        idx2 = i;
        break;
      }
    if (idx1 == -1 || D[idx1] * D1.back() < D[idx2] * D2.back()) {
      D2.PB(D[idx2]);
      D1.pop_back();
    } else {
      D1.PB(D[idx1]);
      D2.pop_back();
    }
  }
  int64_t res = 1;
  for (auto i : D1)
    res = res * i % MOD;
  for (auto i : D2)
    res = res * i % MOD;
  cout << (res % MOD + MOD) % MOD << '\\n';
  return 0;
}
// 2020.07.05 20:28:44


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
#define PB push_back
#define MP make_pair
const int maxn = 200009;
const int MOD = 1e9 + 7;
int main() {
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A, B, C;
  for (int i = 0; i < n; ++i) {
    int64_t t;
    cin >> t;
    if (t == 0)
      C.PB(t);
    else if (t > 0)
      A.PB(t);
    else
      B.PB(t);
  }
  int N = SZ(B) / 2 * 2;
  while (N > k)
    N -= 2;
  if (N + SZ(A) < k) {
    if (SZ(C)) {
      cout << 0;
    } else {
      vector<int64_t> D;
      int64_t res = 1;
      for (auto &i : A)
        D.PB(i);
      for (auto &i : B)
        D.PB(i);
      sort(D.begin(), D.end(),
           [](int64_t a, int64_t b) { return abs(a) < abs(b); });
      for (int i = 0; i < k; ++i)
        res = res * D[i] % MOD;
      cout << (res % MOD + MOD) % MOD << '\\n';
    }
    return 0;
  }
  vector<int64_t> D;
  for (auto &i : A)
    D.PB(i);
  for (auto &i : B)
    D.PB(i);
  sort(D.begin(), D.end(),
       [](int64_t a, int64_t b) { return abs(a) > abs(b); });
  vector<int64_t> D1, D2;
  for (int i = 0; i < k; ++i) {
    if (D[i] > 0)
      D1.PB(D[i]);
    else
      D2.PB(D[i]);
  }
  if (SZ(D2) % 2 == 0) {
    int64_t res = 1;
    for (int i = 0; i < k; ++i)
      res = res * D[i] % MOD;
    cout << (res % MOD + MOD) % MOD;
    return 0;
  }
  assert(0);
  int64_t cmp1 = -1, cmp2 = -1, res1 = 1, res2 = 1;
  if (SZ(D1) != 0) {
    for (int i = k; i < n; ++i)
      if (D[i] > 0) {
        cmp1 = D1.back() * D[i];
        break;
      }
    if (cmp1 != -1) {
      res1 = cmp1;
      for (int i = 0; i < SZ(D1) - 1; ++i)
        (res1 *= D1[i]) %= MOD;
      for (int i = 0; i < SZ(D2) - 1; ++i)
        (res1 *= D2[i]) %= MOD;
    }
  }
  if (SZ(D2) != 0) {
    for (int i = k; i < n; ++i)
      if (D[i] < 0) {
        cmp2 = D2.back() * D[i];
        break;
      }
    if (cmp2 != -1) {
      res2 = cmp2;
      for (int i = 0; i < SZ(D1) - 1; ++i)
        (res2 *= D1[i]) %= MOD;
      for (int i = 0; i < SZ(D2) - 1; ++i)
        (res2 *= D2[i]) %= MOD;
    }
  }
  if (cmp1 > cmp2) {
    cout << (res1 % MOD + MOD) % MOD << '\\n';
  } else {
    cout << (res2 % MOD + MOD) % MOD << '\\n';
  }
  return 0;
}
// 2020.07.05 20:28:44