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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

void solve() {
  int n, K;
  scanf("%d%d", &n, &K);
  vector<int> v0, v1, v2;
  int i;
  for (i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &tmp);
    if (tmp == 0)
      v0.push_back(tmp);
    else if (tmp > 0)
      v1.push_back(tmp);
    else
      v2.push_back(tmp);
  }
  int n0 = (int)v0.size();
  int n1 = (int)v1.size();
  int n2 = (int)v2.size();
  int n2new = MIN(n2, K) / 2 * 2;
  if (n1 + n2 < K) {
    printf("0\\n");
    return;
  }
  if (n1 + n2new < K) {
    if (!v0.empty()) {
      printf("0\\n");
      return;
    }
    assert(n0 == 0);

    for (i = 0; i < (int)v2.size(); i++) {
      v1.push_back(abs(v2[i]));
    }
    sort(v1.begin(), v1.end());
    ll ss = 1;
    for (i = 0; i < K; i++) {
      ss = ss * v1[i] % MOD;
    }
    printf("%lld\\n", (MOD - ss) % MOD);
    return;
  }
  sort(v1.rbegin(), v1.rend());
  sort(v2.begin(), v2.end());

  ll ss = 1;
  for (i = 0; i < n2new; i++) {
    ll tmp = (MOD - (-v2[i]) % MOD) % MOD;
    ss = ss * tmp % MOD;
  }
  int n1sel0 = K - n2new;
  assert(n1sel0 <= n1);
  for (i = 0; i < n1sel0; i++) {
    ll tmp = v1[i] % MOD;
    ss = ss * tmp % MOD;
  }

  int n1sel = n1sel0;
  for (i = 0; i < n2new / 2; i++) {
    if (n1sel + 2 > n1)
      break;
    ll tmp0 = (ll)v2[n2new - 1 - i * 2];
    ll tmp1 = (ll)v2[n2new - 1 - i * 2 - 1];
    ll tmp2 = (ll)v1[n1sel];
    ll tmp3 = (ll)v1[n1sel + 1];
    n1sel += 2;
    if (tmp0 * tmp1 >= tmp2 * tmp3) {
      break;
    }
    ll tmp = tmp2 * tmp3 % MOD * minv(tmp0) % MOD * minv(tmp1) % MOD;
    ss = ss * tmp % MOD;
  }
  printf("%lld\\n", ss);
  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  int t;
  for (t = 0; t < T; t++) {
    // printf("Case #%d: ", t+1);
    solve();
  }
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

void solve() {
  int n, K;
  scanf("%d%d", &n, &K);
  vector<int> v0, v1, v2;
  int i;
  for (i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &tmp);
    if (tmp == 0)
      v0.push_back(tmp);
    else if (tmp > 0)
      v1.push_back(tmp);
    else
      v2.push_back(tmp);
  }
  int n0 = (int)v0.size();
  int n1 = (int)v1.size();
  int n2 = (int)v2.size();
  int n2new = MIN(n2, K) / 2 * 2;
  if (n1 + n2 < K) {
    printf("0\\n");
    return;
  }
  if (n1 + n2new < K) {
    if (!v0.empty()) {
      printf("0\\n");
      return;
    }
    assert(n1 == 0);
    return;

    sort(v2.rbegin(), v2.rend());
    ll ss = 1;
    for (i = 0; i < K; i++) {
      ll tmp = (MOD - (-v2[i]) % MOD) % MOD;
      ss = ss * tmp % MOD;
    }
    printf("%lld\\n", ss);
    return;
  }
  sort(v1.rbegin(), v1.rend());
  sort(v2.begin(), v2.end());

  ll ss = 1;
  for (i = 0; i < n2new; i++) {
    ll tmp = (MOD - (-v2[i]) % MOD) % MOD;
    ss = ss * tmp % MOD;
  }
  int n1sel0 = K - n2new;
  assert(n1sel0 <= n1);
  for (i = 0; i < n1sel0; i++) {
    ll tmp = v1[i] % MOD;
    ss = ss * tmp % MOD;
  }

  int n1sel = n1sel0;
  for (i = 0; i < n2new / 2; i++) {
    if (n1sel + 2 > n1)
      break;
    ll tmp0 = (ll)v2[n2new - 1 - i * 2];
    ll tmp1 = (ll)v2[n2new - 1 - i * 2 - 1];
    ll tmp2 = (ll)v1[n1sel];
    ll tmp3 = (ll)v1[n1sel + 1];
    n1sel += 2;
    if (tmp0 * tmp1 >= tmp2 * tmp3) {
      break;
    }
    ll tmp = tmp2 * tmp3 % MOD * minv(tmp0) % MOD * minv(tmp1) % MOD;
    ss = ss * tmp % MOD;
  }
  printf("%lld\\n", ss);
  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  int t;
  for (t = 0; t < T; t++) {
    // printf("Case #%d: ", t+1);
    solve();
  }
#endif
  return 0;
}