alexjercan/bugnet huggingface dataset:
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a.begin(), a.end());
  ll ans = 1, sign = 1;
  int l = 0, r = n - 1;
  if (k % 2) {
    ans = a[r--];
    --k;
    if (ans < 0)
      sign = -1;
  }
  while (k) {
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * sign > y * sign) {
      ans = x % mod * ans % mod;
      l += 2;
    } else {
      ans = y % mod * ans % mod;
      r -= 2;
    }
    k -= 2;
  }
  printf("%lld", (ans + mod) % mod);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

int n, k;
const int MAX = 2e5 + 5;
int a[MAX], b[MAX];

bool cmp(int x, int y) { return x > y; }

int main() {

  while (~scanf("%d %d", &n, &k)) {
    long long ans = 1;
    int cnt = 0;
    int positive_tot = 0;
    int n_tot = 0;
    for (int i = 0; i < n; i++) {
      long long x;
      scanf("%lld", &x);
      if (x >= 0)
        a[positive_tot++] = x;
      else
        b[n_tot++] = x;
      // cnt++;
    }
    if (positive_tot == 0) {
      long long ans = 1;
      sort(b, b + n_tot);
      if (k % 2 == 0) {
        for (int i = 0; i < n_tot; i++) {
          ans = (ans * b[i]) % MOD;
          k--;
          if (k == 0)
            break;
        }
        cout << ans % MOD << endl;
      } else {
        for (int i = n_tot - 1; i >= 0; i--) {
          ans = (ans * b[i]) % MOD;
          k--;
          if (k == 0)
            break;
        }
        cout << (ans + MOD) % MOD << endl;
      }
    } else if (n_tot == 0) {
      long long ans = 1;
      sort(a, a + positive_tot);
      for (int i = positive_tot - 1; i >= 0; i--) {
        ans = (ans * a[i]) % MOD;
        k--;
        if (k == 0)
          break;
      }

      cout << ans % MOD << endl;
    } else {
      long long ans = 1;
      sort(a, a + positive_tot, cmp);
      sort(b, b + n_tot);
      int tempa = 0, tempb = 0;
      for (int i = 0; i < max(positive_tot, n_tot); i++) {
        if (k != 1) {
          if ((a[tempa] * a[tempa + 1]) > (b[tempb] * b[tempb + 1])) {
            ans = (ans * a[tempa] * a[tempa + 1]) % MOD;
            tempa += 2;
          } else {
            ans = (ans * b[tempb] * b[tempb + 1]) % MOD;
            tempb += 2;
          }
        } else if (k == 1) {
          ans = (ans * a[tempa]) % MOD;
        }
        k = k - 2;
        if (k == 0)
          break;
      }
      cout << ans << endl;
    }
  }

  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define N 1000000007
int main() {
  int64_t n, k, pos = 0, neg = 0, zero = 0, flag = 0, negct = 0, posct = 0,
                zeroct = 0;
  cin >> n >> k;
  boost::multiprecision::cpp_int ans = 1;
  int64_t a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      pos++;
    else if (a[i] < 0)
      neg++;
    else
      zero++;
  }
  sort(a, a + n);
  if (k % 2 == 1 && pos + zero == 0)
    flag = 1;

  if (flag == 1) {
    while (k > 0) {
      ans *= a[neg - 1 - negct];
      ans %= N;
      negct++;
      k--;
    }
    ans %= N;
    while (ans < 0) {
      ans += N;
    }
    cout << ans;
    return 0;
  }

  while (k >= 2) {
    if (a[0 + negct] * a[1 + negct] >= a[n - 1 - posct] * a[n - 2 - posct]) {
      ans *= a[0 + negct];
      ans *= a[1 + negct];
      ans %= N;
      negct += 2;
      k -= 2;
    } else {
      ans *= a[n - 1 - posct];
      ans *= a[n - 2 - posct];
      ans %= N;
      posct += 2;
      k -= 2;
    }
  }
  if (k == 1) {
    ans *= a[n - 1 - posct];
  }
  ans %= N;
  while (ans < 0) {
    ans += N;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define N 1000000007
int main() {
  int64_t n, k, pos = 0, neg = 0, zero = 0, flag = 0, negct = 0, posct = 0,
                zeroct = 0;
  cin >> n >> k;
  boost::multiprecision::cpp_int ans = 1;
  int64_t a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      pos++;
    else if (a[i] < 0)
      neg++;
    else
      zero++;
  }
  sort(a, a + n);
  if (k % 2 == 1 && pos + zero == 0)
    flag = 1;

  if (flag == 1) {
    while (k > 0) {
      ans *= a[neg - 1 - negct];
      negct++;
      k--;
    }
    ans %= N;
    while (ans < 0) {
      ans += N;
    }
    cout << ans;
    return 0;
  }
  while (k >= 2) {
    if (a[0 + negct] * a[1 + negct] > a[n - 1 - posct] * a[n - 2 - posct]) {
      ans *= a[0 + negct] * a[1 + negct];
      negct += 2;
      k -= 2;
    } else {
      ans *= a[n - 1 - posct] * a[n - 2 - posct];
      posct += 2;
      k -= 2;
    }
  }
  if (k == 1) {
    ans *= a[n - 1 - posct];
  }
  ans %= N;
  while (ans < 0) {
    ans += N;
  }
  cout << ans;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll mol = 1e9 + 7;
const int maxn = 2e5;

int a[maxn + 11], b[maxn + 11];

int main() {
  int k, n;
  scanf("%d %d", &n, &k);
  int ca = 0, cb = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x >= 0)
      a[++ca] = x;
    else
      b[++cb] = -x;
  }
  sort(a + 1, a + 1 + ca, [&](int x, int y) { return x > y; });
  sort(b + 1, b + 1 + cb, [&](int x, int y) { return x > y; });
  if (ca == 0) {
    ll ans = 1;
    if (k % 2 == 1) {
      for (int i = cb; i >= cb - k + 1; i--)
        ans = ans * b[i] % mol;
      ans = mol - ans;
    } else {
      for (int i = 1; i <= k; i++)
        ans = ans * b[i] % mol;
    }
    printf("%lld\\n", ans);
    return 0;
  }
  if (n == k) {
    ll ans = 1;
    for (int i = 1; i <= ca; i++)
      ans = ans * a[i] % mol;
    for (int i = 1; i <= cb; i++)
      ans = ans * b[i] % mol;
    if (cb & 1)
      ans = mol - ans;
    printf("%lld\\n", ans);
    return 0;
  }
  int u, d;
  if (ca >= k) {
    u = k;
    d = 0;
  } else if ((k - ca) % 2 == 0) {
    u = ca;
    d = k - ca;
  } else {
    u = ca - 1;
    d = k - ca + 1;
  }
  while (1) {
    if (u < 2 || d + 2 > cb)
      break;
    if (1ll * a[u] * a[u - 1] > 1ll * b[d + 1] * b[d + 2])
      break;
    u -= 2;
    d += 2;
  }
  ll ans = 1;
  for (int i = 1; i <= u; i++)
    ans = ans * a[i] % mol;
  for (int i = 1; i <= d; i++)
    ans = ans * b[i] % mol;
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int zheng[100005];
int ffu[100005];
const long long mod = 1e9 + 7;
bool cmp(int a, int b) { return abs(a) > abs(b); }

int n, k;
int main() {
  scanf("%d%d", &n, &k);
  int s1 = 0, s2 = 0, kk = k, cnt0 = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > 0) {
      zheng[s1] = a[i];
      s1++;
    }
    if (a[i] < 0) {
      ffu[s2] = a[i];
      s2++;
    }
    if (a[i] == 0)
      cnt0++;
  }
  if (n - cnt0 < k) {
    cout << 0 << endl;
    return 0;
  }
  sort(zheng, zheng + s1, cmp);
  sort(ffu, ffu + s2, cmp);
  long long ans = 1;
  int ss1 = 0, ss2 = 0;
  while (k > 0) {
    if (abs(zheng[ss1]) > abs(ffu[ss2]) || (ss2 > s2)) {
      ans = (ans * zheng[ss1]) % mod;
      ss1++;
      k--;
    } else {
      ans = (ans * ffu[ss2]) % mod;
      ss2++;
      k--;
    }
  }
  if (ans > 0) {
    printf("%lld\\n", ans % mod);
    return 0;
  }
  if (ans > 0) {
    printf("%lld\\n", ans % mod);
    return 0;
  }
  if (ans < 0 && s1 > 0) {
    long long ans1 = 1;
    if (ss1 < s1)
      ans1 = (ans / ffu[ss2 - 1]) * zheng[ss1] % mod;
    if (ss2 < s2)
      ans = (ans / zheng[ss1 - 1]) * ffu[ss2] % mod;
    printf("%lld", max(ans1, ans) % mod);
    return 0;
  }
  if (ans < 0 && s1 == 0) {
    sort(ffu, ffu + s2);
    long long ans2 = 1;
    for (int i = s2 - 1; i >= s2 - kk; i--)
      ans2 = (ffu[i] * ans2 % mod);
    printf("%lld", (ans2 + mod) % mod);
  }
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
long long int a[200005], s[200005], ps = 0;
const long long MOD = 1e9 + 7;
int main() {
  scanf("%d %d", &n, &k);
  long long res = 1;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i]);
  }
  sort(a, a + n);

  if (k % 2) {
    res = a[n - 1];
    int w = res > 0 ? 1 : -1;
    int l = 0, r = n - 2, cnt = 1;
    while (cnt < k && l < r) {
      if (w * a[l] * a[l + 1] > w * a[r] * a[r - 1]) {
        s[ps++] = a[l++];
        s[ps++] = a[l++];
        cnt += 2;
      } else {
        s[ps++] = a[r--];
        s[ps++] = a[r--];
        cnt += 2;
      }
    }
  } else {
    res = 1;
    int l = 0, r = n - 1, cnt = 0;
    while (cnt < k && l < r) {
      if (a[l] * a[l + 1] > a[r] * a[r - 1]) {
        s[ps++] = a[l++];
        s[ps++] = a[l++];
        cnt += 2;
      } else {
        s[ps++] = a[r--];
        s[ps++] = a[r--];
        cnt += 2;
      }
    }
  }
  for (int i = 0; i < ps; ++i) {
    res = (1l * (s[i] % MOD) * (res % MOD) + MOD) % MOD;
  }
  res = (res % MOD + MOD) % MOD;
  printf("%lld\\n", res);
  return 0;
}


