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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI 3.14159265358979323846
#define lp(i, n) for (int i = 0; i < n; i++)
#define range1(i, a, b, c) for (int i = int(a); i <= int(b); i += int(c))
#define range(i, a, b) for (int i = int(a); i <= int(b); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define sz(v) ((int)((v).size()))
#define sfi1(v) scanf("%d", &v)
#define sfi2(v1, v2) scanf("%d %d", &v1, &v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d", &v1, &v2, &v3)
#define sfll1(v) scanf("%I64d", &v);
#define sfll2(v1, v2) scanf("%I64d %I64d", &v1, &v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d", &v1, &v2, &v3)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)1e6;
const int N = (int)1e7;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
#endif
  vi neg, pos;
  vector<ll> ans;
  int n, k;
  scanf("%d%d", &n, &k);
  int z = 0;
  ll res = 1;
  lp(i, n) {
    int x;
    scanf("%d", &x);
    if (x > 0)
      pos.pb(x);
    else if (x < 0)
      neg.pb(-x);
    else
      z++;
    res = (res * x % MOD + MOD) % MOD;
  }
  sort(all(pos));
  sort(all(neg));
  int idx = 0, it = 0, nev = 0, temp = k;
  if (n == k) {
    printf("%lld", res);
    return 0;
  } else if (sz(pos) + sz(neg) / 2 * 2 < k) {
    puts("0");
    return 0;
  }
  res = 1;
  if (k & 1) {
    if (sz(pos)) {
      res *= (ll)pos.back() % MOD;
      pos.pop_back();
      k--;
    } else {
      if (z) {
        puts("0");
        return 0;
      }
      lp(i, k) { res = (res * (-neg[i]) % MOD + MOD) % MOD; }
      printf("%lld", res);
      return 0;
    }
  }
  for (int i = sz(pos); i >= 2; i -= 2) {
    ans.pb(1ll * pos[i - 1] * pos[i - 2]);
  }
  for (int i = sz(neg); i >= 2; i -= 2) {
    ans.pb(1ll * neg[i - 1] * neg[i - 2]);
  }
  k /= 2;
  sort(all(ans));
  reverse(all(ans));
  lp(i, k) { res = 1ll * (ans[i] % MOD) * res % MOD; }
  printf("%lld", res);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI 3.14159265358979323846
#define lp(i, n) for (int i = 0; i < n; i++)
#define range1(i, a, b, c) for (int i = int(a); i <= int(b); i += int(c))
#define range(i, a, b) for (int i = int(a); i <= int(b); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define sz(v) ((int)((v).size()))
#define sfi1(v) scanf("%d", &v)
#define sfi2(v1, v2) scanf("%d %d", &v1, &v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d", &v1, &v2, &v3)
#define sfll1(v) scanf("%I64d", &v);
#define sfll2(v1, v2) scanf("%I64d %I64d", &v1, &v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d", &v1, &v2, &v3)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)1e6;
const int N = (int)1e7;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll mult(ll a, ll b) {
  ll res = (a % MOD * b % MOD) % MOD;
  return res;
}
int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
#endif
  vi neg, pos;
  vector<ll> ans;
  int n, k;
  scanf("%d%d", &n, &k);
  int z = 0;
  ll res = 1;
  lp(i, n) {
    int x;
    scanf("%d", &x);
    if (x > 0)
      pos.pb(x);
    else if (x < 0)
      neg.pb(-x);
    else
      z++;
    res = mult(res, (ll)x);
  }
  sort(all(pos));
  sort(all(neg));
  int idx = 0, it = 0, nev = 0, temp = k;
  if (n == k) {
    printf("%lld", res);
    return 0;
  }
  res = 1;
  if (k & 1) {
    if (sz(pos)) {
      res *= (ll)pos.back();
      pos.pop_back();
      k--;
    } else {
      if (z) {
        puts("0");
        return 0;
      }
      lp(i, k) { res = (res * (-neg[i]) % MOD + MOD) % MOD; }
      printf("%lld", res);
      return 0;
    }
  }
  for (int i = sz(pos); i - 2 >= 0; i -= 2) {
    ans.pb((ll)pos[i - 1] * (ll)pos[i - 2]);
  }
  for (int i = sz(neg); i - 2 >= 0; i -= 2) {
    ans.pb((ll)neg[i - 1] * (ll)neg[i - 2]);
  }
  k /= 2;
  sort(all(ans));
  reverse(all(ans));
  lp(i, k) { res = mult(res, ans[i]); }
  printf("%lld", res);
  return 0;
}