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

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 61)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int n, k, a[202020];
vector<i64> c, d;
void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      c.push_back(a[i]);
    else
      d.push_back(abs(a[i]));
  }
  int C = c.size(), D = d.size();

  sort(c.begin(), c.end(), greater<i64>());
  sort(d.begin(), d.end(), greater<i64>());

  int cc = 0, dd = 0;
  i64 ans = 1;
  if (C == 0) {
    if (k % 2) {
      for (int i = 0; i < k; i++) {
        ans *= -d[D - i - 1];
        ans = mod(ans, INF);
      }
    } else {
      for (int i = 0; i < k; i++) {
        ans *= -d[i];
        ans = mod(ans, INF);
      }
    }
    cout << ans << endl;
    return;
  }
  if (n == k) {
    for (int i = 0; i < n; i++) {
      ans = mod(ans * a[i], INF);
    }
    cout << ans << endl;
    return;
  }

  if (k & 1) {
    ans *= c[cc];
    cc++;
    k--;
  }
  for (int i = 0; i < k; i += 2) {
    if (dd > D - 2) {
      ans *= (c[cc] * c[cc + 1]) % INF;
      ans %= INF;
      cc += 2;
    } else if (cc > C - 2) {
      ans *= (d[dd] * d[dd + 1]) % INF;
      ans %= INF;
      dd += 2;
    } else if (c[cc] * c[cc + 1] >= d[dd] * d[dd + 1]) {
      ans *= (c[cc] * c[cc + 1]) % INF;
      ans %= INF;
      cc += 2;
    } else {
      ans *= (d[dd] * d[dd + 1]) % INF;
      ans %= INF;
      dd += 2;
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 61)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int n, k, a[202020];
vector<i64> c, d;
void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      c.push_back(a[i]);
    else
      d.push_back(abs(a[i]));
  }
  int C = c.size(), D = d.size();

  sort(c.begin(), c.end(), greater<i64>());
  sort(d.begin(), d.end(), greater<i64>());

  int cc = 0, dd = 0;
  i64 ans = 1;
  if (C == 0) {
    for (int i = 0; i < k; i++) {
      ans *= -d[D - i - 1];
      ans = mod(ans, INF);
    }
    cout << ans << endl;
    return;
  }
  if (n == k) {
    for (int i = 0; i < n; i++) {
      ans = mod(ans * a[i], INF);
    }
    cout << ans << endl;
    return;
  }

  if (k & 1) {
    ans *= c[cc];
    cc++;
    k--;
  }
  for (int i = 0; i < k; i += 2) {
    if (dd > D + 2) {
      ans *= (c[cc] * c[cc + 1]) % INF;
      ans %= INF;
      cc += 2;
    } else if (cc > C + 2) {
      ans *= (d[dd] * d[dd + 1]) % INF;
      ans %= INF;
      dd += 2;
    } else if (c[cc] * c[cc + 1] >= d[dd] * d[dd + 1]) {
      ans *= (c[cc] * c[cc + 1]) % INF;
      ans %= INF;
      cc += 2;
    } else {
      ans *= (d[dd] * d[dd + 1]) % INF;
      ans %= INF;
      dd += 2;
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}