Error: WA

Fixed submission:

// 及时当勉励，岁月不待人！
// 当前状态 : attempted
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
struct node {
  int val;
  int op;
  bool operator<(const node &b) const { return this->val > b.val; }
};
const int maxn = 2e5 + 120;
int tmp, n, k;
ll ans = 1;
node aaa[maxn];
int maxN = -1 * inf;
bool cmp(node a, node b) { return a.val > b.val; }
// 函数
int main() {
  // std::ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> tmp;
    maxN = max(maxN, tmp);
    if (tmp > 0) {
      aaa[i] = node{tmp, 1};
    } else {
      aaa[i] = node{-1 * tmp, 0};
    }
  }
  // aaa[0]=node{-1*inf,0};
  if (k == 1) {
    cout << (maxN % mod + mod) % mod << endl;
    return 0;
  }
  sort(aaa + 1, aaa + n, cmp);
  int now = 1;
  ll mut = 1;
  int isH = 0;
  while (k) {
    node t = aaa[now++];
    if (t.op == 1) {
      ans = (ans * t.val) % mod;
      --k;
    } else {
      if (isH == 1) {
        mut = (mut * t.val % mod) % mod;
        ans = (ans * mut) % mod;
        mut = 1;
        isH = 0;
        k -= 2;
      } else {
        mut = (mut * t.val) % mod;
        isH = 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, M = 1e6 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
#define mst(a) memset(a, 0, sizeof a)
#define lx x << 1
#define rx x << 1 | 1
#define reg register
#define PII pair<int, int>
#define fi first
#define se second
ll a[N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a, a + n);
  int l = 0, r = n - 1, w = 1;
  ll ans = 1;
  if (k & 1)
    ans = a[r--], k--, w = (ans < 0 ? -1 : 1);
  while (k) {
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * w > y * w)
      ans = (ans * (x % mod)) % mod, l += 2;
    else
      ans = (ans * (y % mod)) % mod, r -= 2;
    k -= 2;
  }
  printf("%lld\\n", (ans % mod + mod) % mod);
  return 0;
}

Error: 0

Fixed submission:

#include "algorithm"
#include "cstdio"
#include "iostream"
#include "vector"
using namespace std;
const long long mod = 1e9 + 7;
typedef long long ll;
int N, K;
vector<ll> pos, neg;
vector<ll> ans;
void input() {
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; ++i) {
    ll tmp;
    scanf("%lld", &tmp);
    if (tmp >= 0)
      pos.push_back(tmp);
    else
      neg.push_back(tmp);
  }
}
void calc() {
  ll ret = 1;
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  int i = pos.size() - 1;
  int j = 0, cnt = 0;
  if (neg.size() == 0) {
    while (cnt < K) {
      ans.push_back(pos[i]);
      --i;
      ++cnt;
    }
    return;
  } else if (pos.size() == 0) {
    if (K & 1) {
      for (int k = neg.size() - 1; cnt < K; --k) {
        ans.push_back(neg[k]);
        ++cnt;
      }
    } else {
      for (int k = 0; cnt < K; ++k) {
        ans.push_back(neg[k]);
        ++cnt;
      }
    }
    return;
  }
  if (K % 2) {
    ans.push_back(pos[i]);
    --i;
    cnt = 1;
  }
  // printf(" i = %d j = %d/n",i,j);
  while (cnt < K && i >= 1 && j < neg.size() - 1) {
    if (pos[i] * pos[i - 1] >= neg[j] * neg[j + 1]) {
      ans.push_back(pos[i]);
      ans.push_back(pos[i - 1]);
      cnt += 2;
      i -= 2;
    } else {
      ans.push_back(neg[j]);
      ans.push_back(neg[j + 1]);
      cnt += 2;
      j += 2;
    }
  }
  while (cnt < K) {
    while (i == 0) {
      if (j < neg.size() - 1 && neg[j] * neg[j + 1] >= pos[i]) {
        ans.push_back(neg[j]);
        ans.push_back(neg[j + 1]);
        j += 2;
        cnt += 2;
      } else {
        ans.push_back(pos[i]);
        ++cnt;
        --i;
      }
    }
    while (cnt < K) {
      ans.push_back(neg[j]);
      ++j;
      ++cnt;
    }
  }
}
int main() {
  input();
  calc();
  long long res = 1;
  for (int i = 0; i < ans.size(); i++)
    res = res * ans[i] % mod;
  if (res < 0)
    res = (res % mod + mod) % mod;
  printf("%lld\\n", res);
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9 + 7;
void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = a * b % MOD; }

