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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

using vi = vector<int>;

const int Z = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  vi a0, a1, a2;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;

    if (t > 0) {
      a2.push_back(t);
    } else if (t == 0) {
      a1.push_back(t);
    } else {
      a0.push_back(t);
    }
  }

  sort(a0.begin(), a0.end());
  sort(a2.begin(), a2.end());

  int ans = 1;
  if ((int)a0.size() + (int)a2.size() < k) {
    ans = 0;
  } else if (a2.size() > 0) {
    if (k % 2 == 1) {
      ans = a2.back();
      a2.pop_back();
      k--;
    }

    for (int i = 0, p = 0, q = (int)a2.size() - 1; i < k; i += 2) {
      if ((p + 1 >= (int)a0.size() && q - 1 >= 0) ||
          (q - 1 >= 0 &&
           1ll * a2.at(q) * a2.at(q - 1) >= 1ll * a0.at(p) * a0.at(p + 1))) {
        ans = 1ll * ans * a2.at(q) % Z * a2.at(q - 1) % Z;
        q -= 2;
      } else if ((q - 1 < 0 && p + 1 < (int)a0.size()) ||
                 (p + 1 < (int)a0.size() &&
                  1ll * a0.at(p) * a0.at(p + 1) >=
                      1ll * a2.at(q) * a2.at(q - 1))) {
        ans = 1ll * ans * a0.at(p) % Z * a0.at(p + 1) % Z;
        p += 2;
      } else if (a1.size() > 0) {
        ans = 0;
      } else {
        ans = 1ll * ans * a2.at(q) % Z * a0.at(p) % Z;
        p++, q--;
      }
    }
  } else if (k % 2 == 0) {
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * a0.at(i) % Z;
    }
  } else if (a1.size() > 0) {
    ans = 0;
  } else {
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * a0.at(a0.size() - 1 - i) % Z;
    }
  }

  ans = (ans + Z) % Z;

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

using vi = vector<int>;

const int Z = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  vi a0, a1, a2;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;

    if (t > 0) {
      a2.push_back(t);
    } else if (t == 0) {
      a1.push_back(t);
    } else {
      a0.push_back(t);
    }
  }

  sort(a0.begin(), a0.end());
  sort(a2.begin(), a2.end());

  int ans = 1;
  if ((int)a0.size() + (int)a2.size() < k) {
    ans = 0;
  } else if (a2.size() > 0) {
    if (k % 2 == 1) {
      ans = a2.back();
      a2.pop_back();
      k--;
    }

    for (int i = 0, p = 0, q = (int)a2.size() - 1; i < k; i += 2) {
      if (p + 1 >= (int)a0.size() ||
          (q - 1 >= 0 &&
           1ll * a2.at(q) * a2.at(q - 1) >= 1ll * a0.at(p) * a0.at(p + 1))) {
        ans = 1ll * ans * a2.at(q) % Z * a2.at(q - 1) % Z;
        q -= 2;
      } else if (q - 1 < 0 || (p + 1 < (int)a0.size() &&
                               1ll * a0.at(p) * a0.at(p + 1) >=
                                   1ll * a2.at(q) * a2.at(q - 1))) {
        ans = 1ll * ans * a0.at(p) % Z * a0.at(p + 1) % Z;
        p += 2;
      } else {
        ans = 0;
        break;
      }
    }
  } else if (k % 2 == 0) {
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * a0.at(i) % Z;
    }
  } else if (a1.size() > 0) {
    ans = 0;
  } else {
    for (int i = 0; i < k; i++) {
      ans = 1ll * ans * a0.at(a0.size() - 1 - i) % Z;
    }
  }

  ans = (ans + Z) % Z;

  cout << ans << endl;

  return 0;
}