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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

ll llmin(ll x, ll y) {
  if (x > y)
    return y;
  return x;
}

ll llmax(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a) { return modpow(a, MOD - 2); }

int main() {
  ll n, k, mincoun = 0;
  cin >> n >> k;
  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      ans = ans * x % MOD;
    }
    if (ans < 0)
      cout << ans + MOD << endl;
    else
      cout << ans << endl;
    return 0;
  }
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      mincoun++;
  }
  if (mincoun == n && k % 2) {
    sort(a.begin(), a.end(), greater<ll>());
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans = ans * a[i] % MOD;
    }
    cout << MOD + ans << endl;
    return 0;
  }
  vector<pair<ll, ll>> vec(n);
  for (int i = 0; i < n; i++) {
    vec[i] = make_pair(abs(a[i]), a[i] / abs(a[i]));
  }
  sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());

  ll ans = 1, coun = 0;
  for (int i = 0; i < k; i++) {
    if (vec[i].second == -1)
      coun++;
    ans = ans * vec[i].first % MOD;
  }
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }
  if (coun % 2) {
    ll prid = MOD, mrid = MOD;
    ll pin = -1, miin = -1;
    for (int i = 0; i < k; i++) {
      if (vec[i].second == 1) {
        prid = llmin(prid, vec[i].first);
      } else if (vec[i].second == -1) {
        mrid = llmin(mrid, vec[i].first);
      }
    }
    for (int i = k; i < n; i++) {
      if (vec[i].second == -1) {
        miin = llmax(miin, vec[i].first);
      } else {
        pin = llmax(pin, vec[i].first);
      }
    }
    if ((prid != MOD && miin != -1) && (mrid != MOD && pin != -1)) {
      if (miin * mrid > pin * prid) {
        cout << (ans * modinv(prid)) % MOD * miin % MOD << endl;
      } else {
        cout << (ans * modinv(mrid)) % MOD * pin % MOD << endl;
      }
    } else if (prid != MOD && miin != -1) {
      cout << (ans * modinv(prid)) % MOD * miin % MOD << endl;
    } else {
      cout << (ans * modinv(mrid)) % MOD * pin % MOD << endl;
    }
  } else {
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

ll llmin(ll x, ll y) {
  if (x > y)
    return y;
  return x;
}

ll llmax(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a) { return modpow(a, MOD - 2); }

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> vec;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      ans = ans * x % MOD;
    }
    cout << ans << endl;
    return 0;
  }
  ll tmp = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x < 0)
      tmp++;
    if (x == 0)
      vec.push_back(make_pair(0, 0));
    else
      vec.push_back(make_pair(abs(x), x / abs(x)));
  }
  if (tmp == n && k % 2) {
    ll ans = 1;
    sort(vec.begin(), vec.end());
    for (int i = 0; i < k; i++) {
      ans = ans * vec[i].first % MOD;
    }
    if (ans == 0)
      cout << 0 << endl;
    else
      cout << MOD - ans << endl;
    return 0;
  }
  sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());
  ll ans = 1, coun = 0;
  for (int i = 0; i < k; i++) {
    ans = ans * vec[i].first % MOD;
    if (vec[i].second == -1)
      coun++;
  }
  if (vec[k - 1].first == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (coun % 2) {
    ll minplus = MOD, minminus = MOD;
    ll maxplus = 0, maxminus = 0;
    for (int i = 0; i < k; i++) {
      if (vec[i].second == 1) {
        minplus = llmin(minplus, vec[i].first);
      } else if (vec[i].second == -1) {
        minminus = llmin(minminus, vec[i].first);
      }
    }
    for (int i = k; i < n; i++) {
      if (vec[i].second == 1) {
        maxplus = llmax(maxplus, vec[i].first);
      } else if (vec[i].second == -1) {
        maxminus = llmax(maxminus, vec[i].first);
      }
    }
    if (minminus != MOD && maxplus != 0) {
      if (minplus != MOD && minminus != 0) {
        if (maxminus / minplus > maxplus / minminus) {
          cout << (ans * modinv(minplus) % MOD) * maxminus % MOD << endl;
        } else {
          cout << (ans * modinv(minminus) % MOD) * maxplus % MOD << endl;
        }
      } else {
        cout << (ans * modinv(minminus) % MOD) * maxplus % MOD << endl;
      }
    } else {
      if (minplus != MOD && minminus != 0) {
        cout << (ans * modinv(minplus) % MOD) * maxminus % MOD << endl;
      } else {
        cout << vec[10000000].first << endl;
      }
    }
  } else {
    cout << ans << endl;
  }
  return 0;
}