int main() {
  int N, K;
  cin >> N >> K;

  // Aには全部入れて、Apos,
  // Anegはそれぞれ正と負の値のみ入れる。全て絶対値で管理。
  vector<int64_t> A, Apos, Aneg;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(abs(a));
    if (a > 0)
      Apos.push_back(a);
    if (a < 0)
      Aneg.push_back(abs(a));
  }
  int szp = Apos.size(), szn = Aneg.size();

  int64_t ans = 1;

  if (szp + szn / 2 * 2 >= K && (K % 2 == 0 || szp > 0)) {
    // 正にできる場合：絶対値を大きくしたい
    sort(Apos.rbegin(), Apos.rend());
    sort(Aneg.rbegin(), Aneg.rend());
    // Kが奇数ならまず正側の最大値を1個使う
    if (K % 2)
      ans = Apos[0];
    // 正負それぞれで絶対値の大きいものから2個ずつペアにして、大きいものから使う
    vector<int64_t> V;
    for (int i = K % 2; i + 1 < szp; i += 2)
      V.push_back(Apos[i] * Apos[i + 1]);
    for (int i = 0; i + 1 < szn; i += 2)
      V.push_back(Aneg[i] * Aneg[i + 1]);
    sort(V.rbegin(), V.rend());
    for (int i = 0; i < K / 2; i++)
      mul(ans, V[i] % MOD);
  } else {
    // 正にできない場合：絶対値を小さくしたい
    // 全部混ぜて絶対値の小さいものから使う
    sort(A.begin(), A.end());
    for (int i = 0; i < K; i++)
      mul(ans, A[i]);
    ans = (MOD - ans) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9 + 7;

void mul(int64_t &a, int64_t b) { a = a * b % MOD; }

int main() {
  // your code goes here
  int n, k, l;
  int64_t ans = 1;
  cin >> n >> k;
  vector<int> a, b;
  for (int i = 0; i < n; ++i) {
    cin >> l;
    if (l < 0)
      b.push_back(l);
    else if (l > 0)
      a.push_back(l);
  }

  int szn = b.size(), szp = a.size();

  if ((szp + szn / 2 * 2 >= k) && (k % 2 == 0 && szp > 0)) {
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    if (k % 2 == 1)
      ans = a[0];
    vector<int64_t> V;
    for (int i = k % 2; i + 1 < szp; i += 2)
      V.push_back(a[i] * a[i + 1]);
    for (int i = 0; i + 1 < szn; i += 2)
      V.push_back(b[i] * b[i + 1]);
    sort(V.rbegin(), V.rend());
    for (int i = 0; i < k / 2; i++)
      mul(ans, V[i] % MOD);
  } else {
    sort(b.begin(), b.end());
    for (int i = 0; i < k; i++)
      mul(ans, b[i]);
    ans = (MOD - ans) % MOD;
  }

  cout << ans << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<vector<int>>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

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

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 x) { return modpow(x, MOD - 2); }

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;

  vector<ll> p, m;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0) {
      p.push_back(a[i]);
      flag = true;
    } else
      m.push_back(a[i]);
  }
  sort(all(p), greater<ll>{});
  sort(all(m));
  ll ans = 1;

  if (n == k) {
    for (int i = 0; i < k; i++)
      ans = ans * a[i] % MOD;
  } else if (!flag) {
    if (k % 2 == 0) {
      sort(all(a));
      for (int i = 0; i < k; i++)
        ans = ans * a[i] % MOD;
    } else {
      sort(all(a), greater<ll>{});
      for (int i = 0; i < k; i++)
        ans = ans * a[i] % MOD;
    }
  } else {
    int posp = 0, posm = 0;
    auto update = [&](ll &x, ll y, int &id) {
      x = x * y % MOD;
      id++;
    };
    for (int i = 0; i < k; i++) {
      if (posm == m.size() || p[posp] > -m[posm]) {
        update(ans, p[posp], posp);
      } else
        update(ans, m[posm], posm);
    }

    if (ans < 0) {
      if (posm == m.size() || posp == 0 ||
          p[posp - 1] * p[posp] > m[posm - 1] * m[posm]) {
        ans = ans * modinv(m[posm - 1]) % MOD * p[posp] % MOD;
      } else
        ans = ans * modinv(p[posp - 1]) % MOD * m[posm] % MOD;
    }
  }

  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N), B(N);
  vector<int> val_n;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(all(A), [&](int i, int j) { return llabs(A[i]) > llabs(A[j]); });
  ll res = 1;
  set<int> ids;
  int cnt = 0, pos, cur = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < 0)
      pos = i, cnt++;
    cur++;
    if (cur == K) {
      if (cnt % 2 == 1) {
        cnt--;
        cur--;
        ids.insert(pos);
      } else
        break;
    }
  }
  cur = 0;
  for (int i = 0; i < N; i++) {
    if (ids.size() && ids.count(i))
      continue;
    res = res * abs(A[i]) % MOD;
    cur++;
    if (cur == K)
      break;
  }
  cout << res << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll m = 1e9 + 7;
