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

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define db(a) cout << fixed << #a << " = " << a << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 1e9 + 7;

ll solve(ll n, ll k, vector<ll> &A) {
  vector<ll> P, N;
  ll zeros = 0;
  for (int i = 0; i < n; i++) {
    if (A[i] > 0)
      P.push_back(A[i]);
    if (A[i] < 0)
      N.push_back(-A[i]);
    if (A[i] == 0)
      zeros++;
  }
  sort(all(P));
  sort(all(N));
  if (k % 2 == 0) {
    ll g = 2 * ((int)P.size() / 2 + (int)N.size() / 2);
    if (g < k) {
      if (zeros) {
        return 0;
      } else {
        assert(n == k);
        ll r = 1;
        for (ll a : A) {
          r *= a;
          r %= MOD;
        }
        return (r + MOD) % MOD;
      }
    } else {
      ll result = 1;
      ll counter = 0;
      while (counter < k) {
        if (P.size() >= 2 && N.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];

          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          if (a * b > c * d) {
            result *= a;
            result %= MOD;
            result *= b;
            result %= MOD;
            P.pop_back();
            P.pop_back();
          } else {
            result *= c;
            result %= MOD;
            result *= d;
            result %= MOD;
            N.pop_back();
            N.pop_back();
          }
          counter += 2;
        } else if ((int)P.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];
          result *= a;
          result %= MOD;
          result *= b;
          result %= MOD;
          P.pop_back();
          P.pop_back();
          counter += 2;
        } else if ((int)N.size() >= 2) {
          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          result *= c;
          result %= MOD;
          result *= d;
          result %= MOD;
          N.pop_back();
          N.pop_back();
          counter += 2;
        } else {
          assert(false);
        }
      }
      return (result + MOD) % MOD;
    }
  } else {
    if ((int)P.size() == 0) {
      if (zeros)
        return 0;
      ll r = -1;
      for (int i = 0; i < k; i++) {
        r *= N[i];
        r %= MOD;
      }
      return (r + MOD) % MOD;
    }
    ll g = (int)P.size() + 2 * ((int)N.size() / 2);
    if (g < k) {
      if (zeros)
        return 0;
      else {
        assert(n == k);
        ll r = 1;
        for (ll a : A) {
          r *= a;
          r %= MOD;
        }
        return (r + MOD) % MOD;
      }
    } else {
      ll result = P.back();
      P.pop_back();
      ll counter = 1;
      while (counter < k) {
        if (P.size() >= 2 && N.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];

          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          if (a * b > c * d) {
            result *= a;
            result %= MOD;
            result *= b;
            result %= MOD;
            P.pop_back();
            P.pop_back();
          } else {
            result *= c;
            result %= MOD;
            result *= d;
            result %= MOD;
            N.pop_back();
            N.pop_back();
          }
          counter += 2;
        } else if ((int)P.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];
          result *= a;
          result %= MOD;
          result *= b;
          result %= MOD;
          P.pop_back();
          P.pop_back();
          counter += 2;
        } else if ((int)N.size() >= 2) {
          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          result *= c;
          result %= MOD;
          result *= d;
          result %= MOD;
          N.pop_back();
          N.pop_back();
          counter += 2;
        } else {
          assert(false);
        }
      }
      return (result + MOD) % MOD;
    }
  }
  return -1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cout << solve(n, k, A) << endl;
  // cases:
  // if (n % 2) == 0:
  // all odd, all even, 2k even and 2l odd
  // if (n % 2) == 1:
  // 		if A[i] < 0 for all i, then find min
  // 		else:
  //			all even
  //			2k odd and leftover even
  //
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define db(a) cout << fixed << #a << " = " << a << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 1e9 + 7;

ll solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  vector<ll> P, N;
  ll zeros = 0;
  for (int i = 0; i < n; i++) {
    if (A[i] > 0)
      P.push_back(A[i]);
    if (A[i] < 0)
      N.push_back(-A[i]);
    if (A[i] == 0)
      zeros++;
  }
  sort(all(P));
  sort(all(N));
  if (k % 2 == 0) {
    ll g = 2 * ((int)P.size() / 2 + (int)N.size() / 2);
    if (g < k) {
      if (zeros) {
        return 0;
      } else {
        assert(n == k);
        ll r = 1;
        for (ll a : A) {
          r *= a;
          r %= MOD;
        }
        return (r + MOD) % MOD;
      }
    } else {
      ll result = 1;
      ll counter = 0;
      while (counter < k) {
        if (P.size() >= 2 && N.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];

          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          if (a * b > c * d) {
            result *= a;
            result %= MOD;
            result *= b;
            result %= MOD;
            P.pop_back();
            P.pop_back();
          } else {
            result *= c;
            result %= MOD;
            result *= d;
            result %= MOD;
            N.pop_back();
            N.pop_back();
          }
          counter += 2;
        } else if ((int)P.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];
          result *= a;
          result %= MOD;
          result *= b;
          result %= MOD;
          P.pop_back();
          P.pop_back();
          counter += 2;
        } else if ((int)N.size() >= 2) {
          ll c = N[(int)N.size() - 1];
          ll d = N[(int)N.size() - 2];
          result *= c;
          result %= MOD;
          result *= d;
          result %= MOD;
          N.pop_back();
          N.pop_back();
          counter += 2;
        } else {
          assert(false);
        }
      }
      return (result + MOD) % MOD;
    }
  } else {
    if ((int)P.size() == 0) {
      if (zeros)
        return 0;
      ll r = -1;
      for (int i = 0; i < k; i++) {
        r *= N[i];
        r %= MOD;
      }
      return (r + MOD) % MOD;
    }
    ll g = (int)P.size() + 2 * ((int)N.size() / 2);
    if (g < k) {
      if (zeros)
        return 0;
      else {
        assert(n == k);
        ll r = 1;
        for (ll a : A) {
          r *= a;
          r %= MOD;
        }
        return (r + MOD) % MOD;
      }
    } else {
      ll result = P.back();
      P.pop_back();
      ll counter = 1;
      while (counter < k) {
        if (P.size() >= 2 && N.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];

          ll c = N[(int)P.size() - 1];
          ll d = N[(int)P.size() - 2];
          if (a * b > c * d) {
            result *= a;
            result %= MOD;
            result *= b;
            result %= MOD;
            P.pop_back();
            P.pop_back();
          } else {
            result *= c;
            result %= MOD;
            result *= d;
            result %= MOD;
            N.pop_back();
            N.pop_back();
          }
          counter += 2;
        } else if ((int)P.size() >= 2) {
          ll a = P[(int)P.size() - 1];
          ll b = P[(int)P.size() - 2];
          result *= a;
          result %= MOD;
          result *= b;
          result %= MOD;
          P.pop_back();
          P.pop_back();
          counter += 2;
        } else if ((int)N.size() >= 2) {
          ll c = N[(int)P.size() - 1];
          ll d = N[(int)P.size() - 2];
          result *= c;
          result %= MOD;
          result *= d;
          result %= MOD;
          N.pop_back();
          N.pop_back();
          counter += 2;
        } else {
          assert(false);
        }
      }
      return (result + MOD) % MOD;
    }
  }
  return -1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << solve() << endl;
  // cases:
  // if (n % 2) == 0:
  // all odd, all even, 2k even and 2l odd
  // if (n % 2) == 1:
  // 		if A[i] < 0 for all i, then find min
  // 		else:
  //			all even
  //			2k odd and leftover even
  //
}