ll mod(ll n) { return (n % m + m) % m; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x >= 0) {
      pos.push_back(x);
    } else {
      neg.push_back(x);
    }
  }
  if (pos.size() == 0) {
    sort(neg.begin(), neg.end());
    if (k & 1) {
      reverse(neg.begin(), neg.end());
    }
    ll p = 1;
    for (int i = 0; i < k; i++) {
      p = mul(p, neg[i]);
    }
    cout << p << endl;
  } else if (neg.size() == 0) {
    sort(pos.begin(), pos.end(), greater<ll>());
    ll p = 1;
    for (int i = 0; i < k; i++) {
      p = mul(p, pos[i]);
    }
    cout << p << endl;
  } else {
    ll ans = 1;
    if (k & 1) {
      sort(pos.begin(), pos.end());
      ans = mul(ans, pos[pos.size() - 1]);
      pos.pop_back();
      k--;
    }
    vector<ll> ok;
    sort(pos.begin(), pos.end(), greater<ll>());
    sort(neg.begin(), neg.end());

    for (int i = 0; i < (int)pos.size() - 1; i += 2) {
      ok.push_back(pos[i] * pos[i + 1]);
    }
    for (int i = 0; i < (int)neg.size() - 1; i += 2) {
      ok.push_back(neg[i] * neg[i + 1]);
    }
    if (pos.size() % 2 && neg.size() % 2) {
      ok.push_back(pos[pos.size() - 1] * neg[neg.size() - 1]);
    }
    sort(ok.begin(), ok.end(), greater<ll>());
    for (int i = 0; i < (k / 2); i++) {
      ans = mul(ans, ok[i]);
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll m = 1e9 + 7;
ll mod(ll n) { return (n % m + m) % m; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0) {
      pos.push_back(x);
    } else {
      neg.push_back(x);
    }
  }
  if (pos.size()) {

    ll ans = 1;
    if (k & 1) {
      sort(pos.begin(), pos.end());
      ans = mul(ans, pos[pos.size() - 1]);
      pos.pop_back();
      k--;
    }
    vector<ll> ok;
    sort(pos.begin(), pos.end(), greater<ll>());
    sort(neg.begin(), neg.end());

    for (int i = 0; i < (int)pos.size() - 1; i += 2) {
      ok.push_back(pos[i] * pos[i + 1]);
    }
    for (int i = 0; i < (int)neg.size() - 1; i += 2) {
      ok.push_back(neg[i] * neg[i + 1]);
    }
    if (pos.size() % 2 == 0 && neg.size() % 2 == 0) {
      ok.push_back(pos[pos.size() - 1] * neg[neg.size() - 1]);
    }
    sort(ok.begin(), ok.end(), greater<ll>());
    for (int i = 0; i < (k / 2); i++) {
      ans = mul(ans, ok[i]);
    }
    cout << ans << endl;
  }

  else {
    sort(neg.begin(), neg.end());
    if (k & 1) {
      reverse(neg.begin(), neg.end());
    }
    ll p = 1;
    for (int i = 0; i < k; i++) {
      p = mul(p, neg[i]);
    }
    cout << p << endl;
  }
}